1

We are working on our Rails app so that it takes into account products that use the new "line item properties" on Shopify.

Part of our app looks at a customers orders as a whole and then calculate how many of a particualr product all the orders contain in total.

So you have 3 orders with the same product on it, our app will then show a single product in a list with the total quantity for all 3 orders combined. Nice and easy for picking.

However, I want to seperate the products with line item properties into seperate products in the list, so these can be displayed with the properties.

I can get hold of the properties from the Order object ok, and I know that the same product can be in the order multiple times with different line item properties attached.

BUT what on the order.line_item tells me that this is a "different product"? ie same product but different properties, what tells them appart except for the properties themselves?

Example Line Item:

  "line_items": [
    {
      "fulfillment_service": "manual",
      "fulfillment_status": null,
      "grams": 200,
      "id": 466157049,
      "price": "199.00",
      "product_id": 632910392,
      "quantity": 1,
      "requires_shipping": true,
      "sku": "IPOD2008GREEN",
      "title": "IPod Nano - 8gb",
      "variant_id": 39072856,
      "variant_title": "green",
      "vendor": null,
      "name": "IPod Nano - 8gb - green",
      "variant_inventory_management": "shopify",
      "properties": [
        {
          "name": "Custom Engraving",
          "value": "Happy Birthday"
        }
      ]
    },

This is what our code looks like before adding support for line item properties.

def build_products_list(orders)
  products = {}
  orders.each do |order|
    order.line_items.each do |item|
      products[item.name] = {} unless products[item.name]
      products[item.name]['sku'] = item.sku
      products[item.name]['qty'] = (products[item.name]['qty'] ? products[item.name]['qty'] : 0) + item.quantity
    end
  end
  products
end

So i need to change the products[item.name] to something unique for each product, including those that have line item properties so they appear as seperate products.

What to change it to?

1 Answer 1

1

The only difference between line items with different line item properties will be the line item properties themselves.

To make unique keys for your hash, I'd encode the properties somehow(if they exist) and tack them onto the end of the key. A simple hash function should do the trick. Products with the same line item properties would have the same hash, so they should still stack accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.