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 1 commit
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
37 changes: 30 additions & 7 deletions src/BlockTypes/ProductButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,26 +89,49 @@ 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(),
'rel' => 'nofollow',
Aljullu marked this conversation as resolved.
Show resolved Hide resolved
),
),
$product
);

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</%8$s>
Aljullu marked this conversation as resolved.
Show resolved Hide resolved
</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'] ),
esc_attr( array_key_exists( 'class', $args ) ? $args['class'] : '' ),
Aljullu marked this conversation as resolved.
Show resolved Hide resolved
esc_attr( $styles_and_classes['styles'] ),
isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
esc_html( $product->add_to_cart_text() ),
$html_element
),
$product
$product,
$args
);
}
}
Expand Down