From a594ea5ef5bc55994b3b3737985428c650a4038b Mon Sep 17 00:00:00 2001 From: Luigi Teschio Date: Thu, 10 Feb 2022 10:26:09 +0100 Subject: [PATCH] Fix Mini Cart doesn't update data (https://github.com/woocommerce/woocommerce-blocks/pull/5741) * Remove api hydration for the Mini Cart Block woocommerce/woocommerce-blocks#5729 Remove api hydration for the Mini Cart Block * fix PHP error * send event when the button is clicked * fix import order --- .../providers/add-to-cart-form/form/submit/index.js | 2 ++ .../woocommerce-blocks/src/BlockTypes/AllProducts.php | 8 ++++++++ plugins/woocommerce-blocks/src/BlockTypes/MiniCart.php | 9 --------- .../woocommerce-blocks/src/BlockTypes/SingleProduct.php | 3 ++- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/plugins/woocommerce-blocks/assets/js/base/context/providers/add-to-cart-form/form/submit/index.js b/plugins/woocommerce-blocks/assets/js/base/context/providers/add-to-cart-form/form/submit/index.js index fad07f5546a..ef715eb7bae 100644 --- a/plugins/woocommerce-blocks/assets/js/base/context/providers/add-to-cart-form/form/submit/index.js +++ b/plugins/woocommerce-blocks/assets/js/base/context/providers/add-to-cart-form/form/submit/index.js @@ -5,6 +5,7 @@ import { __ } from '@wordpress/i18n'; import triggerFetch from '@wordpress/api-fetch'; import { useEffect, useCallback, useState } from '@wordpress/element'; import { decodeEntities } from '@wordpress/html-entities'; +import { triggerAddedToCartEvent } from '@woocommerce/base-utils'; /** * Internal dependencies @@ -107,6 +108,7 @@ const FormSubmit = () => { } else { receiveCart( response ); } + triggerAddedToCartEvent( { preserveCartData: true } ); dispatchActions.setAfterProcessing( response ); setIsSubmitting( false ); } ); diff --git a/plugins/woocommerce-blocks/src/BlockTypes/AllProducts.php b/plugins/woocommerce-blocks/src/BlockTypes/AllProducts.php index ea6bef80fb7..167c5288166 100644 --- a/plugins/woocommerce-blocks/src/BlockTypes/AllProducts.php +++ b/plugins/woocommerce-blocks/src/BlockTypes/AllProducts.php @@ -27,6 +27,14 @@ class AllProducts extends AbstractBlock { $this->asset_data_registry->add( 'min_rows', wc_get_theme_support( 'product_blocks::min_rows', 1 ), true ); $this->asset_data_registry->add( 'max_rows', wc_get_theme_support( 'product_blocks::max_rows', 6 ), true ); $this->asset_data_registry->add( 'default_rows', wc_get_theme_support( 'product_blocks::default_rows', 3 ), true ); + $this->hydrate_from_api(); + } + + /** + * Hydrate the All Product block with data from the API. + */ + protected function hydrate_from_api() { + $this->asset_data_registry->hydrate_api_request( '/wc/store/cart' ); } /** diff --git a/plugins/woocommerce-blocks/src/BlockTypes/MiniCart.php b/plugins/woocommerce-blocks/src/BlockTypes/MiniCart.php index aa289d9043d..cbde8272e1a 100644 --- a/plugins/woocommerce-blocks/src/BlockTypes/MiniCart.php +++ b/plugins/woocommerce-blocks/src/BlockTypes/MiniCart.php @@ -107,8 +107,6 @@ class MiniCart extends AbstractBlock { // Hydrate the following data depending on admin or frontend context. if ( ! is_admin() && ! WC()->is_rest_api_request() ) { - $this->hydrate_from_api(); - $label_info = $this->get_tax_label(); $this->tax_label = $label_info['tax_label']; @@ -209,13 +207,6 @@ class MiniCart extends AbstractBlock { do_action( 'woocommerce_blocks_cart_enqueue_data' ); } - /** - * Hydrate the cart block with data from the API. - */ - protected function hydrate_from_api() { - $this->asset_data_registry->hydrate_api_request( '/wc/store/cart' ); - } - /** * Returns the script data given its handle. * diff --git a/plugins/woocommerce-blocks/src/BlockTypes/SingleProduct.php b/plugins/woocommerce-blocks/src/BlockTypes/SingleProduct.php index 4dcadd6cea3..0265a810e85 100644 --- a/plugins/woocommerce-blocks/src/BlockTypes/SingleProduct.php +++ b/plugins/woocommerce-blocks/src/BlockTypes/SingleProduct.php @@ -39,11 +39,12 @@ class SingleProduct extends AbstractBlock { } /** - * Hydrate the cart block with data from the API. + * Hydrate the Single Product block with data from the API. * * @param int $product_id ID of the product. */ protected function hydrate_from_api( int $product_id ) { $this->asset_data_registry->hydrate_api_request( "/wc/store/products/$product_id" ); + $this->asset_data_registry->hydrate_api_request( '/wc/store/cart' ); } }