diff --git a/assets/js/base/context/providers/cart-checkout/checkout-state/constants.ts b/assets/js/base/context/providers/cart-checkout/checkout-state/constants.ts
index 79b91ad2b4d..d674c35f2b0 100644
--- a/assets/js/base/context/providers/cart-checkout/checkout-state/constants.ts
+++ b/assets/js/base/context/providers/cart-checkout/checkout-state/constants.ts
@@ -26,15 +26,15 @@ export enum STATUS {
AFTER_PROCESSING = 'after_processing',
}
-const preloadedApiRequests = getSetting( 'preloadedApiRequests', {} ) as Record<
+const preloadedCheckoutData = getSetting( 'checkoutData', {} ) as Record<
string,
- { body: Record< string, unknown > }
+ unknown
>;
const checkoutData = {
order_id: 0,
customer_id: 0,
- ...( preloadedApiRequests[ '/wc/store/checkout' ]?.body || {} ),
+ ...( preloadedCheckoutData || {} ),
};
export const DEFAULT_CHECKOUT_STATE_DATA: CheckoutStateContextType = {
diff --git a/assets/js/blocks/active-filters/frontend.js b/assets/js/blocks/active-filters/frontend.js
index 6a1d2705ae5..7b7e0b629f6 100644
--- a/assets/js/blocks/active-filters/frontend.js
+++ b/assets/js/blocks/active-filters/frontend.js
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
-import { withRestApiHydration } from '@woocommerce/block-hocs';
import { renderFrontend } from '@woocommerce/base-utils';
/**
@@ -21,6 +20,6 @@ const getProps = ( el ) => {
renderFrontend( {
selector: '.wp-block-woocommerce-active-filters',
- Block: withRestApiHydration( Block ),
+ Block,
getProps,
} );
diff --git a/assets/js/blocks/attribute-filter/frontend.js b/assets/js/blocks/attribute-filter/frontend.js
index 5eca0cc8b90..72478cbce28 100644
--- a/assets/js/blocks/attribute-filter/frontend.js
+++ b/assets/js/blocks/attribute-filter/frontend.js
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
-import { withRestApiHydration } from '@woocommerce/block-hocs';
import { renderFrontend } from '@woocommerce/base-utils';
/**
@@ -25,6 +24,6 @@ const getProps = ( el ) => {
renderFrontend( {
selector: '.wp-block-woocommerce-attribute-filter',
- Block: withRestApiHydration( Block ),
+ Block,
getProps,
} );
diff --git a/assets/js/blocks/cart-checkout/cart/frontend.js b/assets/js/blocks/cart-checkout/cart/frontend.js
index 5c203a92653..d81248b99ef 100644
--- a/assets/js/blocks/cart-checkout/cart/frontend.js
+++ b/assets/js/blocks/cart-checkout/cart/frontend.js
@@ -1,10 +1,6 @@
/**
* External dependencies
*/
-import {
- withStoreCartApiHydration,
- withRestApiHydration,
-} from '@woocommerce/block-hocs';
import { getValidBlockAttributes } from '@woocommerce/base-utils';
import { Children, cloneElement, isValidElement } from '@wordpress/element';
import { useStoreCart } from '@woocommerce/base-context';
@@ -45,7 +41,7 @@ const Wrapper = ( { children } ) => {
};
renderParentBlock( {
- Block: withStoreCartApiHydration( withRestApiHydration( Block ) ),
+ Block,
blockName,
selector: '.wp-block-woocommerce-cart',
getProps,
diff --git a/assets/js/blocks/cart-checkout/checkout/checkout-order-error/index.js b/assets/js/blocks/cart-checkout/checkout/checkout-order-error/index.js
index 30f34ed20eb..ccb2c293c97 100644
--- a/assets/js/blocks/cart-checkout/checkout/checkout-order-error/index.js
+++ b/assets/js/blocks/cart-checkout/checkout/checkout-order-error/index.js
@@ -27,6 +27,8 @@ const cartItemErrorCodes = [
GENERIC_CART_ITEM_ERROR,
];
+const preloadedCheckoutData = getSetting( 'checkoutData', {} );
+
/**
* When an order was not created for the checkout, for example, when an item
* was out of stock, this component will be shown instead of the checkout form.
@@ -35,11 +37,10 @@ const cartItemErrorCodes = [
* checkout block.
*/
const CheckoutOrderError = () => {
- const preloadedApiRequests = getSetting( 'preloadedApiRequests', {} );
const checkoutData = {
code: '',
message: '',
- ...( preloadedApiRequests[ '/wc/store/checkout' ]?.body || {} ),
+ ...( preloadedCheckoutData || {} ),
};
const errorData = {
diff --git a/assets/js/blocks/cart-checkout/checkout/frontend.tsx b/assets/js/blocks/cart-checkout/checkout/frontend.tsx
index 261da2a8ea4..a99959a04a5 100644
--- a/assets/js/blocks/cart-checkout/checkout/frontend.tsx
+++ b/assets/js/blocks/cart-checkout/checkout/frontend.tsx
@@ -9,10 +9,6 @@ import {
useValidation,
} from '@woocommerce/base-context/hooks';
import { getRegisteredBlockComponents } from '@woocommerce/blocks-registry';
-import {
- withStoreCartApiHydration,
- withRestApiHydration,
-} from '@woocommerce/block-hocs';
import { renderParentBlock } from '@woocommerce/atomic-utils';
/**
@@ -57,7 +53,7 @@ const Wrapper = ( {
};
renderParentBlock( {
- Block: withStoreCartApiHydration( withRestApiHydration( Block ) ),
+ Block,
blockName,
selector: '.wp-block-woocommerce-checkout',
getProps,
diff --git a/assets/js/blocks/cart-checkout/mini-cart/component-frontend.tsx b/assets/js/blocks/cart-checkout/mini-cart/component-frontend.tsx
index 3172ae3b1ef..13742be7d03 100644
--- a/assets/js/blocks/cart-checkout/mini-cart/component-frontend.tsx
+++ b/assets/js/blocks/cart-checkout/mini-cart/component-frontend.tsx
@@ -6,7 +6,6 @@ import { renderFrontend } from '@woocommerce/base-utils';
/**
* Internal dependencies
*/
-import withMiniCartConditionalHydration from './with-mini-cart-conditional-hydration';
import MiniCartBlock from './block';
import './style.scss';
@@ -28,7 +27,7 @@ const renderMiniCartFrontend = () => {
renderFrontend( {
selector: '.wc-block-mini-cart',
- Block: withMiniCartConditionalHydration( MiniCartBlock ),
+ Block: MiniCartBlock,
getProps: ( el: HTMLElement ) => ( {
isDataOutdated: el.dataset.isDataOutdated,
isInitiallyOpen: el.dataset.isInitiallyOpen === 'true',
diff --git a/assets/js/blocks/cart-checkout/mini-cart/with-mini-cart-conditional-hydration.tsx b/assets/js/blocks/cart-checkout/mini-cart/with-mini-cart-conditional-hydration.tsx
deleted file mode 100644
index b04e2053cda..00000000000
--- a/assets/js/blocks/cart-checkout/mini-cart/with-mini-cart-conditional-hydration.tsx
+++ /dev/null
@@ -1,35 +0,0 @@
-/**
- * External dependencies
- */
-import {
- withStoreCartApiHydration,
- withRestApiHydration,
-} from '@woocommerce/block-hocs';
-
-interface MiniCartBlockInterface {
- // Signals whether the cart data is outdated. That happens when
- // opening the mini cart after adding a product to the cart.
- isDataOutdated?: boolean;
- // Signals whether it should be open when the React component is loaded. For
- // example, when adding a product to the cart, the Mini Cart should open
- // when loaded, but when removing a product from the cart, it shouldn't.
- isInitiallyOpen?: boolean;
-}
-
-// Custom HOC to conditionally hydrate API data depending on the isDataOutdated
-// prop.
-export default (
- OriginalComponent: ( component: MiniCartBlockInterface ) => JSX.Element
-) => {
- return ( {
- isDataOutdated,
- ...props
- }: MiniCartBlockInterface ): JSX.Element => {
- const Component = isDataOutdated
- ? OriginalComponent
- : withStoreCartApiHydration(
- withRestApiHydration( OriginalComponent )
- );
- return
This is a simple product.<\/p>", - "short_description": "
This is a simple product.<\/p>", - "description": "
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.<\/p>", - "sku": "woo-beanie", - "low_stock_remaining": null, - "backorders_allowed": false, - "show_backorder_badge": false, - "sold_individually": false, - "permalink": "https:\/\/local.wordpress.test\/product\/beanie\/", - "images": [ - { - "id": 44, - "src": "https:\/\/local.wordpress.test\/wp-content\/uploads\/2020\/03\/beanie-2.jpg", - "thumbnail": "https:\/\/local.wordpress.test\/wp-content\/uploads\/2020\/03\/beanie-2-324x324.jpg", - "srcset": "https:\/\/local.wordpress.test\/wp-content\/uploads\/2020\/03\/beanie-2.jpg 801w, https:\/\/local.wordpress.test\/wp-content\/uploads\/2020\/03\/beanie-2-324x324.jpg 324w, https:\/\/local.wordpress.test\/wp-content\/uploads\/2020\/03\/beanie-2-100x100.jpg 100w, https:\/\/local.wordpress.test\/wp-content\/uploads\/2020\/03\/beanie-2-416x416.jpg 416w, https:\/\/local.wordpress.test\/wp-content\/uploads\/2020\/03\/beanie-2-300x300.jpg 300w, https:\/\/local.wordpress.test\/wp-content\/uploads\/2020\/03\/beanie-2-150x150.jpg 150w, https:\/\/local.wordpress.test\/wp-content\/uploads\/2020\/03\/beanie-2-768x768.jpg 768w", - "sizes": "(max-width: 801px) 100vw, 801px", - "name": "beanie-2.jpg", - "alt": "" - } - ], - "variation": [], - "prices": { - "currency_code": "GBP", - "currency_symbol": "£", - "currency_minor_unit": 2, - "currency_decimal_separator": ".", - "currency_thousand_separator": ",", - "currency_prefix": "£", - "currency_suffix": "", - "price": "1000", - "regular_price": "2000", - "sale_price": "1000", - "price_range": null, - "raw_prices": { - "precision": 6, - "price": "10000000", - "regular_price": "20000000", - "sale_price": "10000000" - } - }, - "totals": { - "currency_code": "GBP", - "currency_symbol": "£", - "currency_minor_unit": 2, - "currency_decimal_separator": ".", - "currency_thousand_separator": ",", - "currency_prefix": "£", - "currency_suffix": "", - "line_subtotal": "1000", - "line_subtotal_tax": "0", - "line_total": "800", - "line_total_tax": "0" - } - }, - { - "key": "e369853df766fa44e1ed0ff613f563bd", - "id": 34, - "quantity": 1, - "quantity_limit": 99, - "name": "WordPress Pennant", - "summary": "
This is an external product.<\/p>", - "short_description": "
This is an external product.<\/p>", - "description": "
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.<\/p>", - "sku": "wp-pennant", - "low_stock_remaining": null, - "backorders_allowed": false, - "show_backorder_badge": false, - "sold_individually": false, - "permalink": "https:\/\/local.wordpress.test\/product\/wordpress-pennant\/", - "images": [ - { - "id": 57, - "src": "https:\/\/local.wordpress.test\/wp-content\/uploads\/2020\/03\/pennant-1.jpg", - "thumbnail": "https:\/\/local.wordpress.test\/wp-content\/uploads\/2020\/03\/pennant-1-324x324.jpg", - "srcset": "https:\/\/local.wordpress.test\/wp-content\/uploads\/2020\/03\/pennant-1.jpg 800w, https:\/\/local.wordpress.test\/wp-content\/uploads\/2020\/03\/pennant-1-324x324.jpg 324w, https:\/\/local.wordpress.test\/wp-content\/uploads\/2020\/03\/pennant-1-100x100.jpg 100w, https:\/\/local.wordpress.test\/wp-content\/uploads\/2020\/03\/pennant-1-416x416.jpg 416w, https:\/\/local.wordpress.test\/wp-content\/uploads\/2020\/03\/pennant-1-300x300.jpg 300w, https:\/\/local.wordpress.test\/wp-content\/uploads\/2020\/03\/pennant-1-150x150.jpg 150w, https:\/\/local.wordpress.test\/wp-content\/uploads\/2020\/03\/pennant-1-768x768.jpg 768w", - "sizes": "(max-width: 800px) 100vw, 800px", - "name": "pennant-1.jpg", - "alt": "" - } - ], - "variation": [], - "prices": { - "currency_code": "GBP", - "currency_symbol": "£", - "currency_minor_unit": 2, - "currency_decimal_separator": ".", - "currency_thousand_separator": ",", - "currency_prefix": "£", - "currency_suffix": "", - "price": "1105", - "regular_price": "1105", - "sale_price": "1105", - "price_range": null, - "raw_prices": { - "precision": 6, - "price": "11050000", - "regular_price": "11050000", - "sale_price": "11050000" - } - }, - "totals": { - "currency_code": "GBP", - "currency_symbol": "£", - "currency_minor_unit": 2, - "currency_decimal_separator": ".", - "currency_thousand_separator": ",", - "currency_prefix": "£", - "currency_suffix": "", - "line_subtotal": "1105", - "line_subtotal_tax": "0", - "line_total": "884", - "line_total_tax": "0" - } - } - ], - "items_count": 2, - "items_weight": 0, - "needs_payment": true, - "needs_shipping": true, - "has_calculated_shipping": true, - "totals": { - "currency_code": "GBP", - "currency_symbol": "£", - "currency_minor_unit": 2, - "currency_decimal_separator": ".", - "currency_thousand_separator": ",", - "currency_prefix": "£", - "currency_suffix": "", - "total_items": "2105", - "total_items_tax": "0", - "total_fees": "0", - "total_fees_tax": "0", - "total_discount": "421", - "total_discount_tax": "0", - "total_shipping": "500", - "total_shipping_tax": "0", - "total_price": "2184", - "total_tax": "0", - "tax_lines": [] - }, - "errors": [], - "payment_requirements": [ "products" ], - "generated_timestamp": "1628668379361", - "extensions": {}, + "coupons": [ + { + "code": "discount20", + "totals": { + "currency_code": "GBP", + "currency_symbol": "£", + "currency_minor_unit": 2, + "currency_decimal_separator": ".", + "currency_thousand_separator": ",", + "currency_prefix": "£", + "currency_suffix": "", + "total_discount": "421", + "total_discount_tax": "0" + } + } + ], + "shipping_rates": [ + { + "package_id": 0, + "name": "Shipping", + "destination": { + "address_1": "550 Central Park West", + "address_2": "Corner Penthouse Spook Central", + "city": "New York", + "state": "NY", + "postcode": "10023", + "country": "US" + }, + "items": [ + { + "key": "9bf31c7ff062936a96d3c8bd1f8f2ff3", + "name": "Beanie", + "quantity": 1 + }, + { + "key": "e369853df766fa44e1ed0ff613f563bd", + "name": "WordPress Pennant", + "quantity": 1 + } + ], + "shipping_rates": [ + { + "rate_id": "flat_rate:4", + "name": "Flat rate", + "description": "", + "delivery_time": "", + "price": "500", + "instance_id": 4, + "method_id": "flat_rate", + "meta_data": [ + { + "key": "Items", + "value": "Beanie × 1, WordPress Pennant × 1" + } + ], + "selected": true, + "currency_code": "GBP", + "currency_symbol": "£", + "currency_minor_unit": 2, + "currency_decimal_separator": ".", + "currency_thousand_separator": ",", + "currency_prefix": "£", + "currency_suffix": "" + } + ] + } + ], + "shipping_address": { + "first_name": "Peter", + "last_name": "Venkman", + "company": "", + "address_1": "550 Central Park West", + "address_2": "Corner Penthouse Spook Central", + "city": "New York", + "state": "NY", + "postcode": "10023", + "country": "US" + }, + "items": [ + { + "key": "9bf31c7ff062936a96d3c8bd1f8f2ff3", + "id": 15, + "quantity": 1, + "quantity_limit": 99, + "name": "Beanie", + "summary": "
This is a simple product.
", + "short_description": "This is a simple product.
", + "description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.
", + "sku": "woo-beanie", + "low_stock_remaining": null, + "backorders_allowed": false, + "show_backorder_badge": false, + "sold_individually": false, + "permalink": "https://local.wordpress.test/product/beanie/", + "images": [ + { + "id": 44, + "src": "https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2.jpg", + "thumbnail": "https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-324x324.jpg", + "srcset": "https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2.jpg 801w, https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-324x324.jpg 324w, https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-100x100.jpg 100w, https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-416x416.jpg 416w, https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-300x300.jpg 300w, https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-150x150.jpg 150w, https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-768x768.jpg 768w", + "sizes": "(max-width: 801px) 100vw, 801px", + "name": "beanie-2.jpg", + "alt": "" + } + ], + "variation": [], + "prices": { + "currency_code": "GBP", + "currency_symbol": "£", + "currency_minor_unit": 2, + "currency_decimal_separator": ".", + "currency_thousand_separator": ",", + "currency_prefix": "£", + "currency_suffix": "", + "price": "1000", + "regular_price": "2000", + "sale_price": "1000", + "price_range": null, + "raw_prices": { + "precision": 6, + "price": "10000000", + "regular_price": "20000000", + "sale_price": "10000000" + } + }, + "totals": { + "currency_code": "GBP", + "currency_symbol": "£", + "currency_minor_unit": 2, + "currency_decimal_separator": ".", + "currency_thousand_separator": ",", + "currency_prefix": "£", + "currency_suffix": "", + "line_subtotal": "1000", + "line_subtotal_tax": "0", + "line_total": "800", + "line_total_tax": "0" + } + }, + { + "key": "e369853df766fa44e1ed0ff613f563bd", + "id": 34, + "quantity": 1, + "quantity_limit": 99, + "name": "WordPress Pennant", + "summary": "This is an external product.
", + "short_description": "This is an external product.
", + "description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.
", + "sku": "wp-pennant", + "low_stock_remaining": null, + "backorders_allowed": false, + "show_backorder_badge": false, + "sold_individually": false, + "permalink": "https://local.wordpress.test/product/wordpress-pennant/", + "images": [ + { + "id": 57, + "src": "https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1.jpg", + "thumbnail": "https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-324x324.jpg", + "srcset": "https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1.jpg 800w, https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-324x324.jpg 324w, https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-100x100.jpg 100w, https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-416x416.jpg 416w, https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-300x300.jpg 300w, https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-150x150.jpg 150w, https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-768x768.jpg 768w", + "sizes": "(max-width: 800px) 100vw, 800px", + "name": "pennant-1.jpg", + "alt": "" + } + ], + "variation": [], + "prices": { + "currency_code": "GBP", + "currency_symbol": "£", + "currency_minor_unit": 2, + "currency_decimal_separator": ".", + "currency_thousand_separator": ",", + "currency_prefix": "£", + "currency_suffix": "", + "price": "1105", + "regular_price": "1105", + "sale_price": "1105", + "price_range": null, + "raw_prices": { + "precision": 6, + "price": "11050000", + "regular_price": "11050000", + "sale_price": "11050000" + } + }, + "totals": { + "currency_code": "GBP", + "currency_symbol": "£", + "currency_minor_unit": 2, + "currency_decimal_separator": ".", + "currency_thousand_separator": ",", + "currency_prefix": "£", + "currency_suffix": "", + "line_subtotal": "1105", + "line_subtotal_tax": "0", + "line_total": "884", + "line_total_tax": "0" + } + } + ], + "items_count": 2, + "items_weight": 0, + "needs_payment": true, + "needs_shipping": true, + "has_calculated_shipping": true, + "totals": { + "currency_code": "GBP", + "currency_symbol": "£", + "currency_minor_unit": 2, + "currency_decimal_separator": ".", + "currency_thousand_separator": ",", + "currency_prefix": "£", + "currency_suffix": "", + "total_items": "2105", + "total_items_tax": "0", + "total_fees": "0", + "total_fees_tax": "0", + "total_discount": "421", + "total_discount_tax": "0", + "total_shipping": "500", + "total_shipping_tax": "0", + "total_price": "2184", + "total_tax": "0", + "tax_lines": [] + }, + "errors": [], + "payment_requirements": [ "products" ], + "extensions": {} } ``` @@ -262,11 +261,11 @@ If a cart action cannot be performed, an error response will be returned. This w ```json { - "code": "woocommerce_rest_cart_invalid_product", - "message": "This product cannot be added to the cart.", - "data": { - "status": 400 - } + "code": "woocommerce_rest_cart_invalid_product", + "message": "This product cannot be added to the cart.", + "data": { + "status": 400 + } } ``` @@ -301,7 +300,7 @@ Returns the full cart object response (see [Cart Response](#cart-response)). ## Add Item -Add an item to the cart and return the full cart response, or an error. +Add an item to the cart and return the full cart response, or an error. This endpoint will return an error unless a valid [Nonce Token](nonce-tokens.md) is provided. @@ -323,7 +322,7 @@ Returns the full [Cart Response](#cart-response) on success, or an [Error Respon ## Remove Item -Remove an item from the cart and return the full cart response, or an error. +Remove an item from the cart and return the full cart response, or an error. This endpoint will return an error unless a valid [Nonce Token](nonce-tokens.md) is provided. @@ -343,7 +342,7 @@ Returns the full [Cart Response](#cart-response) on success, or an [Error Respon ## Update Item -Update an item in the cart and return the full cart response, or an error. +Update an item in the cart and return the full cart response, or an error. This endpoint will return an error unless a valid [Nonce Token](nonce-tokens.md) is provided. @@ -364,7 +363,7 @@ Returns the full [Cart Response](#cart-response) on success, or an [Error Respon ## Apply Coupon -Apply a coupon to the cart and return the full cart response, or an error. +Apply a coupon to the cart and return the full cart response, or an error. This endpoint will return an error unless a valid [Nonce Token](nonce-tokens.md) is provided. @@ -384,7 +383,7 @@ Returns the full [Cart Response](#cart-response) on success, or an [Error Respon ## Remove Coupon -Remove a coupon from the cart and return the full cart response, or an error. +Remove a coupon from the cart and return the full cart response, or an error. This endpoint will return an error unless a valid [Nonce Token](nonce-tokens.md) is provided. @@ -404,7 +403,7 @@ Returns the full [Cart Response](#cart-response) on success, or an [Error Respon ## Update Customer -Update customer data and return the full cart response, or an error. +Update customer data and return the full cart response, or an error. This endpoint will return an error unless a valid [Nonce Token](nonce-tokens.md) is provided. @@ -439,7 +438,7 @@ Returns the full [Cart Response](#cart-response) on success, or an [Error Respon ## Select Shipping Rate -Selects an available shipping rate for a package, then returns the full cart response, or an error. +Selects an available shipping rate for a package, then returns the full cart response, or an error. This endpoint will return an error unless a valid [Nonce Token](nonce-tokens.md) is provided. @@ -447,14 +446,13 @@ This endpoint will return an error unless a valid [Nonce Token](nonce-tokens.md) POST /cart/select-shipping-rate ``` -| Attribute | Type | Required | Description | -| :----------- | :------ | :------: | :---------------------------------------------- | -| `package_id` | integer|string | yes | The ID of the shipping package within the cart. | -| `rate_id` | string | yes | The chosen rate ID for the package. | +| Attribute | Type | Required | Description | +| :----------- | :------ | :------: | :---------------------------------- | +| `package_id` | integer | string | yes | The ID of the shipping package within the cart. | +| `rate_id` | string | yes | The chosen rate ID for the package. | ```http curl --header "X-WC-Store-API-Nonce: 12345" --request POST /cart/select-shipping-rate?package_id=1&rate_id=flat_rate:1 ``` Returns the full [Cart Response](#cart-response) on success, or an [Error Response](#error-response) on failure. - diff --git a/tests/e2e/specs/frontend/cart.test.js b/tests/e2e/specs/frontend/cart.test.js deleted file mode 100644 index b4cc0e5ab7a..00000000000 --- a/tests/e2e/specs/frontend/cart.test.js +++ /dev/null @@ -1,195 +0,0 @@ -/** - * External dependencies - */ -import { switchUserToAdmin } from '@wordpress/e2e-test-utils'; -import { shopper } from '@woocommerce/e2e-utils'; - -/** - * Internal dependencies - */ -import { - getBlockPagePermalink, - getNormalPagePermalink, - visitPostOfType, - scrollTo, -} from '../../../utils'; - -const block = { - name: 'Cart', - slug: 'woocommerce/cart', - class: '.wc-block-cart', -}; - -if ( process.env.WOOCOMMERCE_BLOCKS_PHASE < 2 ) - // eslint-disable-next-line jest/no-focused-tests - test.only( `skipping ${ block.name } tests`, () => {} ); - -describe( `${ block.name } Block (frontend)`, () => { - let cartBlockPermalink; - let productPermalink; - - beforeAll( async () => { - await page.evaluate( () => window.localStorage.clear() ); - await page.evaluate( () => { - localStorage.setItem( - 'wc-blocks_dismissed_compatibility_notices', - '["checkout","cart"]' - ); - } ); - await switchUserToAdmin(); - cartBlockPermalink = await getBlockPagePermalink( - `${ block.name } Block` - ); - await visitPostOfType( 'Woo Single #1', 'product' ); - productPermalink = await getNormalPagePermalink(); - await page.goto( productPermalink ); - await shopper.addToCart(); - } ); - afterAll( async () => { - // empty cart from shortcode page - await shopper.goToCart(); - await shopper.removeFromCart( 'Woo Single #1' ); - await page.evaluate( () => { - localStorage.removeItem( - 'wc-blocks_dismissed_compatibility_notices' - ); - } ); - } ); - - it( 'Adds a timestamp to localstorage when the cart is updated', async () => { - await jest.setTimeout( 60000 ); - await page.goto( cartBlockPermalink ); - await page.waitForFunction( () => { - const wcCartStore = wp.data.select( 'wc/store/cart' ); - return ( - ! wcCartStore.isResolving( 'getCartData' ) && - wcCartStore.hasFinishedResolution( 'getCartData', [] ) - ); - } ); - await page.click( - '.wc-block-cart__main .wc-block-components-quantity-selector__button--plus' - ); - await page.waitForFunction( () => { - const timeStamp = window.localStorage.getItem( - 'wc-blocks_cart_update_timestamp' - ); - return typeof timeStamp === 'string' && timeStamp; - } ); - const timestamp = await page.evaluate( () => { - return window.localStorage.getItem( - 'wc-blocks_cart_update_timestamp' - ); - } ); - expect( timestamp ).not.toBeNull(); - } ); - - it( 'Shows the freshest cart data when using browser navigation buttons', async () => { - await page.goto( cartBlockPermalink ); - - await page - .waitForFunction( () => { - const wcCartStore = wp.data.select( 'wc/store/cart' ); - return ( - ! wcCartStore.isResolving( 'getCartData' ) && - wcCartStore.hasFinishedResolution( 'getCartData', [] ) - ); - } ) - .catch( ( err ) => { - throw new Error( - 'Waiting for the wc/store/cart to not be resolving and to have finished resolution failed. This probably means the block did not load correctly.' - ); - } ); - await page.click( - '.wc-block-cart__main .wc-block-components-quantity-selector__button--plus' - ); - - await page.waitForResponse( - ( response ) => - ( response.url().includes( '/wc/store/cart/update-item' ) && - response.status() === 200 ) || - ( response.url().includes( '/wc/store/batch' ) && - response.status() === 207 ) - ); - - const selectedValue = parseInt( - await page.$eval( - '.wc-block-cart__main .wc-block-components-quantity-selector__input', - ( el ) => el.value - ) - ); - - // This is to ensure we've clicked the right cart button. - expect( selectedValue ).toBeGreaterThan( 1 ); - - await scrollTo( '.wc-block-cart__submit-button' ); - await Promise.all( [ - page.waitForNavigation(), - page.click( '.wc-block-cart__submit-button' ), - ] ); - - // go to checkout page - // note: shortcode checkout on CI / block on local env - await page.waitForSelector( 'h1', { text: 'Checkout' } ); - - // navigate back to cart block page - await page.goBack( { waitUntil: 'networkidle0' } ); - - await page - .waitForFunction( - () => { - const timeStamp = window.localStorage.getItem( - 'wc-blocks_cart_update_timestamp' - ); - return typeof timeStamp === 'string' && timeStamp; - }, - { timeout: 5000 } - ) - .catch( ( err ) => { - throw new Error( - 'Could not get the wc-blocks_cart_update_timestamp item from local storage. It must not have been set by the cartUpdateMiddleware.' - ); - } ); - - const timestamp = await page.evaluate( () => { - return window.localStorage.getItem( - 'wc-blocks_cart_update_timestamp' - ); - } ); - expect( timestamp ).not.toBeNull(); - - // We need this to check if the block is done loading. - await page - .waitForFunction( () => { - const wcCartStore = wp.data.select( 'wc/store/cart' ); - return ( - ! wcCartStore.isResolving( 'getCartData' ) && - wcCartStore.hasFinishedResolution( 'getCartData', [] ) - ); - } ) - .catch( ( err ) => { - throw new Error( - 'Waiting for the wc/store/cart to not be resolving and to have finished resolution failed. This probably means the block did not load correctly.' - ); - } ); - - // Then we check to ensure the stale cart action has been emitted, so it'll fetch the cart from the API. - await page - .waitForFunction( () => { - const wcCartStore = wp.data.select( 'wc/store/cart' ); - return wcCartStore.isCartDataStale() === true; - } ) - .catch( ( err ) => { - throw new Error( - 'isCartDataStale on the wc/store/cart store is not true. The cart contents were not correctly marked as stale.' - ); - } ); - - const value = parseInt( - await page.$eval( - '.wc-block-components-quantity-selector__input', - ( el ) => el.value - ) - ); - expect( value ).toBe( selectedValue ); - } ); -} );