我想在购物车中添加带有自定义标签的产品

时间:2015-01-31 16:33:15

标签: magento magento-1.9

我尝试过多种方法以编程方式将产品添加到购物车,但我想显示报价项标签以及一个购物车页面。任何人都可以帮我做这个或任何参考吗?

2 个答案:

答案 0 :(得分:1)

您可以在观察者中使用以下代码。

        $item = ( $item->getParentItem() ? $item->getParentItem() : $item); 

        $additionalOptions = array(array(
             'code' => 'option_code',
             'label' => 'Some_option_Label',
             'value' => 'Option_Value'
        ));
        $item->addOption(array(
             'code' => 'additional_options',
             'value' => serialize($additionalOptions),
        ));

        // Enable super mode on the product.
        $item->getProduct()->setIsSuperMode(true);

答案 1 :(得分:0)

这是您要查找的代码;)

<?php

    $loadProductData = Mage::getModel('catalog/product')->load($productId);
    $quote = Mage::getSingleton('checkout/session')->getQuote();
    $OrderquoteItem = Mage::getModel('sales/quote_item');
    $quoteItem = $OrderquoteItem->setProduct($loadProductData);
    //custom options to show user on cart page
    $a_options = array(
      'options' => array(
         'label' => 'OptionLabel :',
         'value' => "OptionaValue",
      ));
        //add above options array to this cart item which is going to get added on cart
    $quoteItem->addOption(array(
          'code' => 'additional_options',
          'value' => serialize($a_options),
      ));
     // set price and quantity
    $quoteItem->setQuote($quote)
        ->setQty($productOptions['qty'])
        ->setOriginalCustomPrice($productOptions['price'])
        ->save();

    Mage::getSingleton('checkout/session')->setCartWasUpdated(true);