How to add a product with custom options into Magento shopping cart from an external site?

In: Feeling guru

4 Jun 2010
Advertisement

This is a follow up post to a previous article I wrote on how to add products into Magento but this time the script includes custom options (attributes) of the product. I used Magento version 1.4.0.1 in this example. Just copy the whole script and save it as index.php.

Advertisement

<?php
require_once ("/var/www/your-magento-directory/app/Mage.php");
umask(0);

// Initialize Magento
Mage::app("default");

// You have two options here,
// "frontend" for frontend session or "adminhtml" for admin session
Mage::getSingleton("core/session", array("name" => "frontend"));

// get the current Magento cart
$cart = Mage::getSingleton('checkout/cart');

if(isset($_POST['submit'])){

	// call the Magento catalog/product model
	$product = Mage::getModel('catalog/product')
					 // set the current store ID
					 ->setStoreId(Mage::app()->getStore()->getId())
					 // load the product object
					 ->load($_POST['product']);

	// start adding the product
	// format: addProduct(<product id>, array(
	//         'qty' => <quantity>,
	//         'super_attribute' => array(<attribute id> => <option id>)
	//     )
	// )
	$cart->addProduct($product, array(
			'qty' => $_POST['qty'],
			'super_attribute' => array( key($_POST['super_attribute']) => $_POST['super_attribute'][525] )
			)
		);

	// save the cart
	$cart->save();

	// very straightforward, set the cart as updated
	Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

	// redirect to index.php
	header("Location: index.php");

}else{
?>
<html>
<head></head>
<body>
<div style="width: 400px; margin: auto">
	<h3>Very Nice T-shirt</h3>
	<hr>
	<div style="width: 180px; margin:auto; line-height: 30px">
		<form action="index.php" method="post">
			Size:
			<select name="super_attribute[525]">
				<option value="">Choose an option...</option>
				<option value="100">Small</option>
				<option value="99">Medium</option>
				<option value="98">Large</option>
			</select><br>
			Quantity: <input type="text" name="qty" size="1" value="1">
			<input type="submit" name="submit" value="Add This">
			<input type="hidden" name="product" value="119">
		</form>
	</div>
	<hr>
	<strong><?php echo "Cart Qty: ".$cart->getItemsQty();?></strong>
</div>
</body>
</html>
<?php } ?>
Advertisement

About the author

Richard Feraro is a PHP Developer living in Manila, Philippines. He has been programming for nine (9) years and working on different open source application mostly done in PHP and MySQL.

38 Responses to How to add a product with custom options into Magento shopping cart from an external site?

Avatar

Bill UNITED STATES

July 18th, 2010 at 9:10 pm

So how can we perform this within WordPress if the mage-enabler is plugged in.

Avatar

Richard Feraro

July 18th, 2010 at 10:32 pm

You have to do the programming based on your need.

Avatar

Scott Greenwald UNITED STATES

July 19th, 2010 at 3:07 pm

Very nice! I think this covers what Ive been looking for. Can we also pass form entries to the Magento cart?
I would like to have a mini-form in wp that on submit auto populates the corresponding fields in onepage checkout, allowing users to complete the order. Is this possible?

Avatar

Richard Feraro

July 19th, 2010 at 5:01 pm

Yeah it should allow you to do that. What are the fields that you plan to add from an external page?

Avatar

Scott Greenwald UNITED STATES

July 21st, 2010 at 12:09 am

I have an external page where I’m selling one bottle of a weight loss product.
The form consists of:
First Name
Last Name
Address
City
State
Zip
Country
Email
Phone

Submit would auto populate those fields in magento checkout, allowing users to complete the form and submit payment.

Avatar

Richard Feraro UNITED STATES

July 21st, 2010 at 3:48 pm

Yeah it is possible. Read it here.

Avatar

Scott Greenwald UNITED STATES

July 22nd, 2010 at 1:58 am

Yeah, that’s all greek to me. Can I hire you to do it? Please send me an email if you’re interested

Avatar

Eldonthian McAllister UNITED STATES

September 28th, 2010 at 6:28 am

Hey, how would we add Multiple products at the same time.. ? one simple and one with options?!

Avatar

Eldonthian McAllister UNITED STATES

September 28th, 2010 at 7:25 am

