在Magento中通过SOAP将产品添加到购物车时设置自定义选项


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

我正在尝试使用购物车产品添加SOAP API将带有自定义选项的产品添加到购物车。

下面是我为 products 参数传递的数组。我有一个自定义选项 id 1,下拉列表中的选定值 id 为 2。(您可以在此处查看产品)

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

该产品已添加到购物车中,但是当我检索购物车详细信息/总计时,它没有反映自定义选项。我还手动检查了在sales_flat_quote_itemsales_flat_quote_item_option表中创建的条目,但行没有任何与自定义选项相关的数据或定价。

我做错了什么?


更新日期: 12/11/2013

我已将自定义选项更改为"必需"。现在,当我尝试上述SOAP请求时,它给了我一个"请指定所需的产品选项"错误。看起来它只是忽略了我在数组中的选项键。

经过大量的调试和摆弄,事实证明"选项"必须作为 associaciativeArray 传递,在 SOAP 术语中需要定义如下:

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

有关此格式的更多信息,请单击此处 - https://stackoverflow.com/a/8963453/515268

使用此格式,我能够通过 SOAP 成功添加具有自定义选项的产品。购物车信息和总计中的定价也反映了预期价格。

在深入研究核心文件后,我发现了问题和一种简单的修补方法。

问题在于,"cart_product.add"/"shoppingCartProductAdd"的SOAP API接受一系列产品选项和带有键"选项"的超级属性,正如您上面所做的那样,但是准备要添加到购物车的产品的代码使用键"super_attribute"来查找此信息。为了打补丁,我只是将"选项"数组复制到 cart_product.add api 中的"super_attribute"数组中。

我把补丁文件放在这里,这可能会有所帮助:https://github.com/mezzi/magento-api-patches/blob/master/0001-fix-soap-api-configurable-product-options.patch

API 文档不完整。http://devdocs.magento.com/guides/m1x/api/soap/checkout/cartProduct/cart_product.add.html

添加可配置产品时,您需要"super_attribute"而不是"选项"。

这是通过购物车添加产品时报价对象的转储。

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

这就是数组的结构。

$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
    )
);

请注意,optionId_1 = attribute_id,optionValue_1 = 属性选项值。