简体   繁体   English

如何以编程方式将带有自定义选项的产品添加到Magento中的购物车?

[英]How to programmatically add a product with custom options to cart in Magento?

Custom options will be required and it's a dropdown type: title is donation price is 1 price type is fixed 需要自定义选项,它是一个下拉类型:标题是捐赠价格是1价格类型是固定的

Right now , it's adding that product to the cart but without custom options. 现在,它将产品添加到购物车中,但没有自定义选项。 Here is a screenshot of custom options. 这是自定义选项的屏幕截图。

https://s3.amazonaws.com/uploads.hipchat.com/62230/429611/n0AxrLBapiJZo3t/Screen%20Shot%202014-05-23%20at%206.09.59%20PM.png https://s3.amazonaws.com/uploads.hipchat.com/62230/429611/n0AxrLBapiJZo3t/Screen%20Shot%202014-05-23%20at%206.09.59%20PM.png

   $id = '67'; // Replace id with your product id
                $qty = '1'; // Replace qty with your qty
                $_product = Mage::getModel('catalog/product')->load($id);

                $cart = Mage::getModel('checkout/cart');
                $cart->init();

                $params = array(
                    'product'=>$id,
                    'qty' => $qty,
                    'options' => array (
                        183 => array(
                            array(
                                'price'=> 1.00,
                                'title'=>'$1.00 Donation',
                                'price_type' => 'fixed',
                                'sku' => '',
                                'sort_order' => 10
                            )
                        )
                    )
                );

                $request = new Varien_Object();
                $request->setData($params);

                $cart->addProduct($_product, $request );
                $cart->save();
                Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

What's in that product variable dump 该产品变量转储中包含什么

 [_resourceName:protected] => catalog/product_option_value
                                [_resource:protected] => 
                                [_resourceCollectionName:protected] => catalog/product_option_value_collection
                                [_cacheTag:protected] => 
                                [_dataSaveAllowed:protected] => 1
                                [_isObjectNew:protected] => 
                                [_data:protected] => Array
                                    (
                                        [option_type_id] => 641
                                        [option_id] => 183
                                        [sku] => 
                                        [sort_order] => 10
                                        [default_title] => $1.00 Donation
                                        [store_title] => 
                                        [title] => $1.00 Donation
                                        [default_price] => 1.0000
                                        [default_price_type] => fixed
                                        [store_price] => 
                                        [store_price_type] => 
                                        [price] => 1.0000
                                        [price_type] => fixed

If you have already a quote id then used 如果您已经有报价ID,则使用

below.. 下面..

For getting option id and option value check this link: Magento - Get Product options from $item 要获取选项ID和选项值,请查看以下链接: Magento-从$ item获取产品选项

Here you need to fetch product drop down option value If you need to create new quote then used add below 在这里,您需要获取产品下拉选项的值。如果您需要创建新的报价,请在下面使用添加

$QuoteId= Mage::getModel('checkout/cart_api')->create('default');

Here default is store code . 这里defaultstore code IF you have already a quote id then not need to add upper code 如果您已经有报价ID,则无需添加大写代码

$arrProducts = array(
    array(
        "product_id" => $productId,
        "qty" => 5,
        "options" => array(         
                $optionId => $optinValueId
                 )
    )
);
Mage::getModel('checkout/cart_product_api')->add($QuoteId,$arrProducts,$storeId);

I have done below code to get custom option. 我已经完成以下代码以获取自定义选项。 May be it will help you to set custom option: 可能会帮助您设置自定义选项:

<?php 


        $productSku = "330471";
        $product = Mage::getModel('catalog/product');
        $productId = $product->getIdBySku( $productSku );
        $product->load($productId);

        if ($product->getId()) {
            if ($product->hasCustomOptions()) {
                foreach ($product->getOptions() as $o) {
                    $optionType = $o->getType();
                    Mage::log("Option Type = $optionType;");

                    if ($optionType == 'drop_down') {
                        $values = $o->getValues();
                        Mage::log("List of Drop down Custom Options:-");

                        foreach ($values as $k => $v) {
                            Mage::log("Array Key = $k;");
                            Mage::log("Array Values:-");
                            Mage::log($v);
                        }
                    }
                    else {
                        Mage::log("List of General Custom Options:-");
                        Mage::log($o);
                    }
                }
            }
        }
        else {
            Mage::log('This Product does not exist.');
        }

        $product = Mage::getModel("catalog/product")->load(939); //product id 1
        $i = 1;
        echo "<pre>";
        foreach ($product->getOptions() as $o) {
            echo "<strong>Custom Option:" . $i . "</strong><br/>";
            echo "Custom Option TITLE: " . $o->getTitle() . "<br/>"; //Colors
            echo "Custom Option TYPE: " . $o->getType() . "<br/>"; //drop_down
            echo "Custom Option Values: <br/>";
            $values = $o->getValues();
            foreach ($values as $v) {
                print_r($v->getData());
            }
            $i++;
            echo "<br/>";
        }

?>

try the below code with your code 尝试以下代码与您的代码

 $params = array( 'product' => 164, 'related_product' => null, 'bundle_option' => array( 21 => 58, 20 => 55, 11 => 28, 12 => array( 0 => 31, ), 13 => array( 0 => 32, 1 => 35, ), ), 'options' => array( 3 => 'olaaaaaaaa', ), 'qty' => 2, ); $cart = Mage::getSingleton('checkout/cart'); $product = new Mage_Catalog_Model_Product(); $product->load(164); $cart->addProduct($product, $params); $cart->save(); Mage::getSingleton('checkout/session')->setCartWasUpdated(true); $message = $this->__('Custom message: %s was successfully added to your shopping cart.', $product->getName()); Mage::getSingleton('checkout/session')->addSuccess($message); 

 $params = array( 'product' => 164, 'qty' => 2, ); $cart = Mage::getSingleton('checkout/cart'); $product = new Mage_Catalog_Model_Product(); $product->load(164); $cart->addProduct($product, $params); $cart->save(); Mage::getSingleton('checkout/session')->setCartWasUpdated(true); $message = $this->__('Custom message: %s was successfully added to your shopping cart.', $product->getName()); Mage::getSingleton('checkout/session')->addSuccess($message); 

Let me know if you have any query 让我知道您是否有任何疑问

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Magento 2使用自定义选项以编程方式将产品添加到购物车 - Magento 2 add product to cart programmatically with custom options 马杰托。 如何使用自定义选项以编程方式在购物车中添加简单产品 - Magento. How to add simple product in cart programmatically with custom options 如何将自定义产品添加到magento购物车-请说明这些选项 - How to add a custom product to magento cart - Please explain these options 如何在Magento中以编程方式将产品添加到购物车 - How to add product to cart programmatically in Magento 以编程方式将产品添加到Magento 2中的购物车 - Programmatically add product to cart in Magento 2 Magento Soap API在购物车中添加具有自定义选项值的产品 - Magento soap api add product with custom options value in shopping cart Magento 2.3.5:使用自定义选项和价格将产品添加到购物车 - Magento 2.3.5: Add product to cart with custom options and price Magento。 如何以编程方式为自定义选项(复选框)设置数据并在购物车中添加简单产品? - Magento. How to set data for custom options(checkbox) programatically and add simple product in cart? 在Magento中以编程方式将确切1种产品添加到购物车 - Add exactly 1 product to cart programmatically in Magento 如何在可配置产品页面上添加自定义空购物车按钮:Magento - How to add a custom empty cart button on configurable product page:Magento
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM