skip to Main Content

I am using Shopify’s shopify_api gem in Ruby.

I need to update each products cost and price from an external source.

products = ShopifyAPI::Product.find(:all, :params => {:limit => limit})

products.each do |product|
        #product.variants.first.price == price
    end
end

Is this the correct way to update a products price?

Do I need to do product.variants.first.save after I have done product.variants.first.price == price or is that done automatically?

How can I update the products cost (product.variants.first.cost doesn’t seem to exist)?

2

Answers


  1. In order to update product variant, I think calling the save method is necessary

    variant = product.variants.first
    variant.price = new_price
    variant.save
    

    product cost mentioned is order? You should use ShopifyAPI::Order ( check this sample and the api )

    If you need other attribute for product, check out the metafields using ShopifyAPI::Metafield

    FYI, Order has line_items property and we also have ShopifyAPI:LineItem if u find it helpful

    Login or Signup to reply.
  2. I don’t think you can still update product (variant) prices like that. You will likely have to first loop through the product variants, and for each one you get an inventory_item_id. You use that to get the inventory item details. With the inventory item details, you can set the price and cost price, and SKU.

    It used to be so much simpler! Another option for you is to use a mutation on the variant, and set prices with the GraphQL API. In either case, you are forced to update using the inventory item attributes and not variant price.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search