猿问

根据 WooCommerce 购物车总数添加或删除特定购物车项目

如果订单总额高于 199.99 美元,我正在尝试将免费产品添加到购物车


我已经实现了这一点并且它正在发挥作用。问题是,如果用户随后从购物车中删除了一个商品并再次低于 199.99 美元(以防止游戏系统),我需要删除该产品。


我所拥有的似乎正在工作。问题是,在 REMOVE FROM CART 操作似乎起作用(或刷新页面)之前,我似乎需要单击 2 个链接。


这是什么原因造成的?是否可以通过 AJAX 完成删除操作?


// -------------------------------------------

// ADD PRODUCT IF ORDER MINIMUM ABOVE 200


/*

* Automatically adding the product to the cart when cart total amount reach to $199.99.

*/


function aapc_add_product_to_cart() {

    global $woocommerce;


    $cart_total = 199.99;   


    if ( $woocommerce->cart->total >= $cart_total ) {

        if ( is_user_logged_in() ) {

            $free_product_id = 339;  // Product Id of the free product which will get added to cart

            $found      = false;


            //check if product already in cart

            if ( sizeof( WC()->cart->get_cart() ) > 0 ) {

                foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {

                    $_product = $values['data'];

                    if ( $_product->get_id() == $free_product_id )

                        $found = true;                  

                }

                // if product not found, add it

                if ( ! $found )

                    WC()->cart->add_to_cart( $free_product_id );

            } else {

                // if no products in cart, add it

                WC()->cart->add_to_cart( $free_product_id );

            }        

        }

    }

    if ( $woocommerce->cart->total <= $cart_total && $found ) {

                WC()->cart->remove_cart_item( $free_product_id );

            }       

}


add_action( 'template_redirect', 'aapc_add_product_to_cart' );


add_action( 'template_redirect', 'remove_product_from_cart_programmatically' );



不负相思意
浏览 115回答 1
1回答

桃花长相依

您不应该使用template_redirect钩子根据购物车的总阈值添加或删除免费产品……此外,您的代码有点过时并存在一些错误。而是使用woocommerce_before_calculate_totals启用 Ajax 的钩子,这样:add_action( 'woocommerce_before_calculate_totals', 'add_or_remove_cart_items', 10, 1 );function add_or_remove_cart_items( $cart ) {&nbsp; &nbsp; if ( is_admin() && ! defined( 'DOING_AJAX' ) )&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; // ONLY for logged users (and avoiding the hook repetition)&nbsp;&nbsp; &nbsp; if ( ! is_user_logged_in() && did_action( 'woocommerce_before_calculate_totals' ) >= 2 )&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; $threshold_amount = 200; // The threshold amount for cart total&nbsp; &nbsp; $free_product_id&nbsp; = 339; // ID of the free product&nbsp; &nbsp; $cart_items_total = 0; // Initializing&nbsp; &nbsp; // Loop through cart items&nbsp; &nbsp; foreach ( $cart->get_cart() as $cart_item_key => $cart_item ){&nbsp; &nbsp; &nbsp; &nbsp; // Check if the free product is in cart&nbsp; &nbsp; &nbsp; &nbsp; if ( $cart_item['data']->get_id() == $free_product_id ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $free_item_key = $cart_item_key;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Get cart subtotal incl. tax from items (with discounts if any)&nbsp; &nbsp; &nbsp; &nbsp; $cart_items_total += $cart_item['line_total'] + $cart_item['line_tax'];&nbsp; &nbsp; }&nbsp; &nbsp; // If Cart total is up to the defined amount and if the free products is not in cart, we add it.&nbsp; &nbsp; if ( $cart_items_total >= $threshold_amount && ! isset($free_item_key) ) {&nbsp; &nbsp; &nbsp; &nbsp; $cart->add_to_cart( $free_product_id );&nbsp; &nbsp; }&nbsp; &nbsp; // If cart total is below the defined amount and free product is in cart, we remove it.&nbsp; &nbsp; elseif ( $cart_items_total < $threshold_amount && isset($free_item_key) ) {&nbsp; &nbsp; &nbsp; &nbsp; $cart->remove_cart_item( $free_item_key );&nbsp; &nbsp; }}代码在您的活动子主题(或活动主题)的functions.php 文件中。测试和工作。
随时随地看视频慕课网APP
我要回答