Magento - How to add multiple items to the cart programmatically? - magento

I'm trying to add multiple simple products to the cart at the same time using a query string as below, however it only adds the last product to the cart instead of both:
Can someone let me know what I'm doing wrong?
http://www.domain.co.uk/checkout/cart/add?product=9916&qty=4&product=15749&qty=4
I have also tried this:
http://www.domain.co.uk/checkout/cart/add?product[]=9916&qty[]=4&product[]=15749&qty[]=4
Any help much appreciated!

Add Product To Cart With Querystring
Add simple product in shopping cart with no attribute.
http://yourserver.com/checkout/cart/add?product=PRODUCT_ID&qty=PRODUCT_QUANTITY
Here PRODUCT_ID = 'Product Id',PRODUCT_QUANTITY = 'product quantity to purchase'.
Add product into shopping cart with single custome option.
http://yourserver.com/checkout/cart/add?product=PRODUCT_ID&qty=PRODUCT_QUANTITY&super_attribute[OPTION_ID]=OPTION_VALUE
Here OPTION_ID = 'Custom attribute option id',OPTION_VALUE = 'Custom attribute option value'.
Add product into shopping cart with multipal custome option.
http://yourserver.com/checkout/cart/add?product=PRODUCT_ID&qty=PRODUCT_QUANTITY&super_attribute[OPTION_ID_1]=OPTION_VALUE_1&super_attribute[OPTION_ID_2]=OPTION_VALUE_2
Here OPTION_ID_1 & OPTION_ID_1 = 'Custom attribute option ids',OPTION_VALUE_1 & OPTION_VALUE_2 = 'Custom attribute option values'.Here add more options in `super_attribute` array
Add Extra products with mail product with only 1 quantity.
http://yourserver.com/checkout/cart/add?product=PRODUCT_ID&qty=PRODUCT_QUANTITY&related_product=PRODUCT_ID_1,PRODUCT_ID_2
Here PRODUCT_ID_1 and PRODUCT_ID_2 is other products id. add more product by id using `,` separator. Example:- &related_product=1,2,3,4.
Default magento there is not setting for add related product quantity into cart.so if you want to add this code than open app/code/core/Mage/Checkout/controllers/CartController.php find public function addAction().
if (!empty($related)) {
$cart->addProductsByIds(explode(',', $related));
}
Replace with
$rel_qty = $this->getRequest()->getParam('related_qty');
if (!empty($related)) {
$relatedproducts = explode(',', $related);
$relatedqtys = explode(',',$rel_qty);
$i = 0;
foreach($relatedproducts as $relatedproduct)
{
$cart->addProduct($relatedproduct, array('qty'=>$relatedqtys[$i]));
$i++;
}
}
Now use query string for add related products with quantity.
http://yourserver.com/cart/add?product=PRODUCT_ID&qty=PRODUCT_QUANTITY&related_product=PRODUCT_ID_1,PRODUCT_ID_2&related_qty=PRODUCT_ID_1_QUANTITY,PRODUCT_ID_2_QUANTITY

If you don't want to change any code, you can try to utilize related products functionality by adding related_product parameter to your request. So your url will look like this:
http://www.domain.co.uk/checkout/cart/add?product=9916&qty=4&related_product=15749
If you want to add more products, just list them with comma separator: related_product=1,2,3
The only drawback from that is that you actually can't specify the qty for related products.
To see how it works - Mage_Checkout_Model_Cart::addProductsByIds(array_of_ids)
If qty for subsequent products is a mandatory for you, you'll need to create your own controller, or override the Mage_Checkout_CartController::addAction method.

I found a cheeky way I found of getting around the quantity limitation of the related_products query string field noted above in other answers. If you just put the SAME ID MULTIPLE TIMES in the value of related_products, as many times as the quantity you need, then that will achieve the same effect as if there was an explicit qty field for each related product. So taking himansu's answer above and adapting it we get:
http://yourserver.com/checkout/cart/add?product=PRODUCT_ID&qty=PRODUCT_QUANTITY&related_product=PRODUCT_ID_1,PRODUCT_ID_1,PRODUCT_ID_1,PRODUCT_ID_2,PRODUCT_ID_2
This will add to the cart PRODUCT_QUANTITY of PRODUCT_ID, 3 of PRODUCT_ID_1, and 2 of PRODUCT_ID_2.
So as long as you're happy doing a little work to generate the same ID multiple times this works a treat. And no custom code changes required on your magento server.

