1

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! :)

4

2 回答 2

0

在将产品添加到购物车之前,您必须设置产品的自定义选项。

$product->setCustomOptions($options);

Mage_Sales_Model_Quote::_addCatalogProduct()定义选项中的 将被添加到购物车项目中。

另请参阅:http: //www.magentocommerce.com/boards/viewthread/49659/

顺便说一句:您的代码可能很慢,因为您在 foreach 循环中加载了两次产品。您应该考虑改用产品集合进行一些重构。$_SESSION此外,在这里直接访问变量看起来有点骇人听闻。您更愿意为此使用结帐会话 ( Mage::getSingleton('checkout/session'))。

于 2013-10-11T10:13:42.070 回答
0

经过很多头痛后,我现在已经解决了这个问题。您可以向购物车添加自定义选项,而不必实例化产品对象并保存自定义选项来执行此操作,这可以通过附加到观察者并拉取报价项目来完成。

加入后:sales_quote_add_item

然后我用:

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();
}

包含产品对象和 id 是最重要的,因为该函数由于某种原因不使用加载的对象。

然后,您可以通过以下方式获取对象:

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

快速方便的信息,如果它在您面前转储堆栈跟踪,则找不到定义的选项,丢失 getValue()(如果使用 var_dump)函数以查看您是否获得空值,否则 xdebug 将给出你有很多提示可以绕过它。

于 2013-10-11T14:51:48.713 回答