2021-06-07 09:16:47 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { dispatch } from '@wordpress/data';
|
2022-12-23 11:59:02 +00:00
|
|
|
import { CartResponse, ExtensionCartUpdateArgs } from '@woocommerce/types';
|
2024-08-14 14:24:44 +00:00
|
|
|
import { processErrorResponse } from '@woocommerce/block-data';
|
2021-06-07 09:16:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { STORE_KEY } from '../../../assets/js/data/cart/constants';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When executed, this will call the cart/extensions endpoint.
|
|
|
|
* The args contains a namespace, so if that extension has registered an update
|
|
|
|
* callback, it will be executed server-side and the new cart will be returned.
|
|
|
|
* The new cart is then received into the client-side store.
|
|
|
|
*/
|
|
|
|
export const extensionCartUpdate = (
|
|
|
|
args: ExtensionCartUpdateArgs
|
|
|
|
): Promise< CartResponse > => {
|
|
|
|
const { applyExtensionCartUpdate } = dispatch( STORE_KEY );
|
2024-08-14 14:24:44 +00:00
|
|
|
return applyExtensionCartUpdate( args ).catch( ( error ) => {
|
|
|
|
if ( error?.code === 'woocommerce_rest_cart_extensions_error' ) {
|
|
|
|
processErrorResponse( error );
|
|
|
|
}
|
|
|
|
return Promise.reject( error );
|
|
|
|
} );
|
2021-06-07 09:16:47 +00:00
|
|
|
};
|