douchensou6495 2013-10-11 09:55
浏览 37
已采纳

向订单行添加自定义选项

I'm currently trying to add a custom option to a specific orderline on add to cart via the following:

public function addToPackageQuote()
{
    $cart = Mage::getSingleton("checkout/cart");
    $quote = Mage::getSingleton("checkout/session")->getQuote();
    $packageId = Mage::getModel('MyTuxedo_OPP/Package')->checkPackageId();
    $products = $this->sortArray();
    foreach ($products as $productInfo) {
        try {
            $split = explode(",", $productInfo);
            $_product = Mage::getModel('catalog/product')->load($split[0]);
            if($_product->isConfigurable()) {
                $simpleId = $this->getConfigurableSimple($split[1],$split[3],$split[0]);
            } else {
                $simpleId = $split[0];
            }
            $product = Mage::getModel('catalog/product')->load($simpleId);
            $options = new Varien_Object(array(
                "qty" => 1,
                "custom_options" => array(
                    "package" => $packageId,
                    "packageName" => Mage::helper('MyTuxedo_OPP')->getPackageName()
                )
            ));
            $quote->addProduct($product, $options);
            $this->_getSession()->setCartWasUpdated(true);
            $quote->save();
        } catch (Exception $e) {
            echo $e->getMessage();
        }
        $this->addFreeItems();
    }
    $cart->save();
    unset($_SESSION['products']);
    unset($_SESSION['productId']);
    $cart->save();
    // Let's unset all the package sessions (apart from a few that are needed!).
    $this->kill();
}

This method is completely seperate from the generic add to cart handler, and is used purely in a packages system so that it adds simple products exclusively (also breaks down configurables super attribute to find the simple product too).

These simple products have no custom options attached to them in the Magento backend, nor is it a goal to add custom options to the product itself. What I would like to do is attach custom options to the order-line that is then transferred over to the order if a purchase is made. So effectively data that is added at the add to cart method and no where else!

The add to cart method works as expected it's just not including the custom options I am trying to attach. I have also tried defining the options object as simply:

$options = new Varien_Object(array(
"qty" => 1,
"package" => $packageId,
"packageName" => Mage::helper('MyTuxedo_OPP')->getPackageName()
 )

The above info, not including qty is not in the orderline object at all, and I can't seem to work out where to move on from here.

Endlessly googling at the moment so some help would be most appreciated!!

I do appreciate I’m instantiating the product model object twice in this, however the plan is to just get it working then optimise! :)

  • 写回答

2条回答 默认 最新

  • doudizhu2222 2013-10-11 14:51
    关注

    I have now resolved this, after much headache. You can add a custom option to the cart and not have to instantiate the product object and save a custom option to do this, it can be done via tacking onto an observer, and pulling the quote items.

    After tacking onto: sales_quote_add_item

    I then used:

    public function addCustomData($observer) {
    $event = $observer->getEvent();
    $quote_item = $event->getQuoteItem();
    $quote = $session->getQuote();
    $quote_item->addOption(array("product_id" => $quote_item->getProduct()->getId(),
                                 "product" => $quote_item->getProduct(),
                                 "code" => 'PackageId',
                                 "value" => Mage::getModel('MyTuxedo_OPP/Package')->checkPackageId()
                           ));
    $quote->save();
    }
    

    It is most important to include the product object and id, as the function doesn't use the loaded object for some reason.

    You can then get at the object via:

    $_item->getOptionByCode('PackageId')->getValue();
    

    Quick piece of handy info, if it dumps a stack trace in front of you it can't find the defined option, lose the getValue() (if using var_dump) function to see if you are getting a null value, otherwise xdebug will give you a ton of hints to get around it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集