Add a parameter value to the product, then to the cart with Magento

I searched around for a while and only came up with solutions that added whole new options for products in the Magento store.

What I'm trying to do is a way to add a Simple product to the cart. This simple product has some predefined user parameters (free text fields) that must be filled in using the php function.

So how can I do this? Let's say I have a product with an identifier of "111" and 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 asses.

BTW: setting parameter values ​​through a QueryString is relatively easy, as shown here .

+8
php magento
source share
4 answers

You do not set a custom option in the product model, you pass it through the second argument to $cart->addProduct($product, $params) .

For the project, which is required to be added to the Magento basket for an external application, you need to use the $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" ) ); 

$params['options'] contains information about custom parameters. Keys are custom parameter identifiers, you can see them if you check the custom settings section of the product screen using Firebug or similar.

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

In addition, I am sure that standard add to cart events will fire when this method is added, so you will need to disable them yourself. Side effects are possible.

+15
source share

In Magento 1.7, you need to wrap an array of params 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); 
+4
source share

You must write the input parameter for addproduct as the following format, it is being tested:

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

The problem with the current answer is that magento will not add a second position if the SKU is the same, but the parameters are different from the first. If you want a 3 apple and a 4 "apple you would like to have separate positions. Or at least I do it.

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 positions.

But still, it's only half the battle, what do these option codes mean? 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); } ?> 
0
source share

All Articles