Automattic\WooCommerce\Blocks\BlockTypes
AbstractProductGrid::get_add_to_cart
Get the "add to cart" button.
Method of the class: AbstractProductGrid{}
Hooks from the method
Returns
String. Rendered product output.
Usage
// protected - for code of main (parent) or child class $result = $this->get_add_to_cart( $product );
- $product(WC_Product) (required)
- Product.
AbstractProductGrid::get_add_to_cart() AbstractProductGrid::get add to cart code WC 10.3.3
protected function get_add_to_cart( $product ) {
$attributes = array(
'aria-label' => $product->add_to_cart_description(),
'data-quantity' => '1',
'data-product_id' => $product->get_id(),
'data-product_sku' => $product->get_sku(),
'data-price' => wc_get_price_to_display( $product ),
'rel' => 'nofollow',
'class' => 'wp-block-button__link ' . ( function_exists( 'wc_wp_theme_get_element_class_name' ) ? wc_wp_theme_get_element_class_name( 'button' ) : '' ) . ' add_to_cart_button',
);
if (
$product->supports( 'ajax_add_to_cart' ) &&
$product->is_purchasable() &&
( $product->is_in_stock() || $product->backorders_allowed() )
) {
$attributes['class'] .= ' ajax_add_to_cart';
}
/**
* Filter to manipulate (add/modify/remove) attributes in the HTML code of the generated add to cart button.
*
* @since 8.6.0
*
* @param array $attributes An associative array containing default HTML attributes of the add to cart button.
* @param WC_Product $product The WC_Product instance of the product that will be added to the cart once the button is pressed.
*
* @return array Returns an associative array derived from the default array passed as an argument and added the extra HTML attributes.
*/
$attributes = apply_filters( 'woocommerce_blocks_product_grid_add_to_cart_attributes', $attributes, $product );
return sprintf(
'<a href="%s" %s>%s</a>',
esc_url( $product->add_to_cart_url() ),
wc_implode_html_attributes( $attributes ),
esc_html( $product->add_to_cart_text() )
);
}