In my case, I have a down loadable product, While I only have one link, it is basically a simple product with options, I thought. does this link exist as a super_attribute? where do I find it?or is it something else.. such as links[]?

Avatar

Richard Feraro UNITED STATES

September 29th, 2010 at 12:17 am

Hi,

You can put them in an array then do a foreach loop calling the $cart->addProduct() in each record.

Avatar

Ben AUSTRALIA

October 29th, 2010 at 9:42 am

Hi
I have a product that have attributes. How do i access the attribute value so I can display it in the shopping cart

Avatar

Richard Feraro UNITED STATES

October 29th, 2010 at 5:08 pm

To display a product attribute, use the code below:

$product = Mage::getModel('catalog/product');
$product->load(product-ID-here);
echo $product->getData('attribute_code');

Avatar

tuba

November 8th, 2010 at 2:00 am

oh… very nice!

Thank you very much, I have been searching this module for last 2-3 days. Thanks Richard!

Avatar

Richard Feraro PHILIPPINES

November 8th, 2010 at 12:12 pm

You’re welcome :)

Avatar

Ben AUSTRALIA

November 8th, 2010 at 11:25 pm

Hi Richard,
I have a situation where I need to access the shopping cart item attributes
I have a products that has 3 attributes each attribute has a price. The user needs to select one attribute to add the product to cart. How do i get the attribute value of the added product… Below is my snapshot of my code
$this->_quote = parent::getQuote();
$quote = $this->_quote;
$quote_items = $quote->getAllItems();
foreach($quote_items as $item){
// I want to get the attribute value using the $item object
}
Thanks

Ben

Avatar

Richard Feraro UNITED STATES

November 9th, 2010 at 1:01 am

Hi Ben, try the code below:

$item->getData('attribute_code');

Avatar

Ben AUSTRALIA

November 9th, 2010 at 5:39 am

Hi Richard,

Didn’t work… value is empty

Avatar

Richard Feraro PHILIPPINES

November 9th, 2010 at 12:08 pm

Hmmm, it should display something because $item must be a product object. How about doing this:

print_r($item);

It should display the product object in it.

Avatar

Ben AUSTRALIA

November 9th, 2010 at 2:38 pm

Here is a snapshot from print_r($item);
I want to get the value of myprice and price_type
=================================================
[_origData:protected] => Array ( [option_id] => 144
[item_id] => 138
[product_id] => 167
=> info_buyRequest [value] => a:3:{s:3:”qty”;s:1:”1″;s:4:”qty2″;s:1:”1″;s:15:”super_attribute”;a:2:{s:29:”myprice”;s:7:”30.0000″;s:17:”price_type”;s:3:”type1″;}}
)
=================================================

Thanks

Ben

Avatar

Richard Feraro UNITED STATES

November 9th, 2010 at 5:18 pm

Try this:

$quote = Mage::getSingleton('checkout/session')->getQuote();
foreach($quote->getAllItems() as $item){
	$product = Mage::getModel('catalog/product')->load($item->getProduct()->getId());
	print_r($product->getData('myprice'));
	// or
	print_r($product->getAttributeText('myprice'));
}

Avatar

Ben AUSTRALIA

November 9th, 2010 at 7:54 pm

