2023-05-23 07:28:40 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { _n, sprintf } from '@wordpress/i18n';
|
|
|
|
import {
|
|
|
|
getCurrencyFromPriceResponse,
|
|
|
|
formatPrice,
|
|
|
|
} from '@woocommerce/price-format';
|
2023-06-30 14:05:27 +00:00
|
|
|
import {
|
|
|
|
CartResponse,
|
|
|
|
CartResponseTotals,
|
|
|
|
isBoolean,
|
|
|
|
} from '@woocommerce/types';
|
2023-06-19 15:14:35 +00:00
|
|
|
import { getSettingWithCoercion } from '@woocommerce/settings';
|
|
|
|
|
2023-06-30 14:05:27 +00:00
|
|
|
const getPrice = ( totals: CartResponseTotals, showIncludingTax: boolean ) => {
|
2023-06-20 12:41:37 +00:00
|
|
|
const currency = getCurrencyFromPriceResponse( totals );
|
2023-06-19 15:14:35 +00:00
|
|
|
|
2023-06-20 12:41:37 +00:00
|
|
|
const subTotal = showIncludingTax
|
|
|
|
? parseInt( totals.total_items, 10 ) +
|
|
|
|
parseInt( totals.total_items_tax, 10 )
|
|
|
|
: parseInt( totals.total_items, 10 );
|
|
|
|
|
|
|
|
return formatPrice( subTotal, currency );
|
2023-06-19 15:14:35 +00:00
|
|
|
};
|
2023-05-23 07:28:40 +00:00
|
|
|
|
2023-06-30 14:05:27 +00:00
|
|
|
export const updateTotals = (
|
|
|
|
cartData: [ CartResponseTotals, number ] | undefined
|
|
|
|
) => {
|
|
|
|
if ( ! cartData ) {
|
2023-05-23 07:28:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-06-30 14:05:27 +00:00
|
|
|
const [ totals, quantity ] = cartData;
|
|
|
|
const showIncludingTax = getSettingWithCoercion(
|
|
|
|
'displayCartPricesIncludingTax',
|
|
|
|
false,
|
|
|
|
isBoolean
|
|
|
|
);
|
|
|
|
const amount = getPrice( totals, showIncludingTax );
|
2023-06-01 12:41:43 +00:00
|
|
|
const miniCartBlocks = document.querySelectorAll( '.wc-block-mini-cart' );
|
2023-05-23 07:28:40 +00:00
|
|
|
const miniCartQuantities = document.querySelectorAll(
|
|
|
|
'.wc-block-mini-cart__badge'
|
|
|
|
);
|
|
|
|
const miniCartAmounts = document.querySelectorAll(
|
|
|
|
'.wc-block-mini-cart__amount'
|
|
|
|
);
|
|
|
|
|
2023-06-01 12:41:43 +00:00
|
|
|
miniCartBlocks.forEach( ( miniCartBlock ) => {
|
|
|
|
if ( ! ( miniCartBlock instanceof HTMLElement ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const miniCartButton = miniCartBlock.querySelector(
|
|
|
|
'.wc-block-mini-cart__button'
|
|
|
|
);
|
|
|
|
|
|
|
|
miniCartButton?.setAttribute(
|
2023-05-23 07:28:40 +00:00
|
|
|
'aria-label',
|
2023-06-01 12:41:43 +00:00
|
|
|
miniCartBlock.dataset.hasHiddenPrice
|
|
|
|
? sprintf(
|
|
|
|
/* translators: %s number of products in cart. */
|
|
|
|
_n(
|
|
|
|
'%1$d item in cart',
|
|
|
|
'%1$d items in cart',
|
|
|
|
quantity,
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
quantity
|
|
|
|
)
|
|
|
|
: sprintf(
|
|
|
|
/* translators: %1$d is the number of products in the cart. %2$s is the cart total */
|
|
|
|
_n(
|
|
|
|
'%1$d item in cart, total price of %2$s',
|
|
|
|
'%1$d items in cart, total price of %2$s',
|
|
|
|
quantity,
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
quantity,
|
|
|
|
amount
|
|
|
|
)
|
2023-05-23 07:28:40 +00:00
|
|
|
);
|
2023-06-30 14:05:27 +00:00
|
|
|
|
|
|
|
miniCartBlock.dataset.cartTotals = JSON.stringify( totals );
|
|
|
|
miniCartBlock.dataset.cartItemsCount = quantity.toString();
|
2023-05-23 07:28:40 +00:00
|
|
|
} );
|
|
|
|
miniCartQuantities.forEach( ( miniCartQuantity ) => {
|
|
|
|
if ( quantity > 0 || miniCartQuantity.textContent !== '' ) {
|
|
|
|
miniCartQuantity.textContent = quantity.toString();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
miniCartAmounts.forEach( ( miniCartAmount ) => {
|
|
|
|
miniCartAmount.textContent = amount;
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Show the tax label only if there are products in the cart.
|
|
|
|
if ( quantity > 0 ) {
|
|
|
|
const miniCartTaxLabels = document.querySelectorAll(
|
|
|
|
'.wc-block-mini-cart__tax-label'
|
|
|
|
);
|
|
|
|
miniCartTaxLabels.forEach( ( miniCartTaxLabel ) => {
|
|
|
|
miniCartTaxLabel.removeAttribute( 'hidden' );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getMiniCartTotalsFromLocalStorage = ():
|
2023-06-30 14:05:27 +00:00
|
|
|
| [ CartResponseTotals, number ]
|
2023-05-23 07:28:40 +00:00
|
|
|
| undefined => {
|
|
|
|
const rawMiniCartTotals = localStorage.getItem(
|
|
|
|
'wc-blocks_mini_cart_totals'
|
|
|
|
);
|
|
|
|
if ( ! rawMiniCartTotals ) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2023-06-30 14:05:27 +00:00
|
|
|
const cartData = JSON.parse( rawMiniCartTotals );
|
|
|
|
return [ cartData.totals, cartData.itemsCount ] as [
|
|
|
|
CartResponseTotals,
|
|
|
|
number
|
|
|
|
];
|
2023-05-23 07:28:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const getMiniCartTotalsFromServer = async (): Promise<
|
2023-06-30 14:05:27 +00:00
|
|
|
[ CartResponseTotals, number ] | undefined
|
2023-05-23 07:28:40 +00:00
|
|
|
> => {
|
|
|
|
return fetch( '/wp-json/wc/store/v1/cart/' )
|
|
|
|
.then( ( response ) => {
|
|
|
|
// Check if the response was successful.
|
|
|
|
if ( ! response.ok ) {
|
|
|
|
throw new Error();
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.json();
|
|
|
|
} )
|
|
|
|
.then( ( data: CartResponse ) => {
|
|
|
|
// Save server data to local storage, so we can re-fetch it faster
|
|
|
|
// on the next page load.
|
|
|
|
localStorage.setItem(
|
|
|
|
'wc-blocks_mini_cart_totals',
|
|
|
|
JSON.stringify( {
|
|
|
|
totals: data.totals,
|
|
|
|
itemsCount: data.items_count,
|
|
|
|
} )
|
|
|
|
);
|
2023-06-30 14:05:27 +00:00
|
|
|
return [ data.totals, data.items_count ] as [
|
|
|
|
CartResponseTotals,
|
|
|
|
number
|
|
|
|
];
|
2023-05-23 07:28:40 +00:00
|
|
|
} )
|
|
|
|
.catch( ( error ) => {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error( error );
|
|
|
|
return undefined;
|
|
|
|
} );
|
|
|
|
};
|