[英]Programmatically Add multiple Products with Custom Options
I'm trying to develop a code in php which will take the order increment Id from an old order and will create a new order with same products along with the product custom options(if any). 我正在尝试在php中开发一个代码,该代码将从旧订单中获取订单增量ID,并将使用相同的产品以及产品自定义选项(如果有)创建一个新订单。 Fetching par is fine even it was possible to add product via querystring but by that I can't add custom options.
即使可以通过querystring添加产品,但获取par还是可以的,但是我不能添加自定义选项。 The cart page shows : Product Custom Options Required.
购物车页面显示:必需的产品自定义选项。
I think(not sure) I even succeeded in adding products with custom options via soap api but when I redirect to cart it shows an empty cart. 我认为(不确定)我什至成功地通过soap api添加了带有自定义选项的产品,但是当我重定向到购物车时,它显示了一个空购物车。
I'm a beginner, so please ignore any stupidity if it exists. 我是一个初学者,因此请忽略任何愚蠢(如果存在)。 :D The code with the querystring is as below:
:D带有查询字符串的代码如下:
$form_key = Mage::getSingleton('core/session')->getFormKey();
$arrProducts = array(
/*array
(
"product" => "3",
"qty" => 7
),*/
array
(
"product" => "1",
"qty" => 3,
"options" => array
(
2 => array(
"date" => "11/12/13",
"date_internal" => "2013-12-11 00:00:00"
),
1 => "1"
)
header('Location: '.Mage::getUrl('checkout/cart/add/form_key/'.$form_key.'/', $arrProduct));
So, this directs to a page with the product but asks me to chose the product options. 因此,这将转到产品页面,但要求我选择产品选项。 and when i use the other product which is commented above, its a success.
当我使用上面提到的其他产品时,它是成功的。
Even i'm now able to add a product without custom(product with id='3') option in the cart. 甚至我现在也可以在购物车中添加没有自定义(id ='3'的产品)选项的产品。 code is as follows:
代码如下:
<?php
require('../../../app/Mage.php'); //include mage.php
Mage::app();
Mage::getSingleton('core/session', array('name' => 'frontend'));
$form_key = Mage::getSingleton('core/session')->getFormKey();
//echo $form_key;
$cart = Mage::getSingleton('checkout/cart');
$product = new Mage_Catalog_Model_Product();
$product->load(2);
//$product->setProductOptions();
$params = array(
array
(
"product" => "3",
"qty" => 1
),
array
(
"product" => "2",
"qty" => 2,
"options" =>array
(
2 => array
(
"date" => "11/12/2013",
"date_internal"=> "2013-12-11 00:00:00"
),
1 => "1"
)
)
);
$cart->addProduct($product, $params);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
header('Location:http://111.111.1.1/magento/checkout/cart');
?>
but it doesn't work for any product with custom options(product with id='2'). 但不适用于具有自定义选项(id ='2'的产品)的任何产品。 Error Message :
错误信息 :
Fatal error: Uncaught exception 'Mage_Core_Exception' with message 'Please specify the product required option(s).' 致命错误:消息为“请指定产品所需的选项”的未捕获异常“ Mage_Core_Exception”。 in C:\\xampp\\htdocs\\magento\\app\\Mage.php:594 Stack trace: #0 C:\\xampp\\htdocs\\magento\\app\\code\\core\\Mage\\Checkout\\Model\\Cart.php(284): Mage::throwException('Please specify ...') #1 C:\\xampp\\htdocs\\magento\\pratmagento\\panel\\orders\\try2addtocart.php(53): Mage_Checkout_Model_Cart->addProduct(Object(Mage_Catalog_Model_Product), Array) #2 {main} thrown in C:\\xampp\\htdocs\\magento\\app\\Mage.php on line 594
在C:\\ xampp \\ htdocs \\ magento \\ app \\ Mage.php:594中堆栈跟踪:#0 C:\\ xampp \\ htdocs \\ magento \\ app \\ code \\ core \\ Mage \\ Checkout \\ Model \\ Cart.php(284): Mage :: throwException('请指定...')#1 C:\\ xampp \\ htdocs \\ magento \\ pratmagento \\ panel \\ orders \\ try2addtocart.php(53):Mage_Checkout_Model_Cart-> addProduct(Object(Mage_Catalog_Model_Product),Array)# 2 {main}在第594行的C:\\ xampp \\ htdocs \\ magento \\ app \\ Mage.php中引发
必须设置产品的website_ids。
$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.