Add to cart button ignored redirect to cart setting (https://github.com/woocommerce/woocommerce-blocks/pull/5708)
* Expose products settings in wcSettings 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. * update AddToCartButton to respect cartRedirectAfterAdd 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.
This commit is contained in:
parent
4156e0f532
commit
ee6d1b5ae3
|
@ -9,6 +9,8 @@ import {
|
||||||
useStoreAddToCart,
|
useStoreAddToCart,
|
||||||
} from '@woocommerce/base-context/hooks';
|
} from '@woocommerce/base-context/hooks';
|
||||||
import { decodeEntities } from '@wordpress/html-entities';
|
import { decodeEntities } from '@wordpress/html-entities';
|
||||||
|
import { CART_URL } from '@woocommerce/block-settings';
|
||||||
|
import { getSetting } from '@woocommerce/settings';
|
||||||
import {
|
import {
|
||||||
useInnerBlockLayoutContext,
|
useInnerBlockLayoutContext,
|
||||||
useProductDataContext,
|
useProductDataContext,
|
||||||
|
@ -101,6 +103,12 @@ const AddToCartButton = ( { product } ) => {
|
||||||
dispatchStoreEvent( 'cart-add-item', {
|
dispatchStoreEvent( 'cart-add-item', {
|
||||||
product,
|
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;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -89,6 +89,7 @@ class AssetDataRegistry {
|
||||||
'locale' => $this->get_locale_data(),
|
'locale' => $this->get_locale_data(),
|
||||||
'orderStatuses' => $this->get_order_statuses(),
|
'orderStatuses' => $this->get_order_statuses(),
|
||||||
'placeholderImgSrc' => wc_placeholder_img_src(),
|
'placeholderImgSrc' => wc_placeholder_img_src(),
|
||||||
|
'productsSettings' => $this->get_products_settings(),
|
||||||
'siteTitle' => get_bloginfo( 'name' ),
|
'siteTitle' => get_bloginfo( 'name' ),
|
||||||
'storePages' => $this->get_store_pages(),
|
'storePages' => $this->get_store_pages(),
|
||||||
'wcAssetUrl' => plugins_url( 'assets/', WC_PLUGIN_FILE ),
|
'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.
|
* Format a page object into a standard array of data.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue