if you want to add custom add to cart button in your shop page or custom loop you can use below code.
<?php
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(
'button',
'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