1.4 KiB
1.4 KiB
The useCartDiscountCodesUpdateCallback
hook returns a callback that can be used to update the cart's discount codes. It must be a descendent of a CartProvider
component.
Example code
import {useCartDiscountCodesUpdateCallback} from '@shopify/hydrogen';
export function CartDiscountCodeUpdate() {
const updateDiscountCodes = useCartDiscountCodesUpdateCallback();
const [discountCode, setDiscountCode] = useState();
return (
<>
<input
type="text"
placeholder="Discount code"
onChange={(event) => {
setDiscountCode(event.target.value);
}}
/>
<button
onClick={() => {
updateDiscountCodes([discountCode]);
}}
>
Apply
</button>
</>
);
}
Return value
A callback to update the cart's discount codes. The callback expects an array of strings corresponding to the discount codes to apply to the cart.