/** * Block checkout: under the shipping options, the locker or shop cards for a chosen pickup point * rate (PMSG-347). Loaded with the checkout block by PickupPointBlocksIntegration. * * The places and the selected one come with the cart (Store API extension data); a new choice is * sent back with extensionCartUpdate and kept in the customer's session, where placing the order * reads it (PickupPointBlocks::add_to_order). */ import { useCallback, useEffect, useState } from 'react'; import { registerPlugin } from '@wordpress/plugins'; import { ExperimentalOrderShippingPackages, extensionCartUpdate, } from '@woocommerce/blocks-checkout'; import { PickupPointCards, PickupPointChoice } from './pickup-point-cards'; import './cards.css'; const NAMESPACE = 'parcel2go-shipping'; interface FillProps { extensions?: Record; } function PackageChoice({ choice }: { choice: PickupPointChoice }) { // The customer's pick shows at once; the cart's selection takes over once it is saved. const [picked, setPicked] = useState(null); useEffect(() => setPicked(null), [choice.selected, choice.provider]); const onSelect = useCallback( (id: number) => { setPicked(id); extensionCartUpdate({ namespace: NAMESPACE, data: { package: choice.package, id, provider: choice.provider }, }).catch(() => setPicked(null)); }, [choice.package, choice.provider] ); return ; } function PickupPointFill(props: FillProps) { const choices = props.extensions?.[NAMESPACE]?.pickupPoints ?? []; if (choices.length === 0) { return null; } return (
{choices.map((choice) => ( ))}
); } function Render() { return ( {/* The slot clones its children with its props (cart, extensions). */} ); } registerPlugin('parcel2go-pickup-point', { render: Render, scope: 'woocommerce-checkout' });