2013-07-03 43 views
0

我正嘗試使用shopify_api gem向商店添加產品。以下是我正在嘗試使用的代碼:使用shopify gem產品創建返回404錯誤

ShopifyAPI::Base.authenticate 
variant_shopify = ShopifyAPI::Variant.create(
          :compare_at_price => @variant.compare_at_price, 
          :created_at => @variant.created_at, 
          :fulfillment_service => @variant.product.vendor.fulfillment_service.name, 
          :grams => @variant.grams, 
          :id => @variant.id, 
          :inventory_management => @variant.inventory_management, 
          :inventory_policy => @variant.inventory_policy, 
          :inventory_quantity => @variant.inventory_quantity, 
          :price => @variant.price, 
          :requires_shipping => @variant.requires_shipping, 
          :sku => @variant.sku, 
          :taxable => @variant.taxable, 
          :title => @variant.title, 
          :updated_at => @variant.updated_at) 
product_shopify = ShopifyAPI::Product.create(
          :body_html => @product.body_html, 
          :created_at => @product.created_at, 
          :id => @product.id, 
          :images => [{ :src => @product.image.url }], 
          :product_type => @product.product_type, 
          :published_at => @product.published_at, 
          :template_suffix => @product.template_suffix, 
          :title => @product.title, 
          :updated_at => @product.updated_at, 
          :variant => variant_shopify, 
          :vendor => @product.vendor.business_name) 

但是,此代碼會導致shopify發出404響應。關於我能做什麼的任何想法?感謝您在advnace的幫助

回答

0

您的代碼毫無意義。讓我們先從您的產品創建語句開始。您正在嘗試使用哈希創建產品。精細。爲什麼你的散列參數是一個產品實例變量?好的,把它放在一邊,當你創建一個產品時,你會從API獲得一個ID。另外,它爲created_at日期提供時間戳。創建不是更新,因此您不提供該更新。

假設您爲@product抓取現有產品,那麼您不僅需要提供重要的參數,還需要提供唯一的句柄。將其分解爲首先解決的問題,其他問題將隨之解決。

+0

我正在創建一個全新的項目。產品實例變量是從字段填充的對象。有沒有辦法讓shopify生成這個句柄,或者我應該生成它並自己填充它? – fizzy123

+0

花點時間看看Shopify是如何做到的。按照他們的模式。有用。 –

+0

我打算在第一句話中說產品而不是項目。也許這會導致某種誤解,但當你說「Shopify如何做」時,我並不確定你的意思,我也不知道從哪裏得到這些信息。官方的API頁面主要涉及如何通過POST和GET請求與shopify進行交互,但並沒有真正講述如何使用gem,而似乎沒有任何關於如何使用gem來創建的資源新產品。如果你知道我應該看的鏈接,這些鏈接將非常有幫助。 – fizzy123