Setting Custom Options while adding a product to cart via SOAP in Magento

为君一笑 提交于 2019-12-02 00:20:41
Dayson

After much debugging and fiddling around, it turns out the 'options' must be passed as an associativeArray which in SOAP terms needs to be defined as follows:

array (size=1)
  0 => 
    array (size=3)
      'product_id' => int 25
      'qty' => int 1
      'options' => 
        array (size=1)
          0 => 
            array (size=2)
              'key' => int 1
              'value' => int 2

More on this format here - https://stackoverflow.com/a/8963453/515268

Using this format, I am able to successfully add Products with Custom Options via SOAP. The pricing in the cart info and totals also reflects the expected price.

After digging into the core files, I found the problem and a simple way to patch it.

The problem is that the SOAP API for "cart_product.add" / "shoppingCartProductAdd" accepts an array of product options and super attributes with the key "options", as you've done above, yet the code that prepares the product to be added to the cart looks for this information using the key "super_attribute", instead. To patch, I simply just copied the the "options" array to a "super_attribute" array in the cart_product.add api.

I put the patch file here which may help: https://github.com/mezzi/magento-api-patches/blob/master/0001-fix-soap-api-configurable-product-options.patch

The API documentation is incomplete. http://devdocs.magento.com/guides/m1x/api/soap/checkout/cartProduct/cart_product.add.html

You require 'super_attribute' instead of 'options' when adding configurable products.

This is a dump from the quote object when adding products via the cart.

Mage_Sales_Model_Quote::addProduct->request=Varien_Object Object
(
    [_data:protected] => Array
        (
            [product_id] => 2002
            [qty] => 1
            [super_attribute] => Array
                (
                    [0] => Array
                        (
                            [207] => 1002
                        )
                )
        )

This is how your array should be structured.

$arrProducts = array(
    array(
        "product_id" => "1",
        "qty" => 2
        "super_attribute" => array(         
            optionId_1 => optionValue_1
        )
    )
);
$resultCartProductAdd = $proxy->call(
    $sessionId,
    "cart_product.add",
    array(
        $quoteId,
        $arrProducts
    )
);

Note that optionId_1 = the attribute_id and optionValue_1 = the attribute option value.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!