How to Show Ajax Add To Cart Button On Custom Product Loop

if you want to add custom add to cart button in your shop page or custom loop you can use below code.

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

Last Update on:March 5th, 2023 at 5:19 pm

How to Add New Tab to My Account Page in WooCommerce
Previous post