woocommerce_template_loop_add_to_cart()
Get the add to cart template for the loop.
Хуки из функции
Возвращает
null. Ничего (null).
Использование
woocommerce_template_loop_add_to_cart( $args );
- $args(массив)
- Arguments.
По умолчанию:array()
Код woocommerce_template_loop_add_to_cart() woocommerce template loop add to cart WC 10.5.2
function woocommerce_template_loop_add_to_cart( $args = array() ) {
global $product;
if ( ! ( $product instanceof WC_Product ) ) {
return;
}
$defaults = array(
'quantity' => 1,
'class' => implode(
' ',
array_filter(
array(
'button',
wc_wp_theme_get_element_class_name( 'button' ), // escaped in the template.
'product_type_' . $product->get_type(),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
$product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '',
)
)
),
'aria-describedby_text' => $product->add_to_cart_aria_describedby(),
'attributes' => array(
'data-product_id' => $product->get_id(),
'data-product_sku' => $product->get_sku(),
'aria-label' => $product->add_to_cart_description(),
'rel' => 'nofollow',
),
);
if ( is_a( $product, 'WC_Product_Simple' ) ) {
$defaults['attributes']['data-success_message'] = $product->add_to_cart_success_message();
}
/**
* Filter to customize the arguments for the add to cart template for the loop.
*
* @param array $args Arguments.
*
* @since 2.4.11
*/
$args = apply_filters( 'woocommerce_loop_add_to_cart_args', wp_parse_args( $args, $defaults ), $product );
if ( ! empty( $args['attributes']['aria-describedby'] ) ) {
$args['attributes']['aria-describedby'] = wp_strip_all_tags( $args['attributes']['aria-describedby'] );
}
if ( isset( $args['attributes']['aria-label'] ) ) {
$args['attributes']['aria-label'] = wp_strip_all_tags( $args['attributes']['aria-label'] );
}
$cart_redirect_after_add = get_option( 'woocommerce_cart_redirect_after_add' ) === 'yes';
$ajax_add_to_cart_enabled = get_option( 'woocommerce_enable_ajax_add_to_cart' ) === 'yes';
// The template is using an anchor instead of a button. For AJAX
// add-to-cart, it needs to be a button for accessibility reasons.
// See https://github.com/woocommerce/woocommerce/issues/59382.
if (
! $cart_redirect_after_add &&
$ajax_add_to_cart_enabled &&
$product->supports( 'ajax_add_to_cart' ) &&
$product->is_purchasable() &&
$product->is_in_stock()
) {
$args['attributes']['role'] = 'button';
}
wc_get_template( 'loop/add-to-cart.php', $args );
}