Shop
Cart

Cart

The cart object is arguably the most crucial component of an online shop. Shopify offers a range of features around the cart that Shophub must manage effectively.

Key Parameter

August 15, 2024

Shopify has updated the process for fetching and updating carts. Typically, like all other Shopify entities, data is retrieved using the GID of that entity.

For example, a cart GID might look like this: gid://shopify/Cart/12345ABCD, where the keyword Cart indicates that the entity is a Cart entity. This keyword changes depending on the type of entity. However, for carts, the GID now includes an additional parameter called key.

Thus, new cart GIDs appear as follows: gid://shopify/Cart/12345ABCD?key=DEFG56789. Similar to URL search parameters, the key is appended to the cart's GID. The Shopify Developer Changelog states:

The Storefront API Cart ID now includes a key param that must be included for any mutation or query operations where the Cart ID is passed. Updating a cart without including the key param will result in the removal of buyer details and your updates will be applied to a newly generated cart. This behavior is applied retroactively to all versions of the Storefront API.

Read more (opens in a new tab)

To prevent side effects from Shopify generating new cart objects or altering existing ones due to the absence of a key parameter, the backend must now handle cart keys actively.

The key is stored in our database when the cart is created. This database entry is then retrieved by the backend whenever operations on a cart entity are performed. Cart objects sent to the frontend do include the key value in their ID but will not include it in the local storage. Consequently, the frontend does not need to know the key parameter value, as this is fully managed by the backend.

Stale Cart Objects

To prevent our database from accumulating long-expired cart objects, Shophub runs a cron job daily at 4 AM. This cron job identifies and deletes all cart entries in our database that were created 10 days ago or earlier. The 10-day threshold aligns with Shopify's cart expiration policy. (source (opens in a new tab))

Cart Association

Shopify carts can be associated with a customer to include a buyerIdentity, which contains information about the buyer interacting with the cart. This association also makes the cart private and inaccessible to other customers. If an unauthenticated customer—or one who is not the original associated customer—attempts to access the cart, a 404 error will be returned. This mechanism prevents unauthorized access to cart information.

Shopify attempts to associate an unassociated cart with a customer on several occasions:

  • Cart Creation: When a cart is created, Shopify checks if the requesting customer is authenticated. If so, the cart is associated with that customer.
  • Cart Fetching: When an unassociated cart is fetched, Shopify attempts to associate it with the current customer if they are authenticated.

Once a cart is associated with a customer, Shophub will not attempt to re-associate it with another customer. The cart remains linked to the originally associated customer.

Custom Attributes

Shopify allows a cart object to contain any number of custom attributes in the form of key:value pairs, where both the key and the value must be strings, except that the value can also be undefined. These custom attributes will be displayed on the order in the admin UI.

Shophub leverages this feature to add a custom attribute for the desired delivery date. Once a customer selects a delivery date, the custom attribute is added to the cart object and becomes accessible to an admin later on. All currently whitelisted attributes can be found under the CART_ATTRIBUTE_WHITELIST constant. Any attributes not listed in this array will be removed during the cart object's sanitation process.

Cart Product Availability

August 28, 2024

In most areas of the shop, product availability is determined based on the values and settings saved in the Shophub database. However, the cart operates differently, as its data is fetched directly from Shopify. This approach makes it impractical—or at least costly—to verify if a product is truly out of stock or if its inventory is simply untracked. To accurately determine the stock status, Shophub would need to check each line item every time a cart is requested and modify the returned JSON to indicate whether a product variant is actually out of stock.

Currently, we have decided against this approach and instead return the cart data exactly as it is fetched from Shopify.

The issue, as mentioned in the Untracked Inventory section on this page, is that even when products are marked as untracked inventory, the cart object still provides a quantity for the available product, without indicating its tracking status. Thus, if a product is set to a quantity of 0 but flagged as inventory untracked, Shophub will still display the product as out of stock. This can be avoided by setting the product’s available quantity above zero before marking its inventory as untracked.

This is a limitation within Shopify, which may be addressed in future updates to streamline the process of checking line items.

Optional Features

VIP Program

When utilizing the VIP Program feature, the cart is configured to apply custom discounts to the customer’s cart based on their VIP level. This is accomplished by adding all relevant discounts, which are saved in our database as "VIP program-related" discounts. Since these discounts are linked to a Shopify segment, they will only be applicable if the current customer is part of that segment.

Segments are customer categories within Shopify that allow grouping customers based on various criteria, such as the amount spent over the last six months, for example.