Conditionally adding or removing a free product in cart

青春壹個敷衍的年華 提交于 2021-02-05 08:47:09

问题


I've a function in woocommerce that add automatically to cart a free gift.

I would like to add this based on minimum quantity of 15

i've this code that works, but when i update the cart don't remove the item if the quantity is not 15.

How I can automatic remove product when I update the cart if total id different to 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


回答1:


Updated - Added Woocommerce 3 compatibility

Is better to use woocommerce_before_calculate_totals dedicated WooCommerce hook as it handle all changes that customers can do on cart page (removing items, updating quantities).

This is code:

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

    }
}

Code goes on function.php file of your active child theme (or theme) or in any plugin file.

This codes tested and works.


Based on: Adding a promotional product when a certain cart amount is reached

Related thread: Auto add or remove a freebie product from cart in Woocommerce



来源:https://stackoverflow.com/questions/40483268/conditionally-adding-or-removing-a-free-product-in-cart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!