Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Add support for woocommerce_loop_add_to_cart_args filter in Products block #8422

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions src/BlockTypes/ProductButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,26 +89,53 @@ protected function render( $attributes, $content, $block ) {
$html_element = ( ! $product->has_options() && $product->is_purchasable() && $product->is_in_stock() && ! $cart_redirect_after_add ) ? 'button' : 'a';
$styles_and_classes = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes, array( 'border_radius', 'font_size', 'font_weight', 'margin', 'padding', 'text_color' ) );
$text_align_styles_and_classes = StyleAttributesUtils::get_text_align_class_and_style( $attributes );
$html_classes = implode(
' ',
array_filter(
array(
'wp-block-button__link',
'wc-block-components-product-button__button',
$product->is_purchasable() && ! $product->has_options() ? 'ajax_add_to_cart add_to_cart_button' : '',
'product_type_' . $product->get_type(),
$styles_and_classes['classes'],
)
)
);

$args = apply_filters(
'woocommerce_loop_add_to_cart_args',
array(
'class' => $html_classes,
'attributes' => array(
'data-product_id' => $product->get_id(),
'data-product_sku' => $product->get_sku(),
'aria-label' => $product->add_to_cart_description(),
'rel' => 'nofollow',
),
),
$product
);

if ( isset( $args['attributes']['aria-label'] ) ) {
$args['attributes']['aria-label'] = wp_strip_all_tags( $args['attributes']['aria-label'] );
}
Comment on lines +119 to +121
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 💯


return apply_filters(
'woocommerce_loop_add_to_cart_link',
sprintf(
'<div class="wp-block-button wc-block-components-product-button wc-block-grid__product-add-to-cart %1$s">
<%2$s href="%3$s" rel="nofollow" data-product_id="%4$s" data-product_sku="%5$s" class="wp-block-button__link %6$s wc-block-components-product-button__button product_type_%7$s %8$s" style="%9$s">%10$s</%11$s>
<%2$s href="%3$s" class="%4$s" style="%5$s" %6$s>%7$s</%2$s>
</div>',
esc_attr( $text_align_styles_and_classes['class'] ?? '' ),
$html_element,
esc_url( $product->add_to_cart_url() ),
esc_attr( $product->get_id() ),
esc_attr( $product->get_sku() ),
$product->is_purchasable() && ! $product->has_options() ? 'ajax_add_to_cart add_to_cart_button' : '',
esc_attr( $product->get_type() ),
esc_attr( $styles_and_classes['classes'] ),
isset( $args['class'] ) ? esc_attr( $args['class'] ) : '',
esc_attr( $styles_and_classes['styles'] ),
esc_html( $product->add_to_cart_text() ),
$html_element
isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
esc_html( $product->add_to_cart_text() )
),
$product
$product,
$args
);
}
}
Expand Down