From 11bcafd72944136c37fdde1ac9021f468fb1f251 Mon Sep 17 00:00:00 2001 From: Tomasz Tunik Date: Wed, 2 Feb 2022 18:02:47 +0100 Subject: [PATCH 1/2] Expose products settings in wcSettings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For the time being we expose only what is used by the blocks which is `cartRedirectAfterAdd`. In the future more can be added as needed. Setting is accessible via `getSetting( 'productsSettings' )`. We namespace the settings under productsSettigns to reflect the domain and how settings are organised in Woo admin and to inform that this is an object with more settings within. This setting normally was available **only** if AJAX add to cart was set as a js global `wc_add_to_cart_params.cart_redirect_after_add`. By accessing the option directly we ensure it’s exposed to blocks regardless of if AJAX option is enabled. --- src/Assets/AssetDataRegistry.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Assets/AssetDataRegistry.php b/src/Assets/AssetDataRegistry.php index cf622971873..88bffa26b7e 100644 --- a/src/Assets/AssetDataRegistry.php +++ b/src/Assets/AssetDataRegistry.php @@ -89,6 +89,7 @@ protected function get_core_data() { 'locale' => $this->get_locale_data(), 'orderStatuses' => $this->get_order_statuses(), 'placeholderImgSrc' => wc_placeholder_img_src(), + 'productsSettings' => $this->get_products_settings(), 'siteTitle' => get_bloginfo( 'name' ), 'storePages' => $this->get_store_pages(), 'wcAssetUrl' => plugins_url( 'assets/', WC_PLUGIN_FILE ), @@ -151,6 +152,19 @@ protected function get_store_pages() { ); } + /** + * Get product related settings. + * + * Note: For the time being we are exposing only the settings that are used by blocks. + * + * @return array + */ + protected function get_products_settings() { + return [ + 'cartRedirectAfterAdd' => get_option( 'woocommerce_cart_redirect_after_add' ) === 'yes', + ]; + } + /** * Format a page object into a standard array of data. * From 9ae7faef28ddb4bfef67a4404db056e6164395a1 Mon Sep 17 00:00:00 2001 From: Tomasz Tunik Date: Wed, 2 Feb 2022 18:03:59 +0100 Subject: [PATCH 2/2] update AddToCartButton to respect cartRedirectAfterAdd MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This adds the redirect directly on the AddToCartButton after succesful add to cart action. This follows convention that redirects or other side effects shouldn’t happen as part of the action but rather be part of the control that triggers such flow. --- assets/js/atomic/blocks/product-elements/button/block.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/assets/js/atomic/blocks/product-elements/button/block.js b/assets/js/atomic/blocks/product-elements/button/block.js index f8142a04854..dfdb7482358 100644 --- a/assets/js/atomic/blocks/product-elements/button/block.js +++ b/assets/js/atomic/blocks/product-elements/button/block.js @@ -9,6 +9,8 @@ import { useStoreAddToCart, } from '@woocommerce/base-context/hooks'; import { decodeEntities } from '@wordpress/html-entities'; +import { CART_URL } from '@woocommerce/block-settings'; +import { getSetting } from '@woocommerce/settings'; import { useInnerBlockLayoutContext, useProductDataContext, @@ -101,6 +103,12 @@ const AddToCartButton = ( { product } ) => { dispatchStoreEvent( 'cart-add-item', { product, } ); + // redirect to cart if the setting to redirect to the cart page + // on cart add item is enabled + const { cartRedirectAfterAdd } = getSetting( 'productsSettings' ); + if ( cartRedirectAfterAdd ) { + window.location.href = CART_URL; + } }; }