【问题标题】:Removing WooCommerce Product programmatically以编程方式删除 WooCommerce 产品
【发布时间】:2015-06-01 22:37:22
【问题描述】:

目前,如果商品进入客户的购物车,我会动态添加产品 + 优惠券。

// add Sufficiency Naked Calories to cart

add_action( 'wp_loaded', 'wc_custom_add_to_cart', 80 );
function wc_custom_add_to_cart( $product )
{
    if (is_admin()) {
        return;
    }

    $cart = WC()->cart->get_cart();
    $products_to_check = array(10305, 10302, 10300, 10303, 10301);
    $book = 10311;
    $coupon_code = 'nutreincesufficiencyquiznakedcalories';
    foreach ($cart as $cart_item_key => $values) {
        $_product = $values['data'];

        if ($_product->id == $book) return; // product has already been added
        if (in_array($_product->id, $products_to_check)) {
            WC()->cart->add_to_cart($book);
            WC()->cart->add_discount($coupon_code);
            break;
        }
    }
}

我想编写一个函数,如果数组$products_to_check 中的任何项目离开购物车,我可以删除$book

到目前为止,我的尝试都没有成功

add_action( 'wp_loaded', 'wc_custom_add_to_cart', 80 );
function wc_custom_add_to_cart( $product )
{
    if (is_admin()) {
        return;
    }

    $cart = WC()->cart->get_cart();
    $products_to_check = array(10305, 10302, 10300, 10303, 10301);
    $book = 10311;
    $coupon_code = 'nutreincesufficiencyquiznakedcalories';
    foreach ($cart as $cart_item_key => $values) {
        $_product = $values['data'];

        if ($_product->id == $book) return; // product has already been added
        if (in_array($_product->id, $products_to_check)) {
            WC()->cart->add_to_cart($book);
            WC()->cart->add_discount($coupon_code);
        }

        if (in_array($_product->id, $products_to_check)) {
            return;
        } else {
            WC()->cart->remove_cart_item($book);
        }
    }
}

尝试 #2

add_action( 'wp_loaded', 'wc_custom_add_to_cart', 80 );
    function wc_custom_add_to_cart( $product )
    {
        if (is_admin()) {
            return;
        }

        $cart = WC()->cart->get_cart();
        $nutreince_sufficiency = array(10305, 10302, 10300, 10303, 10301);
        $book = 10310;
        $nutincart = false;
        $coupon_code = 'nutreincesufficiencyquiznakedcalories';
        foreach ($cart as $cart_item_key => $values) {
            $_product = $values['data'];

            if (in_array($_product->id, $nutreince_sufficiency)) {
                $nutincart = true;
                break;
            }
        }

        if ($nutincart) {
            WC()->cart->add_to_cart($book);
            WC()->cart->add_discount($coupon_code);
        } else {
            echo $_product->id;
            WC()->cart->set_quantity($book, 0);
        }
    }

从购物车中取出产品,仍将书留在原处。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    尝试更改 add_action 优先级以覆盖您的函数

    求助link

    【讨论】:

      【解决方案2】:

      函数set_quantity需要使用$cart_item_key作为目标产品,而不是$_product_id

      add_action( 'wp_loaded', 'wc_custom_add_to_cart', 80 );
      function wc_custom_add_to_cart( $product )
      {
          if (is_admin()) {
              return;
          }
      
          $cart = WC()->cart->get_cart();
          $nutreince_sufficiency = array(10305, 10302, 10300, 10303, 10301);
          $book = 10310;
          $nutincart = false;
          $book_cart_reference = 0;
          $coupon_code = 'nutreincesufficiencyquiznakedcalories';
          foreach ($cart as $cart_item_key => $values) {
              $_product = $values['data'];
      
              if (in_array($_product->id, $nutreince_sufficiency)) {
                  $nutincart = true;
                  break;
              }
      
              // get cart item key for $book
              if ( $_product->id == $book )
              {
                      $book_cart_reference = $cart_item_key;
              }
          }
      
          if ($nutincart) {
              WC()->cart->add_to_cart($book);
              WC()->cart->add_discount($coupon_code);
          } else {
              WC()->cart->set_quantity($book_cart_reference, 0);
          }
      }
      

      【讨论】:

        【解决方案3】:

        在下面的示例中,我的目标产品 ID = 282,并检查当前购物车总数是多少。如果我超过 50 美元的门槛,sn-p 会遍历购物车项目,如果找到产品就会停止。在这个阶段,它存储了它的“cart item key”,最后使用 remove_cart_item() 函数将其移除。

        add_action( 'template_redirect', 'remove_product_from_cart_programmatically' );
        
        function remove_product_from_cart_programmatically() {
         if ( is_admin() ) return;
             $product_id = 282; // product id
             $cart_total = 50; // replace with threshold
             $in_cart = false;
         foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
             if ( $cart_item['product_id'] === $product_id ) {
              $in_cart = true;
              $key = $cart_item_key;
              break;
             }
         }
          if( WC()->cart->total > $cart_total ) {
            if ( $in_cart ) WC()->cart->remove_cart_item( $key );
          }
         }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-01-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-04-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多