Related

value of specific group attributes of product

in manage attributes set say default i created a group say "my custom group" now i want to get value of all attributes under this group for current product how can i do this?..
$attributeValue = Mage::getModel('catalog/product')
->load($this->getProduct()->getId());
echo "<pre>";
print_r($attributeValue);
echo "</pre>";
show all attributes for product
Hello You can not fetch the Attributes values as per group.
Magento Follow the EAV Model.
So if you want to access the value form any Custom attribute value for that product then you have to follow the Magento Standard Way as below.
Syntax
$product=Mage::getModel('catalog/product')->load($Id);
Once you write above line the it will fetch all data for that product. Now you want to fetch the value of any attributes like name, color or any other attributes then use below code.
$product->getData('Attribute Code');
Or
$product->getName();
This way you can access the details of any attributes.
First thing:
$_prod = Mage::getModel('catalog/product')->load($id);
There are 2 kinds of attributes:
The ones in 'General' where you can use:
$_prod->getAttributeText('some_attribute_code')
The ones in a custom group where you use:
$_prod->getData('some_attribute_code')
The difference is the function you call getAttributeText or getData

Enable/disable cash-on-delivery only for some specific products

I want to disable cash on delivery payment method option for some specific products. I want to show cash on delivery method only for specific products and need to hide other payment options.
How can I do this? I read
this other question but it did not solve my problem.
you can use following free extension to solve your problem
http://www.magentocommerce.com/magento-connect/shipping-and-payment-filters.html
Using payment_method_is_active observer you could load the current checkout session quote and check what city the order is be shipped to.
$checkout = Mage::getSingleton('checkout/session')->getQuote();
$shipping = $checkout->getShippingAddress();
$cashOnDeliveryCities = array('city name 1','city name 2',..)
if(in_array($shipping->getCity(), $cashOnDeliveryCities)){
$result->isAvailable = true;
}else{
$result->isAvailable = false;
}
Here's how to do it without an extension.
Just go to admin > per motions > Shopping Cart Price Rule and create rules for this "cash on delivery payment method option for some specific products".
First in conditions select payment method "COD" in options. After that in "Action" add product SKU, for multiple using add SKU by comma. Activate the rule and check your payment method for those products to ensure it's working.

Magento 1 - How to get type of a Magento quote item?

Grouped products are grouping simple products, everyone knows. In somewhere of my custom module, I need to know if there's a simple product in my cart (quote) "added by grouped product" or by "itself"?
I know there's a table sales_flat_quote_item_option. Item records doesn't differ in sales_flat_quote_item but in that option table there's some difference. When you add your simple product with "grouped product add action", it creates a row including info_buyRequest in sales_flat_quote_item_option table.
I want to determine that record programatically.
Thanks for any help/directive.
I found a solution. Its enough simple to embarrass me;
$_item->getProductType()
returns "grouped" when that simple item came with a grouped product's add action. Altough, there's a logical mistake, because that product's type is actually "simple" not "grouped".
If you do not want to loop through all the products in the cart items what you could do would be to get the quote id and load the items directly from the database, including the product type filter.
/** #var Mage_Sales_Model_Quote $quote */
$quoteId = Mage::getModel('checkout/cart')->getQuote()->getId();
/** #var Mage_Sales_Model_Resource_Quote_Item_Collection $quoteItems */
$quoteItems = Mage::getModel('sales/quote_item')->getCollection();
$quoteItems->addFieldToFilter('quote_id', $quoteId);
$quoteItems->addFieldToFilter('product_type', 'bundle');
if ($quoteItems->getSize() >= 1) {
echo 'We have bundle products in the cart';
}

