如果卡值<1,则删除产品。 X

时间:2017-02-07 13:27:51

标签: php wordpress action product

我希望有人可以提供帮助。我已经设置了一些代码(thanks to this post),如果购物车的价值超过50美元,我会将产品添加到购物车中。

有效。但是,如果价值低于50美元,我想确保产品不在购物车中。因此,如果用户添加了产品,则会添加其他产品,但他们会删除产品,因此价值低于50美元(不包括添加的新产品!)...我希望它删除已添加的产品。< / p>

我尝试按照上面提到的帖子实现代码:

add_action( 'template_redirect', 'add_product_to_cart' ); 

function add_product_to_cart() {
// Bonus products
$product_1 = '4751'; 
$product_2 = '4752'; 
$product_3 = '24711'; 

// Get cart value in a clean format
$cart_total = WC()->cart->get_cart_subtotal();
$cart_total = html_entity_decode($cart_total, ENT_QUOTES, 'UTF-8');
$cart_total_format = strip_tags($cart_total);
$cart_value = preg_filter("/[^0-9]/", "", $cart_total_format);
$sum_raw = $cart_value;

// Set the sum level 
$level3 = '50';

// Check sum and apply product
if ($sum_raw >= $level3) {

    // Cycle through each product in the cart and check for match
    $found = 'false';
        foreach (WC()->cart->cart_contents as $item) {
            global $product;
            $product_id = $item['variation_id'];

            if ($product_id == $product_3) {
                $found = 'true';
            }
        }

    // If product found we do nothing 
        if ($found == 'true') {}
    // else we will add it
        else {
        //We add the product
            WC()->cart->add_to_cart($product_3);
        }
}


// Check if sum
if ($sum_raw < $level3) {
    foreach ($woocommerce->cart->get_cart() as $cart_item_key =>     $cart_item) {

        if ($cart_item['variation_id'] == $product_3) {
                //remove single product
                $woocommerce->cart->remove_cart_item($cart_item_key);
            }
        }
    }

}

但它并没有删除产品。 :/任何人都知道为什么它不起作用,或者是否有更好的解决方案来检查购物车:如果购物车&gt; 50美元添加一个产品...如果它发生了变化并且去了&lt; 50美元,删除相同的产品...

并且还从支票中排除新添加的产品(因此&lt; $ 50不包括刚刚以编程方式添加的产品)。

请帮忙! :(

2 个答案:

答案 0 :(得分:0)

也许你必须改变这一行:

$cart_value = preg_filter("/[^0-9]/", "", $cart_total_format);

到(add - minus):

$cart_value = preg_filter("/[^0-9\-]/", "", $cart_total_format);

答案 1 :(得分:0)

尝试此代码...您目前正在尝试更新&#34;添加&#34;行动,而它应该在&#34;删除&#34;动作。

add_action( 'template_redirect', 'remove_product_from_cart' );
function remove_product_from_cart() {
    // Run only in the Cart or Checkout Page
    if( is_cart() || is_checkout() ) {


        /*
         GET THE CART TOTAL 
         */

        // Set the product ID to remove
        $prod_to_remove = PRODUCT-ID-TO-BE-REMOVED;

        // Cycle through each product in the cart
        foreach( WC()->cart->cart_contents as $prod_in_cart ) {
            // Get the Variation or Product ID
            $prod_id = ( isset( $prod_in_cart['variation_id'] ) && $prod_in_cart['variation_id'] != 0 ) ? $prod_in_cart['variation_id'] : $prod_in_cart['product_id'];
            // Check to see if IDs match
            if( $prod_to_remove == $prod_id ) {
                // Get it's unique ID within the Cart
                $prod_unique_id = WC()->cart->generate_cart_id( $prod_id );
                // Remove it from the cart by un-setting it
                unset( WC()->cart->cart_contents[$prod_unique_id] );
            }
        }
    }
}