Show Ajax Add To Cart Button On Custom Product Loop

Share Now

Share on facebook
Share on pinterest
Share on twitter
Share on linkedin
Share on email

1. Enable This ‘Enable AJAX add to cart buttons on archives’ from WooCommerce Settings

2. Then Add this code where you want to show “Add To Cart” Button

echo sprintf('<a href="%s" data-quantity="1" class="%s" %s>%s</a>', esc_url($product->add_to_cart_url()), esc_attr(implode(' ', array_filter(array(
    'product_type_' . $product->get_type(),
    $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
    $product->supports('ajax_add_to_cart') ? 'ajax_add_to_cart' : ''
)))), wc_implode_html_attributes(array(
    'data-product_id' => $product->get_id(),
    'data-product_sku' => $product->get_sku(),
    'aria-label' => $product->add_to_cart_description(),
    'rel' => 'nofollow'
)), esc_html($product->add_to_cart_text()));
Nahid Mahamud

Nahid Mahamud

Web Developer | Graphic Designer | WordPress & Woo-commerce Expert