ruby-on-railsrubymagentocartmagento-soap-api

Add a configurable product to Magenta cart using SOAP V2 (Savon gem)


I'm stuck in a very common problem, but all solutions I'm finding are for PHP instead of Ruby.

I'm using Savon gem (https://github.com/savonrb/savon) to communicate with the Magento API SOAP V2.

I try to add a configurable product to my cart (https://www.magentocommerce.com/api/soap/checkout/cartProduct/cart_product.add.html).

The product has two options, flavour and strength.

my code is :

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]}})

I've got the following error :

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

I think that my options argument is not correct, but I don't understand how it should be ?


Solution

  • 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] }})