使用 SOAP V2 (Savon gem) 将可配置产品添加到 Magenta 购物车
Add a configurable product to Magenta cart using SOAP V2 (Savon gem)
我遇到了一个非常普遍的问题,但我找到的所有解决方案都是针对 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).
我认为我的选项参数不正确,但我不明白它应该如何?
更新: 这不起作用,但由于评论中的信息,我不会删除此答案。
尝试将请求更改为:
res = client.call(:shopping_cart_product_add,message: {
sessionId: session_id,
quoteId: quote_id,
products: [product_data]
})
终于明白了,
我在 shoppingCarteProductEntity
中添加了:wsi.xml
和 wsdl.xml
行:
<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>
然后(这是最重要的部分),这是用 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] }})
我遇到了一个非常普遍的问题,但我找到的所有解决方案都是针对 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).
我认为我的选项参数不正确,但我不明白它应该如何?
更新: 这不起作用,但由于评论中的信息,我不会删除此答案。
尝试将请求更改为:
res = client.call(:shopping_cart_product_add,message: {
sessionId: session_id,
quoteId: quote_id,
products: [product_data]
})
终于明白了,
我在 shoppingCarteProductEntity
中添加了:wsi.xml
和 wsdl.xml
行:
<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>
然后(这是最重要的部分),这是用 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] }})