How to Get Checkout Subtotal Value in PWA for Magento 2

To get the subtotal value in the checkout page of Magento 2 PWA, you can follow these steps using GraphQL and the built-in Apollo Client in PWA Studio:

Utilize this code to retrieve the subtotal in the checkoutPage.js file. Insert the following line at the beginning of the import section within the file.

    import { usePriceSummary } from '@magento/peregrine/lib/talons/CartPage/PriceSummary/usePriceSummary';

    Next, add the provided code into the CheckoutPage = props => {...} function.

      const subtotalProps = usePriceSummary();
      
      const {
          flatData
      } = subtotalProps;
      
      const {
          total
      } = flatData;

      Next, insert this code following the GET_CART_DETAILS GraphQL query

      const checkoutMeta = () => {
          fbq('track', 'InitiateCheckout', {
              'value': total?.value,
              'currency': total.currency
          });
      }
      
      useEffect(() => {
          checkoutMeta();
      }, []);

      By following the steps outlined in this guide, you can easily retrieve and display the checkout subtotal value in Magento 2 PWA.

      Leave a Reply

      Your email address will not be published. Required fields are marked *