Hi Richard
It did not work I think because ‘myprice’ and ‘price_type’ are not product attributes…
I have extended Mage_Checkout_CartController and rewrite Addaction function below is snapshot.
public function addAction()
{
$cart = $this->_getCart();
if ($_POST['price_type] == ’1′) {
$params = array(
‘qty’ => $_POST['qty'],
‘super_attribute’ => array(‘my_price’=> $_POST['price'],
‘price_type’=>’type1′ )

);
…….

Avatar

Richard Feraro UNITED STATES

November 9th, 2010 at 9:15 pm

If you extended it, then it is just treated as an array which is accessible via getData(). I tested the script and it is working.

Avatar

Ben AUSTRALIA

November 10th, 2010 at 7:44 am

Are you able to post your code and advise on any config.xml adjustment so i can test it from my end.

Cheers

Avatar

Richard Feraro UNITED STATES

November 10th, 2010 at 10:45 pm

My config.xml is the same as it was when I downloaded it and installed. Maybe your customization in the checkout cart controller prevents you from using the proper method in Magento in accessing the attributes. I would advise that you follow the right way of adding attributes for a product in the administration panel so you can use the codes I provided earlier.

Avatar

maged INDIA

December 15th, 2010 at 6:32 pm

Hi,

Can you please help me that how can i set custom price for the product added in cart .
I have override the cart controller add action to add product.

i want to use custom price to be set for the product in cart.

Thanks

Avatar

Dave UNITED STATES

January 10th, 2011 at 1:38 am

Is there a users manual for this… I just want to embed the content area of my magento site into a page on wordpress so people can view products are checkout without leaving my blog…

Avatar

Richard Feraro PHILIPPINES

January 10th, 2011 at 1:51 am

The only instructions needed by the plugin is found in the Installation tab. After that, check the Description page “Is this plugin for you?” section.

Avatar

impi

October 15th, 2011 at 1:00 am

Dude…you’re a legend
Thank you,

Avatar

Richard Feraro PHILIPPINES

October 15th, 2011 at 1:11 am

Thanks :)

Avatar

richard PHILIPPINES

October 18th, 2011 at 8:43 pm

galing mo pare ^_^

Avatar

Richard Feraro

October 18th, 2011 at 10:37 pm

Thank you ^_^

Avatar

Nicola ITALY

October 26th, 2011 at 7:27 pm

Hi!
Really good article!

Can I also insert the price into Magento shopping cart from external web-site?

Thanks a lot!!!

Avatar

Richard Feraro

October 26th, 2011 at 8:25 pm

Thanks :)

Yes, you just need to figure out the right array format.

Avatar

Nicola ITALY

November 27th, 2011 at 6:50 pm

Hi, I’m running Magento 1.5, there’s no way to add product with this script O_O

I copy the whole script in a subdirectory under magento install, then I run the script but I can’t understand why no product appear in my magento cart

Any Ideas?

Thanks a lot ;)

Nicola

Avatar

Nicola ITALY

December 11th, 2011 at 7:03 pm

Resolved! Thanks a lot!!

Just one question… How can I add a message with the order?

Thanks a lot!!!

Nicola

Avatar

Richard Feraro PHILIPPINES

December 12th, 2011 at 12:35 pm

Is it for the confirmation email of the order?

Avatar

Silu UNITED KINGDOM

January 18th, 2012 at 6:48 pm

Richard, thank you for this. I’m also impressed with the patience you show to some of the ungrateful commenters on your blog!

Avatar

Richard Feraro

January 19th, 2012 at 12:11 pm

Thank you Silu :)

Comment Form

About my blog

This is a programmer's blog. Whenever I encounter a difficult coding task, I make sure that I'll be able to share it here in the hope that others may find it useful. It's my way of giving back to the open source community who have been a great help to me as well.

Share This Post

or bookmark to your mobile

Bookmark: http://mysillypointofview.richardferaro.com/2010/06/04/add-a-product-with-custom-options-into-magento-cart-from-external-site/

Advertisement

WordPress + Magento

Proudly Pinoy!

Proudly Pinoy!

Recent Trackbacks

Archives

Disclaimer

All entries in this blog are my opinion and don't necessarily reflect the opinion of my employer or my mom.

Viewers by Country

in India 3274 (21 %) 3781 (19 %)
us United States 2739 (17 %) 3560 (18 %)
gb United Kingdom 926 (6 %) 1176 (6 %)
de Germany 767 (5 %) 989 (5 %)
nl Netherlands 586 (4 %) 743 (4 %)
fr France 483 (3 %) 606 (3 %)
ph Philippines 468 (3 %) 763 (4 %)
br Brazil 443 (3 %) 547 (3 %)
au Australia 334 (2 %) 402 (2 %)
ca Canada 330 (2 %) 408 (2 %)
es Spain 323 (2 %) 429 (2 %)
it Italy 318 (2 %) 400 (2 %)
vn Vietnam 285 (2 %) 357 (2 %)
pk Pakistan 278 (2 %) 307 (2 %)
ro Romania 261 (2 %) 319 (2 %)
cn China 246 (2 %) 351 (2 %)
ua Ukraine 240 (2 %) 297 (2 %)
pl Poland 138 (1 %) 167 (1 %)
ru Russia 134 (1 %) 171 (1 %)
id Indonesia 132 (1 %) 159 (1 %)
Total Countries: 139
Total Visits: 15686
Total Pageviews: 19735