简体   繁体   English

以编程方式添加具有自定义选项的多个产品

[英]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.

相关问题 Magento通过编程导入产品添加自定义选项 - Magento add custom options with programmatically importing products Magento 2使用自定义选项以编程方式将产品添加到购物车 - Magento 2 add product to cart programmatically with custom options 使用PHP代码以编程方式将多个新产品添加到网站woocommerce - Add multiple New products to website programmatically woocommerce Using PHP code Magento删除产品自定义选项 - Magento remove products custom options 使用自定义选项以编程方式创建产品 - Create Products Programatically with Custom Options 马杰托。 如何使用自定义选项以编程方式在购物车中添加简单产品 - Magento. How to add simple product in cart programmatically with custom options 如何以编程方式将带有自定义选项的产品添加到Magento中的购物车? - How to programmatically add a product with custom options to cart in Magento? 在 WooCommerce 中以编程方式添加多个产品 - Adding multiple products programmatically in WooCommerce WooCommerce 将多个产品的自定义重定向添加到购物车 - WooCommerce Add to Cart custom redirection for multiple products Variations 在 WooCommerce 特定 ID 产品页面上的多个产品中添加自定义内容 - Add custom content into multiple products on WooCommerce specific IDs product pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM