hydrogen/packages/hydrogen/src/hooks/useCartDiscountCodesUpdateCallback/README.md
2021-11-09 18:20:09 -08:00

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.