根据WooCommerce购物车总额添加或删除特定购物车项目 [英] Add or remove specific cart Item based on WooCommerce cart total

查看:140
本文介绍了根据WooCommerce购物车总额添加或删除特定购物车项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果订单总额超过$ 199.99,我正在尝试向购物车中添加免费产品

I am trying to add a free product to the cart if the order total is above $199.99

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

I have achieved this and it is working. The issue is that I need to remove the product if the user then deletes an item from the cart and goes below $199.99 again (to prevent gaming the system).

我该怎么办似乎正在工作。问题是,似乎我需要单击2个链接,然后才能执行从CART进行删除操作(或刷新页面)。

What I have seems to be working. The problem is that it seems I need to click 2 links before the REMOVE FROM CART action seems to be working (or refresh the page).

这是什么原因?

// -------------------------------------------
// 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' );

function remove_product_from_cart_programmatically() {
    if ( is_admin() ) return;
    $product_id = 339; // product id
    $cart_total = 199.99;
    $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 );
    }
}


推荐答案

您不应使用 template_redirect 挂钩根据购物车总阈值金额添加或删除免费产品……而且您的代码有些过时了。

You should not use template_redirect hook to add or remove a free product based on a cart total threshold amount… Also your code is a bit outdated with some mistakes.

请使用启用了Ajax的 woocommerce_before_calculate_totals 挂钩,方法是:

Instead use woocommerce_before_calculate_totals hook that is Ajax enabled, this way:

add_action( 'woocommerce_before_calculate_totals', 'add_or_remove_cart_items', 10, 1 );
function add_or_remove_cart_items( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // ONLY for logged users (and avoiding the hook repetition) 
    if ( ! is_user_logged_in() && did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $threshold_amount = 200; // The threshold amount for cart total
    $free_product_id  = 339; // ID of the free product
    $cart_items_total = 0; // Initializing

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ){
        // Check if the free product is in cart
        if ( $cart_item['data']->get_id() == $free_product_id ) {
            $free_item_key = $cart_item_key;
        }
        // Get cart subtotal incl. tax from items (with discounts if any)
        $cart_items_total += $cart_item['line_total'] + $cart_item['line_tax'];
    }

    // If Cart total is up to the defined amount and if the free products is not in cart, we add it.
    if ( $cart_items_total >= $threshold_amount && ! isset($free_item_key) ) {
        $cart->add_to_cart( $free_product_id );
    }
    // If cart total is below the defined amount and free product is in cart, we remove it.
    elseif ( $cart_items_total < $threshold_amount && isset($free_item_key) ) {
        $cart->remove_cart_item( $free_item_key );
    }
}

代码会在您活动的孩子的functions.php文件中显示主题(或活动主题)。经过测试,可以正常工作。

Code goes on functions.php file of your active child theme (or active theme). Tested and works.

相关:其他类似的答案线程

这篇关于根据WooCommerce购物车总额添加或删除特定购物车项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