简体   繁体   中英

Update Shopify product cost and price using Ruby

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)?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM