Reputation: 563
Custom options will be required and it's a dropdown type: title is donation price is 1 price type is fixed
Right now , it's adding that product to the cart but without custom options. Here is a screenshot of custom options.
$id = '67'; // Replace id with your product id
$qty = '1'; // Replace qty with your qty
$_product = Mage::getModel('catalog/product')->load($id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$params = array(
'product'=>$id,
'qty' => $qty,
'options' => array (
183 => array(
array(
'price'=> 1.00,
'title'=>'$1.00 Donation',
'price_type' => 'fixed',
'sku' => '',
'sort_order' => 10
)
)
)
);
$request = new Varien_Object();
$request->setData($params);
$cart->addProduct($_product, $request );
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
What's in that product variable dump
[_resourceName:protected] => catalog/product_option_value
[_resource:protected] =>
[_resourceCollectionName:protected] => catalog/product_option_value_collection
[_cacheTag:protected] =>
[_dataSaveAllowed:protected] => 1
[_isObjectNew:protected] =>
[_data:protected] => Array
(
[option_type_id] => 641
[option_id] => 183
[sku] =>
[sort_order] => 10
[default_title] => $1.00 Donation
[store_title] =>
[title] => $1.00 Donation
[default_price] => 1.0000
[default_price_type] => fixed
[store_price] =>
[store_price_type] =>
[price] => 1.0000
[price_type] => fixed
Upvotes: 1
Views: 18536
Reputation: 1175
try the below code with your code
$params = array(
'product' => 164,
'related_product' => null,
'bundle_option' => array(
21 => 58,
20 => 55,
11 => 28,
12 => array(
0 => 31,
),
13 => array(
0 => 32,
1 => 35,
),
),
'options' => array(
3 => 'olaaaaaaaa',
),
'qty' => 2,
);
$cart = Mage::getSingleton('checkout/cart');
$product = new Mage_Catalog_Model_Product();
$product->load(164);
$cart->addProduct($product, $params);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
$message = $this->__('Custom message: %s was successfully added to your shopping cart.', $product->getName());
Mage::getSingleton('checkout/session')->addSuccess($message);
$params = array(
'product' => 164,
'qty' => 2,
);
$cart = Mage::getSingleton('checkout/cart');
$product = new Mage_Catalog_Model_Product();
$product->load(164);
$cart->addProduct($product, $params);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
$message = $this->__('Custom message: %s was successfully added to your shopping cart.', $product->getName());
Mage::getSingleton('checkout/session')->addSuccess($message);
Let me know if you have any query
Upvotes: 0
Reputation: 7611
If you have already a quote id then used
below..
For getting option id and option value check this link: Magento - Get Product options from $item
Here you need to fetch product drop down option value If you need to create new quote then used add below
$QuoteId= Mage::getModel('checkout/cart_api')->create('default');
Here default
is store code
.
IF you have already a quote id then not need to add upper code
$arrProducts = array(
array(
"product_id" => $productId,
"qty" => 5,
"options" => array(
$optionId => $optinValueId
)
)
);
Mage::getModel('checkout/cart_product_api')->add($QuoteId,$arrProducts,$storeId);
Upvotes: 2
Reputation: 106
I have done below code to get custom option. May be it will help you to set custom option:
<?php
$productSku = "330471";
$product = Mage::getModel('catalog/product');
$productId = $product->getIdBySku( $productSku );
$product->load($productId);
if ($product->getId()) {
if ($product->hasCustomOptions()) {
foreach ($product->getOptions() as $o) {
$optionType = $o->getType();
Mage::log("Option Type = $optionType;");
if ($optionType == 'drop_down') {
$values = $o->getValues();
Mage::log("List of Drop down Custom Options:-");
foreach ($values as $k => $v) {
Mage::log("Array Key = $k;");
Mage::log("Array Values:-");
Mage::log($v);
}
}
else {
Mage::log("List of General Custom Options:-");
Mage::log($o);
}
}
}
}
else {
Mage::log('This Product does not exist.');
}
$product = Mage::getModel("catalog/product")->load(939); //product id 1
$i = 1;
echo "<pre>";
foreach ($product->getOptions() as $o) {
echo "<strong>Custom Option:" . $i . "</strong><br/>";
echo "Custom Option TITLE: " . $o->getTitle() . "<br/>"; //Colors
echo "Custom Option TYPE: " . $o->getType() . "<br/>"; //drop_down
echo "Custom Option Values: <br/>";
$values = $o->getValues();
foreach ($values as $v) {
print_r($v->getData());
}
$i++;
echo "<br/>";
}
?>
Upvotes: 0