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

走远了吗. 提交于 2020-01-11 10:24:14

问题


I am trying to use the shoppingCartProductAdd SOAP API to add a product with Custom Options to cart.

Below is the array I am passing for the products parameter. I've a custom option id 1 for which the selected value id in the dropdown is 2. (you can view the product here)

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

This product gets added to the cart but when I retrieve the cart details / totals, it does not reflect the custom options. I've also manually checked the entry created in the sales_flat_quote_item and the sales_flat_quote_item_option tables, but rows do not have any data or pricing related to the custom option.

What am I doing wrong?


Update: 12/11/2013

I've changed the custom option to be "required". Now when I try the above SOAP request, it gives me a "Please specify the product required option(s)." error. Looks like it is just ignoring my options key in the array.


回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/19899127/setting-custom-options-while-adding-a-product-to-cart-via-soap-in-magento

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