WordPress教程:WooCommerce访问时自动添加产品到购物车

访问时自动添加产品到购物车

  1. /*
  2. * Add item to cart on visit
  3. */
  4. function add_product_to_cart() {
  5. if ( ! is_admin() ) {
  6. global $woocommerce;
  7. $product_id = 64;
  8. $found = false;
  9. https://check if product already in cart
  10. if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
  11. foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
  12. $_product = $values['data'];
  13. if ( $_product->id == $product_id )
  14. $found = true;
  15. }
  16. https:// if product not found, add it
  17. if ( ! $found )
  18. $woocommerce->cart->add_to_cart( $product_id );
  19. } else {
  20. https:// if no products in cart, add it
  21. $woocommerce->cart->add_to_cart( $product_id );
  22. }
  23. }
  24. }
  25. add_action( 'init', 'add_product_to_cart' );

在哪里添加此代码?

将PHP代码放在主题或子主题functions.php文件的底部。

WordPress技巧

WordPress教程:WooCommerce清空购物车

2019-3-17 15:26:19

WordPress技巧

WordPress教程:WooCommerce添加自定义货币/符号

2019-3-17 15:30:54

0 条回复 A文章作者 M管理员
欢迎您,新朋友,感谢参与互动!
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
私信列表
搜索
51La