0

I am trying to remove Add to cart button from products under "car_tickets" category when any product from "bus_tickets" category is not in cart. This is my code so far.

add_action('woocommerce_single_product_summary','check_if_bus_in_cart');
function check_if_bus_in_cart() {

// Set $cat_in_cart to false
$cat_in_cart = false;

// Loop through all products in the Cart        
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

    // If Cart has category "download", set $cat_in_cart to true
    if ( !has_term( 'bus_tickets', 'product_cat', $cart_item['product_id'] ) ) {
        $cat_in_cart = true;
        break;
    }
}

// Do something if category "download" is in the Cart      
if ( $cat_in_cart ) {

//function for deleting ....
function remove_car_add_cart_button(){
    global $product;

    // Set HERE your category ID, slug or name (or an array)
    $category = 'car_tickets';

    //Remove Add to Cart button from product description of product with id 1234    
    if ( has_term( $category, 'product_cat', $product->id ) )
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );

}


}

}

The main problem is the code is not working and I cant find the error ..

0

Finally, I solved my problem. These are the steps:

  1. Disable Add to Cart button on "car_tickets" category initially.

Heres the code for this :

/*--Disable Add to cart button initially for "car_tickets" category ----*/
add_action('wp','disable_cars_initially');  

function disable_cars_initially() {
global $product;

// Set HERE your category ID, slug or name (or an array)
$category = 'car_tickets';

//Remove Add to Cart button from product description of product with any cat    
if ( has_term( $category, 'product_cat', $product->id ) )
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );

}
  1. Enable Add to cart button on "car_tickets" category if any product from "bus_tickets" category is in cart already!

    /* -----  Enable add to cart on  category "car_tickets"  if 
    "bus_tickets" 
    already exists in cart  ------- */
    
    add_action('wp','only_add_car_in_cart');  
    function only_add_car_in_cart() {
    
    
     // Loop through all products in the Cart        
     foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    
    // False initially
    $car_in_cart = false;
    
        // If Cart has category "Bus Tickets", set $cat_in_cart to true
        if ( has_term( 'bus_tickets', 'product_cat', $cart_item['product_id'] ) ) {
            $cat_in_cart = true;
            break;
        } 
    
    }
    
    // Enable Cars if "Bus Tickets" is in the Cart      
    if ( $cat_in_cart) {
    
    global $product;
    
    // Set HERE your category ID, slug or name (or an array)
    $category = 'car_tickets';
    
    //Enambe Add to Cart button from product description of product with cat  
    if ( has_term( $category, 'product_cat', $product->id ) )
    add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    
    }
    
    
    }
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.