Magento Soap API将捆绑产品添加到购物车

时间:2014-06-12 06:36:51

标签: php magento soap

我尝试使用Magento SOAP API v1创建订单,并在将捆绑产品添加到购物车时遇到问题。我能够通过简单的产品正确地获得订单,但我对添加捆绑产品感到困惑。

// The Products Array with Bundle
$products = array(
        array(
            "product_id" => "38914",
            "qty" => "1",
            "bundle_option" => array(
                "18194" => "20360",
            ),
            "related_product" => null,
            "bundle_qty" => array("20360" => "1"),
            "options" => array(
                "0" => array(
                    "key" => "3328",
                    "value" => "4494",
                ),
                "1" => array(
                    "key" => "3329",
                    "value" => null,
                ),
                "2" => array(
                    "key" => "3339",
                    "value" => null,
                ),

            )
        )
    );

// Get an API session
$client = new \SoapClient('magentoinstallation/index.php/api/soap/?wsdl');
$session = $client->login('user', 'password');

//Create the Cart
$cart = $client->call( $session, 'cart.create');

// add the products
$resultCartProductsAdd = $client->call( $session, "cart_product.add", array(     $cart, $products ) );

我尝试了很多不同的格式并遇到错误

Selected required options are not available

Please specify product option(s).

非常感谢任何帮助或建议。

1 个答案:

答案 0 :(得分:6)

我想出了一种通过SOAP将捆绑产品添加到购物车的方法。

bundle_option中的值必须是选项(包/选择)(不是产品ID)的模型的ID。键必须是选项的ID(我假设在您的示例中已经正确)

$products = array(
    array(
        "product_id" => "38914",
        "qty" => "1",
        "bundle_option" => array(
            "18194" => "20360", // <-- THE VALUE MUST BE THE ID OF THE CORRESPONDING "bundle/selection" MODEL, INSTEAD OF THE PRODUCT'S ID
        ),
// ...
);

此外,捆绑数量的关键字应为bundle_option_qty,而不是bundle_qty

捆绑产品的可用性可能会影响您的流程,因此请确保产品全部畅销。


我使用magento和此代码段的示例数据成功尝试了

$client = new \SoapClient('magentoinstallation/index.php/api/soap/?wsdl');
$session = $client->login('testuser', 'password');

$products = array(
    array(
        "product_id"    => 158,
        "qty"           => "1",
        "bundle_option" => array(
            1               => 2, // 1 is the option id, 2 is the bundle/selection id
        ),
    )
);

$cart = $client->call($session, 'cart.create', array('default'));
$resultCartProductsAdd = $client->call($session, "cart_product.add", array($cart, $products));

我重复了我的回答,发现了一个重要的要点。

选择商店; - )

只需使用

$cart = $client->call($session, 'cart.create', array('default'));

而不是

$cart = $client->call($session, 'cart.create');

(有关详细信息,请查看API规范:http://www.magentocommerce.com/api/soap/checkout/cart/cart.create.html

更改后,添加任何捆绑产品很简单,如上所述