有条不紊地在购物车中添加或删除免费商品

时间:2016-11-08 09:29:08

标签: php wordpress woocommerce cart product

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

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

更新 - 添加了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文件中显示。

此代码经过测试和运作。

基于:Adding a promotional product when a certain cart amount is reached

相关主题:Auto add or remove a freebie product from cart in Woocommerce