Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Add to cart button ignored redirect to cart setting #5708

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions assets/js/atomic/blocks/product-elements/button/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
};
}

Expand Down
14 changes: 14 additions & 0 deletions src/Assets/AssetDataRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ),
Expand Down Expand Up @@ -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.
*
Expand Down