/* global wc, wp */ (() => { // Bail if Blocks globals aren’t present if (!window.wp || !window.wc) return; const { __, sprintf } = wp.i18n || { __: (s) => s, sprintf: (s) => s }; const { registerPlugin } = wp.plugins; const { Notice } = wp.components; const { useState } = wp.element; const { useDispatch, useSelect } = wp.data; // Woo Blocks slot and helpers const blocksCheckout = wc.blocksCheckout || wc.blocks || {}; const { ExperimentalOrderMeta, extensionCartUpdate } = blocksCheckout || {}; // Cart data store key const cartStore = (wc.wcBlocksData && wc.wcBlocksData.cartStore) || 'wc/store/cart'; if (!ExperimentalOrderMeta || !useDispatch || !useSelect) return; // Store API extensions namespace const NAMESPACE = 'pw-gift-cards'; const ApplyGiftCard = () => { const [code, setCode] = useState(''); const [isSubmitting, setIsSubmitting] = useState(false); const [notice, setNotice] = useState(null); // Get full cart data so we can read our extension data const cartData = useSelect( (select) => { const s = select(cartStore); return s && s.getCartData ? s.getCartData() : null; }, [] ); // Applied gift cards coming from the Store API extensions payload const giftCards = (cartData && cartData.extensions && cartData.extensions[NAMESPACE] && cartData.extensions[NAMESPACE].gift_cards) || []; const submit = async () => { if (!code) { setNotice({ status: 'error', message: __('Enter a gift card code.', 'pw-woocommerce-gift-cards') }); return; } setIsSubmitting(true); setNotice(null); try { await extensionCartUpdate({ namespace: NAMESPACE, data: { code } }); setCode(''); } catch (err) { setNotice({ status: 'error', message: err?.message || __('Something went wrong.', 'pw-woocommerce-gift-cards') }); } finally { setIsSubmitting(false); } }; const handleRemove = async (cardNumber) => { setIsSubmitting(true); setNotice(null); try { await extensionCartUpdate({ namespace: NAMESPACE, data: { code: cardNumber, remove: true, }, }); } catch (err) { setNotice({ status: 'error', message: err?.message || __('Unable to remove gift card.', 'pw-woocommerce-gift-cards'), }); } finally { setIsSubmitting(false); } }; return (
{giftCards.length > 0 && (
{__('Gift cards applied', 'pw-woocommerce-gift-cards')}
    {giftCards.map((card) => (
  • {card.number}
    {card.balance && (
    {sprintf( __('Remaining balance is %s', 'pw-woocommerce-gift-cards'), card.balance )}
    )}
    {card.amount && ( {card.amount} )}
  • ))}
)} {!window.pwgcBlocks.hideRedeemForm && (
{ e.preventDefault(); submit(); }} noValidate > setCode(e.target.value)} placeholder={__('Gift card number', 'pw-woocommerce-gift-cards')} aria-label={__('Gift card number', 'pw-woocommerce-gift-cards')} disabled={isSubmitting} /> {notice && ( {notice.message} )}
)}
); }; // Register for both checkout and cart scopes registerPlugin('pw-apply-gift-card-checkout', { render: ApplyGiftCard, scope: 'woocommerce-checkout', }); registerPlugin('pw-apply-gift-card-cart', { render: ApplyGiftCard, scope: 'woocommerce-cart', }); })();