Lipsa
Lipsa

Reputation: 417

certain product are single product in cart woocommerce

Certain products when have tag value "buynow" or categories by featue products added to cart I want to remove all other product in cart and dont allow to add more product in cart. When one user added one product of this type and tries to add another product in cart, It will show error notice.

I have tried this code for product id 1024

add_filter( 'woocommerce_add_cart_item_data', 'wdm_empty_cart', 10, 3);
function wdm_empty_cart( $cart_item_data, $product_id, $variation_id )
{
    global $woocommerce;
    if($product_id == '1024'){
        $woocommerce->cart->empty_cart();
        wc_add_notice( 'Error!', 'error' );

    }
    elseif ( WC()->cart->get_cart_contents_count() >=1 ){
        $items = $woocommerce->cart->get_cart();
            foreach($items as $item => $values) { 
                $_product = $values['data']->post; 
                $cartproductid[] = $_product->ID;
            }
            foreach($cartproductid as $id){
                if($id == 1024){
                    wc_add_notice( 'Error!', 'error' );
                    $woocommerce->cart->empty_cart();
                    $woocommerce->cart->add_to_cart('1024');

                }

            }

     }
     return $cart_item_data;
}

My code works as follows:

When user add 1024 product id to the cart it empty the cart and add only the 1024 product. when user add product after adding 1024 not the same product it adds one more to the cart the stops adding further product.

My requirement is when 1024 product added to the cart no other product can not add to the cart. When user try to add it shows the notice "Error!"

Any solution is appreciated. Thanks in advance. Anyone's help appreciated.

Upvotes: 2

Views: 640

Answers (1)

Scarecrow
Scarecrow

Reputation: 4137

Instead of woocommerce_add_cart_item_data you can use woocommerce_add_to_cart_validation. this works.

add_filter( 'woocommerce_add_to_cart_validation', 'woocommerce_add_to_cart_validation_CB', 30, 3);
function woocommerce_add_to_cart_validation_CB( $bool, $product_id, $quantity  )
{
    global $woocommerce;
    if($product_id == '1024'){
        $woocommerce->cart->empty_cart();
    }
    elseif ( WC()->cart->get_cart_contents_count() >= 1 ){
        $items = $woocommerce->cart->get_cart();
        foreach($items as $item => $values) {
            if( '1024' == $values['product_id'] ){
                wc_add_notice( 'Error!', 'error' );
                return false;
            }
        }
     }
     return $bool;
}

If you want do this for featured product then use following code.

add_filter( 'woocommerce_add_to_cart_validation', 'woocommerce_add_to_cart_validation_CB', 30, 3);
function woocommerce_add_to_cart_validation_CB( $bool, $product_id, $quantity  )
{

    global $woocommerce;
    if( isFeaturedProduct($product_id)){
        $woocommerce->cart->empty_cart();
    }
    elseif ( WC()->cart->get_cart_contents_count() >= 1 ){
        $items = $woocommerce->cart->get_cart();
        foreach($items as $item => $values) {
            if(isFeaturedProduct($values['product_id'])   ){
                wc_add_notice( 'Error!', 'error' );
                return false;
            }
        }
     }
     return $bool;
}

function isFeaturedProduct( $product_id ){
    $wc_product_obj = new WC_Product($product_id);
    return $wc_product_obj->is_featured();
}

Upvotes: 3

Related Questions