有条件地添加或删除购物车中的免费产品


Conditionally adding or removing a free product in cart

我在woocommerce中有一个功能,可以自动向购物车添加免费礼物。

我想在最小数量15的基础上加上这个

我有这个代码工作,但当我更新购物车不删除项目,如果数量不是15。

如果总id不同于15,我如何在更新购物车时自动删除产品?

}
//add_action( 'init', 'wcsg_add_product_to_cart' );
add_action( 'wp_loaded', 'wcsg_add_product_to_cart', 99 );
function wcsg_add_product_to_cart() {
if ( ! is_admin() ) {
    global $woocommerce;
            //calcoliamo quanti prodotti ci sono nel carrello
            $totalecarrello = $woocommerce->cart->cart_contents_count;
            echo "<script type='text/javascript'>alert('$totalecarrello');</script>";
    $cart = WC()->cart->get_cart();
    $wcsgProduct = '0';
    $wcsgProduct = get_option('wcsgProduct');
    $product_id = $wcsgProduct;
    if ($product_id== '0' || $product_id == NULL) {
        // do nothing
    } else {
        $found = false;
        if ( sizeof( $cart ) > 0 ) {
            foreach ( $cart as $cart_item_key => $values ) {
                $_product = $values['data'];
                if ( $_product->id == $product_id )
                    $found = true;
            }
                            //controlliamo quanti prodotti ci sono
            if ( ! $found && $totalecarrello == 15 )    
                                //se sono 15 aggiungiamo il prodotto free
                WC()->cart->add_to_cart( $product_id );
             $message = $woocommerce->cart->cart_contents_count;
echo "<script type='text/javascript'>alert('$message');</script>";
$totalecarrello = $woocommerce->cart->cart_contents_count;
//altrimenti no 
        } else {
            //WC()->cart->add_to_cart( $product_id );
            WC()->cart->remove_cart_item($product_id);
        }
    }
}
}

Thanks for help

更新-添加Woocommerce 3兼容性

最好使用 woocommerce_before_calculate_totals 专用的WooCommerce钩子,因为它处理客户可以在购物车页面上做的所有更改(删除项目,更新数量)。

这是代码:

add_action( 'woocommerce_before_calculate_totals', 'wcsg_adding_promotional_product', 10, 1 );
function wcsg_adding_promotional_product( $cart ) {
    if (is_admin() && !defined('DOING_AJAX'))
        return;
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;
    if( ! is_cart() ) return; // Only cart
    $promo_id = get_option('wcsgProduct'); // Getting the ID of your promotional product
    $targeted_cart_items = 15; // <=== Set HERE the targeted number of items in cart
    $cart_count = $cart_object->cart_contents_count; // Items in cart
    $has_promo = false;
    if ( ! $cart->is_empty() && is_cart() && ! empty( $promo_id ) ){
        // compatibility with WC +3
        $cart_items = version_compare( WC_VERSION, '3.0', '<' ) ? $cart->cart_contents : $cart->get_cart();
        // Iterating through each item in cart
        foreach ( $cart_items as $cart_item_key => $cart_item ){
            // If Promo product is in cart
            if( $cart_item['product_id'] == $promo_id || $cart_item['variation_id'] == $promo_id ) {
                $has_promo = true;
                $promo_key= $cart_item_key;
            }
        }
        //If Promo product is NOT in cart and targeted item count is reached, we add it.
        if( ! $has_promo && $cart_count >= $targeted_cart_items )
            $cart->add_to_cart( $promo_id );
        // If Promo product is in cart and targeted item count NOT reached, we remove it.
        if( $has_promo && $cart_count <= $targeted_cart_items )
            $cart->remove_cart_item( $promo_key );
    }
}

代码在您的活动子主题(或主题)的function.php文件或任何插件文件中。

此代码经过测试并正常运行。


基于:当购物车达到一定金额时添加促销产品

相关主题:自动添加或删除购物车中的免费产品