Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add option value to product, then to cart with Magento

Tags:

php

magento

I searched around for a while and only came up wit solutions that added whole new option sets to products in a Magento store.

What I'm trying to accomplish is a way to add a Simple Product to the cart. This Simple Product has some predifined custom options (free text fields) that has to be filled by a php function.

So, how can I do this? Let's say I have a product with the ID "111" and a one custom option.

$qty = '1';
$product = Mage::getModel('catalog/product')->load("111");
// set option value in product model?

$cart = Mage::helper('checkout/cart')->getCart();
$cart->addProduct($product, $qty);
// set option value while passing product to car?

$cart->save();

Thanks in advance for any hinds.

BTW: setting option values via QueryString is relativly easy as seen here.

like image 409
NiBa Avatar asked Jan 25 '11 12:01

NiBa


3 Answers

You don't set the custom option on the product model, you pass it in through the second argument to $cart->addProduct($product, $params).

The set up we have for a project, that requires an external app to add to the Magento cart, is to use a $params array of the following format:

$params = array(
    'product' => 1, // This would be $product->getId()
    'qty' => 1,
    'options' => array(
        34 => "value",
        35 => "other value",
        53 => "some other value"
    )
);

The $params['options'] contains the custom option information. The keys are the custom option ids, you can see them if you inspect the custom options section of the product screen with Firebug, or similar.

The $params['product'] may be redundant, I wrote this script a while ago for a much earlier version of Magento.

Also, I'm fairly sure that the standard add to cart events will fire when you add this way, so you'll need to set them off yourself. There may be side effects.

like image 111
Nick Avatar answered Oct 03 '22 01:10

Nick


In Magento 1.7 you have to wrap the params array in a Varien Object.

                $params = array(
                    'product' => $_fancypack->getId(),
                    'qty' => 1,
                    'options' => array(
                        $this->_getOptionId($_fancypack,'Product SKU') => $product->getId() .'/'. $product->getSku()
                    )
                );

                $request = new Varien_Object();
                $request->setData($params);

                $quote->addProduct($_fancypack, $request);
like image 28
Andre Aus B Avatar answered Oct 02 '22 23:10

Andre Aus B


You should write the input parameter for addproduct as the following format, it is tested by myself:

$params = array(
    'product' => 1, // This would be $product->getId()
    'qty' => 1,
    'super_attribute' => array(
        34 => "value",
        35 => "other value",
        53 => "some other value"
    )
);
like image 29
heghogbbb Avatar answered Oct 03 '22 01:10

heghogbbb