From beadbab90fe0813c0f36cc25a9e0b14a8f8e96d2 Mon Sep 17 00:00:00 2001 From: Seghir Nadir Date: Wed, 12 May 2021 13:32:05 +0100 Subject: [PATCH] Add cart data to filters (#4164) * add cart data to filters * add extensions back to footer filter --- .../cart-checkout/order-summary/order-summary-item.js | 9 ++++++++- .../components/cart-checkout/totals/footer-item/index.js | 9 +++++++-- .../cart-checkout/cart/full-cart/cart-line-item-row.tsx | 9 ++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/assets/js/base/components/cart-checkout/order-summary/order-summary-item.js b/assets/js/base/components/cart-checkout/order-summary/order-summary-item.js index eecfdfa6e86..47fb911fb43 100644 --- a/assets/js/base/components/cart-checkout/order-summary/order-summary-item.js +++ b/assets/js/base/components/cart-checkout/order-summary/order-summary-item.js @@ -15,6 +15,7 @@ import PropTypes from 'prop-types'; import Dinero from 'dinero.js'; import { getSetting } from '@woocommerce/settings'; import { useCallback, useMemo } from '@wordpress/element'; +import { useStoreCart } from '@woocommerce/base-context/hooks'; /** * Internal dependencies @@ -41,6 +42,11 @@ const OrderSummaryItem = ( { cartItem } ) => { extensions = {}, } = cartItem; + // Prepare props to pass to the __experimentalApplyCheckoutFilter filter. + // We need to pluck out receiveCart. + // eslint-disable-next-line no-unused-vars + const { receiveCart, ...cart } = useStoreCart(); + const productPriceValidation = useCallback( ( value ) => mustBeString( value ) && mustContain( value, '' ), [] @@ -50,8 +56,9 @@ const OrderSummaryItem = ( { cartItem } ) => { () => ( { context: 'summary', cartItem, + cart, } ), - [ cartItem ] + [ cartItem, cart ] ); const priceCurrency = getCurrencyFromPriceResponse( prices ); diff --git a/assets/js/base/components/cart-checkout/totals/footer-item/index.js b/assets/js/base/components/cart-checkout/totals/footer-item/index.js index f7acaa248ab..79e1a658168 100644 --- a/assets/js/base/components/cart-checkout/totals/footer-item/index.js +++ b/assets/js/base/components/cart-checkout/totals/footer-item/index.js @@ -24,11 +24,16 @@ const SHOW_TAXES = const TotalsFooterItem = ( { currency, values } ) => { const { total_price: totalPrice, total_tax: totalTax } = values; - const { extensions } = useStoreCart(); + + // Prepare props to pass to the __experimentalApplyCheckoutFilter filter. + // We need to pluck out receiveCart. + // eslint-disable-next-line no-unused-vars + const { receiveCart, ...cart } = useStoreCart(); const label = __experimentalApplyCheckoutFilter( { filterName: 'totalLabel', defaultValue: __( 'Total', 'woo-gutenberg-products-block' ), - extensions, + extensions: cart.extensions, + arg: { cart }, // Only accept strings. validation: mustBeString, } ); diff --git a/assets/js/blocks/cart-checkout/cart/full-cart/cart-line-item-row.tsx b/assets/js/blocks/cart-checkout/cart/full-cart/cart-line-item-row.tsx index 7b7696a936f..6ee8e7f5972 100644 --- a/assets/js/blocks/cart-checkout/cart/full-cart/cart-line-item-row.tsx +++ b/assets/js/blocks/cart-checkout/cart/full-cart/cart-line-item-row.tsx @@ -9,6 +9,7 @@ import ProductName from '@woocommerce/base-components/product-name'; import { useStoreCartItemQuantity, useStoreEvents, + useStoreCart, } from '@woocommerce/base-context/hooks'; import { ProductBackorderBadge, @@ -114,12 +115,18 @@ const CartLineItemRow = ( { ( value ) => mustBeString( value ) && mustContain( value, '' ), [] ); + + // Prepare props to pass to the __experimentalApplyCheckoutFilter filter. + // We need to pluck out receiveCart. + // eslint-disable-next-line no-unused-vars + const { receiveCart, ...cart } = useStoreCart(); const arg = useMemo( () => ( { context: 'cart', cartItem: lineItem, + cart, } ), - [ lineItem ] + [ lineItem, cart ] ); const priceCurrency = getCurrencyFromPriceResponse( prices ); const name = __experimentalApplyCheckoutFilter( {