diff --git a/plugins/woocommerce-blocks/assets/js/atomic/blocks/product-elements/button/block.js b/plugins/woocommerce-blocks/assets/js/atomic/blocks/product-elements/button/block.js index f8142a04854..dfdb7482358 100644 --- a/plugins/woocommerce-blocks/assets/js/atomic/blocks/product-elements/button/block.js +++ b/plugins/woocommerce-blocks/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; + } }; } diff --git a/plugins/woocommerce-blocks/src/Assets/AssetDataRegistry.php b/plugins/woocommerce-blocks/src/Assets/AssetDataRegistry.php index cf622971873..88bffa26b7e 100644 --- a/plugins/woocommerce-blocks/src/Assets/AssetDataRegistry.php +++ b/plugins/woocommerce-blocks/src/Assets/AssetDataRegistry.php @@ -89,6 +89,7 @@ class AssetDataRegistry { '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 @@ class AssetDataRegistry { ); } + /** + * 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. *