Add to Cart Form block: Prevent notice from appearing after clicking on Add to Cart button inside Single Product block (https://github.com/woocommerce/woocommerce-blocks/pull/9685)
* Add isDescendentOfSingleProductBlock attribute to Add to Cart Form block * Prevent notice from appearing when Add to Cart button is clicked When the Add to Cart Form is added inside the Single Product Block, we have to prevent the notice from appearing when the Add to Cart button is clicked. * Fix PHP CS errors * Fix PHP CS errors * Fix PHP CS errors * Fix PHP CS errors * Fix PHP CS errors * Fix PHP CS errors * Add phpcs:ignore WordPress.Security.NonceVerification.Recommended * Fix Processing form data without nonce verification. * PHP Ignore * Improve doc comments * Improve name for add_to_cart_message_html_filter hook * Replace equal operator with identity operator in the conditional statement
This commit is contained in:
parent
d85a6ecbd2
commit
5f307e1097
|
@ -3,6 +3,12 @@
|
|||
"version": "1.0.0",
|
||||
"title": "Add to Cart with Options",
|
||||
"description": "Display a button so the customer can add a product to their cart. Options will also be displayed depending on product type. e.g. quantity, variation.",
|
||||
"attributes": {
|
||||
"isDescendentOfSingleProductBlock": {
|
||||
"type": "boolean",
|
||||
"default": false
|
||||
}
|
||||
},
|
||||
"category": "woocommerce",
|
||||
"keywords": [ "WooCommerce" ],
|
||||
"usesContext": ["postId"],
|
||||
|
|
|
@ -1,23 +1,38 @@
|
|||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { useEffect } from '@wordpress/element';
|
||||
import { useBlockProps } from '@wordpress/block-editor';
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { Button, Disabled, Tooltip } from '@wordpress/components';
|
||||
import { Skeleton } from '@woocommerce/base-components/skeleton';
|
||||
import { BlockEditProps } from '@wordpress/blocks';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import './editor.scss';
|
||||
import { useIsDescendentOfSingleProductBlock } from '../shared/use-is-descendent-of-single-product-block';
|
||||
export interface Attributes {
|
||||
className?: string;
|
||||
isDescendentOfSingleProductBlock: boolean;
|
||||
}
|
||||
|
||||
const Edit = () => {
|
||||
const Edit = ( props: BlockEditProps< Attributes > ) => {
|
||||
const { setAttributes } = props;
|
||||
const blockProps = useBlockProps( {
|
||||
className: 'wc-block-add-to-cart-form',
|
||||
} );
|
||||
const { isDescendentOfSingleProductBlock } =
|
||||
useIsDescendentOfSingleProductBlock( {
|
||||
blockClientId: blockProps?.id,
|
||||
} );
|
||||
|
||||
useEffect( () => {
|
||||
setAttributes( {
|
||||
isDescendentOfSingleProductBlock,
|
||||
} );
|
||||
}, [ setAttributes, isDescendentOfSingleProductBlock ] );
|
||||
|
||||
return (
|
||||
<div { ...blockProps }>
|
||||
|
|
|
@ -15,6 +15,33 @@ class AddToCartForm extends AbstractBlock {
|
|||
*/
|
||||
protected $block_name = 'add-to-cart-form';
|
||||
|
||||
/**
|
||||
* Initializes the AddToCartForm block and hooks into the `wc_add_to_cart_message_html` filter
|
||||
* to prevent displaying the Cart Notice when the block is inside the Single Product block
|
||||
* and the Add to Cart button is clicked.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function initialize() {
|
||||
parent::initialize();
|
||||
add_filter( 'wc_add_to_cart_message_html', array( $this, 'add_to_cart_message_html_filter' ), 10, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the block's attributes.
|
||||
*
|
||||
* @param array $attributes Block attributes. Default empty array.
|
||||
* @return array Block attributes merged with defaults.
|
||||
*/
|
||||
private function parse_attributes( $attributes ) {
|
||||
// These should match what's set in JS `registerBlockType`.
|
||||
$defaults = array(
|
||||
'isDescendentOfSingleProductBlock' => false,
|
||||
);
|
||||
|
||||
return wp_parse_args( $attributes, $defaults );
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the block.
|
||||
*
|
||||
|
@ -42,6 +69,7 @@ class AddToCartForm extends AbstractBlock {
|
|||
}
|
||||
|
||||
ob_start();
|
||||
|
||||
/**
|
||||
* Trigger the single product add to cart action for each product type.
|
||||
*
|
||||
|
@ -57,6 +85,10 @@ class AddToCartForm extends AbstractBlock {
|
|||
return '';
|
||||
}
|
||||
|
||||
$parsed_attributes = $this->parse_attributes( $attributes );
|
||||
$is_descendent_of_single_product_block = $parsed_attributes['isDescendentOfSingleProductBlock'];
|
||||
$product = $this->add_is_descendent_of_single_product_block_hidden_input_to_product_form( $product, $is_descendent_of_single_product_block );
|
||||
|
||||
$classname = $attributes['className'] ?? '';
|
||||
$classes_and_styles = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes );
|
||||
|
||||
|
@ -73,6 +105,45 @@ class AddToCartForm extends AbstractBlock {
|
|||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a hidden input to the Add to Cart form to indicate that it is a descendent of a Single Product block.
|
||||
*
|
||||
* @param string $product The Add to Cart Form HTML.
|
||||
* @param string $is_descendent_of_single_product_block Indicates if block is descendent of Single Product block.
|
||||
*
|
||||
* @return string The Add to Cart Form HTML with the hidden input.
|
||||
*/
|
||||
protected function add_is_descendent_of_single_product_block_hidden_input_to_product_form( $product, $is_descendent_of_single_product_block ) {
|
||||
|
||||
$hidden_is_descendent_of_single_product_block_input = sprintf(
|
||||
'<input type="hidden" name="is-descendent-of-single-product-block" value="%1$s">',
|
||||
$is_descendent_of_single_product_block ? 'true' : 'false'
|
||||
);
|
||||
$regex_pattern = '/<button\s+type="submit"[^>]*>.*?<\/button>/i';
|
||||
|
||||
preg_match( $regex_pattern, $product, $input_matches );
|
||||
|
||||
if ( ! empty( $input_matches ) ) {
|
||||
$product = preg_replace( $regex_pattern, $hidden_is_descendent_of_single_product_block_input . $input_matches[0], $product );
|
||||
}
|
||||
|
||||
return $product;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the add to cart message to prevent the Notice from being displayed when the Add to Cart form is a descendent of a Single Product block
|
||||
* and the Add to Cart button is clicked.
|
||||
*
|
||||
* @param string $message Message to be displayed when product is added to the cart.
|
||||
*/
|
||||
public function add_to_cart_message_html_filter( $message ) {
|
||||
// phpcs:ignore
|
||||
if ( isset( $_POST['is-descendent-of-single-product-block'] ) && 'true' === $_POST['is-descendent-of-single-product-block'] ) {
|
||||
return false;
|
||||
}
|
||||
return $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the frontend script handle for this block type.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue