Domanda

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

È stato utile?

Soluzione 2

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.

Altri suggerimenti

You have to set the custom options for the product before adding it to cart.

$product->setCustomOptions($options);

The in Mage_Sales_Model_Quote::_addCatalogProduct() the custom options will be added to the cart item.

See also here: http://www.magentocommerce.com/boards/viewthread/49659/

By the way: Your code may be pretty slow because you are loading products twice in a foreach loop. You should consider some refactoring by using the product collection instead. Also it looks kind of hackish to directly access the $_SESSION variable here. You could rather use the Checkout Session for that (Mage::getSingleton('checkout/session')).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top