0
votes

I created a custom gift box creator for a client. The current approach is that I created a "Gift Box" product and add the selected items as attributes on the cart item. This is all done in the theme's JS code but the problem I'm facing is that because I'm not actually adding the underlying items to the cart inventory is not updating.

Is there a way to not show the underlying items in the cart but have checkout update their inventory counts?

1
Need to check and skip using liquid or CSS in cart page. - Onkar
Is there an easy way to add the items to the cart, price it at $0 with some attribute that I'll use to skip in liquid? - Everest
You might want to look at the Ajax API -> shopify.dev/docs/themes/ajax-api/reference/cart - Karim Tarek

1 Answers

0
votes

What if you had a microservice which responded to order/created and edited the order to include the items @ $0?

You could use the steps outlined in this official Shopify tutorial on their GraphQL feature, and implemented in node:

import Shopify from 'shopify-api-node'; // https://github.com/MONEI/Shopify-api-node

shopify = new Shopify({/* your app credentials */});

// assume this is the HTTP endpoint w/ a JSON middleware
function(req, res) {
  const order = req.body;

  const giftBasket = order.line_items.find(li => li.id === 1234); // 1234, or whatever the product ID of your gift basket is.

  const realItemSKUs = giftBasket.properties.filter((lineItemProp) => {
    // assuming you use a product1: sku, product2: sku line item attribute name/value.
    // but adjust to your needs.
    return lineItemProp.name.match('product\d'); 
  }).map((lineItemProp) => lineItemProp.value);

  function createVariantQuery(sku) {
    return `product${id}: {
      productVariants(query: "sku:${sku}") { id }
    }`;
  }

  const query = realItemSKUs.map((sku) => createVariantQuery(sku)).join('\n')

  const variantIds = await shopify.graphql(`{${query}}`)
    .then((variants) => Object.values(variants).map((variant) => variant.id));

  const orderEditRes = await shopify.graphql(`mutation beginEdit{
    orderEditBegin(id: "gid://shopify/Order/1234"){
      calculatedOrder{
        id
      }
    }
  }`);

  const calcLineItems = await Promise.all(variantIds.map(async (id) =>
    shopify.graphql(`mutation addVariantToOrder{
      orderEditAddVariant(id: "gid://shopify/CalculatedOrder/${orderEditRes.calculatedOrder}", variantId: "${id}", quantity: 1) {
        calculatedOrder {
          id
          addedLineItems(first:5) {
            edges {
              node {
                id
                quantity
              }
            }
          }
        }
        userErrors {
          field
          message
        }
      }
    }`);
  ));

  await Promise.all(calcLineItems.map(async (calcLineItem =>
    shopify.graphql(`addDiscount {
        orderEditAddLineItemDiscount(id: "${calculatedOrder.id}", lineItemId: "${calcLineItem.id}", discount: {percentValue: 100, description: "Giftbasket"}) {
        calculatedOrder {
          id
          addedLineItems(first:5) {
            edges {
              node {
                id
                quantity
              }
            }
          }
        }
        userErrors {
          message
        }
      }
    }`
  );

  return shopify.graphql(`mutation commitEdit {
    orderEditCommit(id: "${calculatedOrder}", notifyCustomer: false, staffNote: "Auto giftbasket updated") {
      order {
        id
      }
      userErrors {
        field
        message
      }
    }
  }`);
};