Magento 1.7: Add Configurable Product To Cart Via Query String

The Magento Wiki has a resource for adding a product to cart via Query String for Magento < 1.3 HERE
This quotes a method using this example:
http://www.your_domain.com/checkout/cart/add?product=68&qty=1&super_attribute[528]=55&super_attribute[525]=56
It also mentions that this was valid up to version 1.3.
I have been playing around with this in 1.7 and have noticed a Major difference in 1.7 is the encrypted key in the ->getAddUrl() method for the Form Action Attribtue so now the URLs look more like
http://www.your_domain.com.au/checkout/cart/add/uenc/aHR0cDovL3d3dy5jdWx0dXJla2luZ3MuY29tLmF1L2FjY2Vzc29yaWVzL3NvbC1yZXB1YmxpYy90cmFja3Mtb24tZWFyLWJsYWNrLTM1OTg5Lmh0bWw_X19fU0lEPVU,/product/35900/
With the product ID being the 35900.
If I use this URL in the browser it will direct me to the product page with a message saying Please specify the product's option(s).
I have been trying to pass the desired attribute options value in the URL to add the product to the cart with no success. (For the sake of saving space I'm omitting the URL up to and including the encrypted key) I've tried methods these to no avail:
/product/35900/super_attribute/49265/4834
/product/35900/super_attribute/49265=4834
/product/35900/49265=4834
/product/35900/49265/4834
My question is: Is it possible to add a configurable product via URL to the cart in Magento and if so, what is the format for passing the super_attribute id and Attribute Option Value?
You can use something like this :
$_typeInstance = $_product->getTypeInstance(true);
$_children = $_typeInstance->getUsedProducts(null, $_product);
$_attributes = $_typeInstance->getUsedProductAttributes($_product);
$_cartHelper = Mage::helper('checkout/cart');
foreach ($_children as $_child) {
$_superAttributes = array();
foreach ($_attributes as $_attribute) {
$_superAttributes[$_attribute->getAttributeId()] = $_child->getData($_attribute->getAttributeCode());
}
$_addUrl = $_cartHelper->getAddUrl($_product, array(
'_query' => array(
'super_attribute' => $_superAttributes
)));
}
This question was also posted on magento.stackexchange and user Marius kindly gave me the solution...
This has worked for me on CE 1.7.0.2 (with sample data):
/checkout/cart/add/product/126?super_attribute[525]=100&super_attribute[272]=22
NOTE (this puzzles me a bit):
There is a difference between calling:
/checkout/cart/add/product/126?super_attribute[525]=100&super_attribute[272]=22
and
/checkout/cart/add/product/126?super_attribute[272]=22&super_attribute[525]=100
I mean the order of the super_attribute parameters is important. After calling the 2 URLs above I ended up with 2 cart lines of the same product with the same options. one looked like this:
Size Small Color Green
and the other was
Color Green Size Small
I guess if you add the products to cart via URL you should keep the order of the attributes as shown in the product view page for consistency.
As per his suggestion, you can build the add to cart link using that method.
In latest magento we need to add form_key also:
https://{site-name}/checkout/cart/add/product/{product_id}/form_key/{form_key}?super_attribute[{attribute_id}]={attribute_value}&super_attribute[{attribute_id}]={attribute_value}

How to get attribute codes of super product attributes of a configurable product

For example, a configurable product with attributes Size and Color, I need to get the attribute codes of the above attributes.
or to be more specific, I need to know whether an attribute is used to configure a configurable product. I need this to check at product list page
try using this code
$config_product = Mage::getModel('catalog/product')->load($config_product_id);
$productAttributeOptions = $config_product->getTypeInstance(true)->getConfigurableAttributesAsArray($config_product);
Create an array as shown below:
$attributeValues['additional_options'][$count]['label'] = $aAttr['name'];
$attributeValues['additional_options'][$count]['value'] = $aAttr['value'];
Then pass the array while adding items to the order:
if (!empty($product['product_options'])) {
$orderItem->setProductOptions($product['product_options']);
}
Here $product['product_options'] is the array that we have created in the first step.

Resources