Add option value to product, then to cart with Magento

Please LogIn to Reply!

Post By: admin | 26-01-2012 06:35 Reply

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.

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.

The problem with the current answer is that magento will not add a second line item if the SKU is the same but the options are distinct from the first. If you want a 3" apple and a 4" apple you would like to have separate line items. Or at least I do.

A HTTP call to the following URL

/store/checkout/cart/add?product=23&qty=1&options[41]=4

followed by

/store/checkout/cart/add?product=23&qty=1&options[41]=3

will add two line items.

But still this is just half of the battle, what do these option codes stand for?? Well the following PHP code will tell you. And since we are using an HTTP call the code will return javascript ready JSON.

<?php     
include_once '../app/Mage.php';
Mage::app();
echo getProductOptionsIds($_GET['eventcode']);

function getProductOptionsIds($sku) 
{

    $ProductID = Mage::getModel('catalog/product')->getIdBySku($sku);
    $Product = Mage::getModel('catalog/product')->load($ProductID);

    $config = array();
    $config['ProductID'] = $ProductID;

    foreach ($Product->getOptions() as $option) {
          // @var $option Mage_Catalog_Model_Product_Option 
        if ($option->getGroupByType() == Mage_Catalog_Model_Product_Option::OPTION_GROUP_SELECT) {
            $_tmpValues = array();
            foreach ($option->getValues() as $value) {
                // @var $value Mage_Catalog_Model_Product_Option_Value 
                $_tmpValues[$value->getTitle()] = $value->getId();
            }

            $config[$option->getTitle().'list'] = $option->getId();
            $optionValue = $_tmpValues;
        } else {
            $optionValue = $option->getId();
        }
        $config[$option->getTitle()] = $optionValue;
    }
    return json_encode($config);
}
?>
`Reply Or Comment!
Please LogIn to Reply!