我正在尝试使用 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).
如有任何帮助或建议,我们将不胜感激。
最佳答案
我找到了一种通过 SOAP 将捆绑产品添加到购物车的方法。
键 bundle_option
中的值必须是选项( bundle /选择)的模型 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)
改变这个之后,添加任何捆绑产品就很简单了,如上所述
关于php - Magento Soap API 添加捆绑产品到购物车,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24177920/