简体   繁体   English

添加到购物车中Magento中的产品自定义选项

[英]Add to cart with product custom option in Magento

I have a virtual product with custom option (type-1 and type-2). 我有一个带有自定义选项(类型1和类型2)的虚拟产品。 Price may change depend on type. 价格可能会因类型而异。 How can I add to cart with this option directly and change the price depend on custom option. 如何直接使用此选项添加到购物车并根据自定义选项更改价格。 I tried this code but it doesn't work. 我试过了这段代码,但是没有用。

<?php
// the ID of the product
$product_id  = "123";

$product     = Mage::getModel('catalog/product')->load($product_id);

$cart = Mage::getModel('checkout/cart');
$cart->init();
$params = array(
    'product' => $product_id,
    'qty' => 1,
    'options' => array(
        'options' => array(
            '7462' => 'Type Option Id' ,
            '3731' => 'Type-1',
        )

    )
);

$cart->addProduct($product, $params);
$cart->save();

Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
$this->_redirect('checkout/cart');

?>

Please try following code to add option to your product : 请尝试以下代码为您的产品添加选项:

$quoteItem->addOption(new Varien_Object(
    array(
        'product' => $quoteItem->getProduct(),
        'code' => 'additional_options',
        'value' => serialize($a_options)
    )
));

Ref Here : add product to cart with custom options 在这里引用: 使用自定义选项将产品添加到购物车

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

相关问题 添加到购物车中带有magento虚拟产品的自定义选项 - add to cart with custom option for virtual product in magento 将具有multiselect自定义选项的产品添加到magento中的购物车 - Add a product with multiselect custom option to the cart in magento Magento使用即时生成的产品自定义选项将产品添加到购物车 - Magento Add product to cart with on-fly generated product custom option 将选项值添加到产品,然后使用Magento添加到购物车 - Add option value to product, then to cart with Magento 用户将产品添加到购物车时,Magento自定义选项值和选项标题存储 - Magento custom option value and option title store when the user add product to cart Magento 2使用自定义选项以编程方式将产品添加到购物车 - Magento 2 add product to cart programmatically with custom options Magento 2在未创建购物车的情况下添加自定义选项 - Magento 2 add custom option in cart without create 我们如何在Magento中使用自定义选项类型文件将产品添加到购物车中? - How can we add in code a product to the cart with a custom option type file in Magento? 自动在magento中向新产品添加“自定义选项” - Auto add a 'Custom Option' to a new product in magento 如何在可配置产品页面上添加自定义空购物车按钮:Magento - How to add a custom empty cart button on configurable product page:Magento
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM