以编程方式删除WooCommerce产品

时间:2015-06-01 22:37:22

标签: php wordpress woocommerce

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

// 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;
        }
    }
}

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

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

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);
        }
    }

从购物车中取出产品,仍将书籍留在那里。

3 个答案:

答案 0 :(得分:1)

在下面的示例中,我将产品ID设置为282,并检查当前的购物车总数是多少。如果我超过了$ 50美元的门槛,则摘要会循环遍历购物车中的商品,并在找到该商品时停止。在此阶段,它存储其“购物车钥匙”,最后使用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 );
  }
 }

答案 1 :(得分:0)

尝试更改add_action优先级以覆盖您的功能

帮助link

答案 2 :(得分:0)

函数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);
    }
}