2016-04-15 30 views
-1

我有一個應用程序需要收集各種產品選項,然後向外部網址(https://my-shop.myshopify.com/cart/add)發出發佈請求,然後重定向到該網站並添加了項目到購物車。如何使用Post方法在Rails中從外部域添加到購物車

不幸的是,我不能簡單地使用購物車固定鏈接,因爲我需要包含lineitems。到目前爲止,我已經在我的控制器中成功地創建了該帖子,但沒有重定向到shopify站點。

def add_to_cart 

    @urlstring_to_post = 'https://my-shop.myshopify.com/cart/add' 

    #submit and get a result 

    @result = HTTParty.post(@urlstring_to_post.to_str, 
    :body => {:id => '15158758855', 
    :add => 'Add to Cart' 
    }.to_json, 
    :headers => { 'Content-Type' => 'application/json' }) 

    end 

回答

0

我不知道你從Shopify API會得到什麼(例如,是什麼的@result內容),但你可以執行這樣的重定向:

class CartItemController 
    def create 
    # .. some stuff to add an item to the cart ... 
    redirect_to "/your-custom-url" 
    end 
end 
相關問題