Cark Blen
Cark Blen

Reputation: 55

Disable the ajax add to cart button woocommerce in specific categories

In WooCommerce, I need to disable the AJAX of the add to cart button, but only in "category x", for the redirection to work and go direct to the checkout: https://jeroensormani.com/redirect-users-after-add-to-cart/

I tried removing the class but it didn't work:

$(document).ready(function(){
    $(".add_to_cart_button").removeClass("ajax_add_to_cart")
});

How do I add a simple jQuery script to WordPress?

Upvotes: 0

Views: 1754

Answers (1)

Cark Blen
Cark Blen

Reputation: 55

Solved:

add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button', 10, 2 );
function replace_default_button($button, $product){
    //list category slugs where button needs to be changed
    $selected_cats = array('my-category');
    //get current category object
    $current_cat = get_queried_object();
    //get category slug from category object
    $current_cat_slug = $current_cat->slug;
    //check if current category slug is in the selected category list
    if( in_array($current_cat_slug, $selected_cats) ){
        //replace default button code with custom code
        $button = '<div class="add-to-cart-button"><a href="?add-to-cart='.$product->get_id().'" data-quantity="1" class="primary is-small mb-0 button product_type_simple add_to_cart_button is-flat" data-product_id="'.$product->get_id().'" data-product_sku="" aria-label="Add to cart" rel="nofollow">Buy now</a></div>';
    }
    return $button;
}

Upvotes: 2

Related Questions