使用SOAP V2(Savon gem)将可配置产品添加到Magenta购物车

时间:2016-01-14 16:34:21

标签: ruby-on-rails ruby magento soap cart

我遇到了一个非常普遍的问题,但我发现的所有解决方案都是针对PHP而不是Ruby。

我使用Savon gem(https://github.com/savonrb/savon)与Magento API SOAP V2进行通信。

我尝试将可配置产品添加到购物车(https://www.magentocommerce.com/api/soap/checkout/cartProduct/cart_product.add.html)。

该产品有两种选择,味道和强度。

我的代码是:

require 'savon'
client = Savon.client(wsdl: 'http://example.com/api/v2_soap/index/wsdl/1')
# new session
session_id = client.call(:login, :message => {:username=> 'username', :apiKey=>'api_key'}).body[:login_response][:login_return]
# new cart
res = client.call(:shopping_cart_create, message: {sessionId: session_id})
quote_id = res.body[:shopping_cart_create_response][:quote_id]

product_id = 6
product_data = {
 'product_id' => product_id,
 'qty' => 1,
 'options' => [{ 
    'key' => 537,
    'value' => 51
  },
  {
    'key' => 549,
    'value' => 60
  },
 ]
}


res = client.call(:shopping_cart_product_add, message: {sessionId: session_id, quoteId: quote_id, products: {item: [product_data]}})

我收到了以下错误:

(1022) Please specify the product's option(s).

我认为我的选择论证不正确,但我不明白它应该如何?

2 个答案:

答案 0 :(得分:0)

UPD:这不起作用,但由于评论中的信息,我不会删除此答案。

尝试将请求更改为:

res = client.call(:shopping_cart_product_add,message: {
  sessionId: session_id, 
  quoteId: quote_id, 
  products: [product_data]
})

答案 1 :(得分:0)

Finally, I got it,

I've added to the shoppingCarteProductEntity in: wsi.xml and wsdl.xml the line :

<element name="super_attribute" type="typens:associativeArray" minOccurs="0"/>

shoppingCartProductEntity :

<complexType name="shoppingCartProductEntity">
    <all>
        <element name="product_id" type="xsd:string" minOccurs="0"/>
        <element name="sku" type="xsd:string" minOccurs="0"/>
        <element name="qty" type="xsd:double" minOccurs="0"/>
        <element name="options" type="typens:associativeArray" minOccurs="0"/>
        <element name="super_attribute" type="typens:associativeArray" minOccurs="0"/>
        <element name="bundle_option" type="typens:associativeArray" minOccurs="0"/>
        <element name="bundle_option_qty" type="typens:associativeArray" minOccurs="0"/>
        <element name="links" type="typens:ArrayOfString" minOccurs="0"/>
    </all>
</complexType>

Then (and it's the most important part), here's the way to call with Savon gem :

# ... 
# Before we initialize client, session_id, quote_id...

product_data = {
 'product_id' => 123,
 'sku' => 'MYSKU',
 'qty' => 1,
 'super_attribute' => [
    [
      {
        key: '537',
        value: '57'
      },
      {
        key: '549',
        value: '66'
      }
    ]  
  ]
}

res = client.call(:shopping_cart_product_add, message: {sessionId: session_id, quoteId: quote_id, products: {item: [product_data] }})