Magento: Add Product To Cart By SKU – Form

Today I had to figure out how to create a way for people to add a product to their cart by just entering in a SKU and a quantity. They call it the “Quick Order.” It is really quite simple to implement if you want to add this functionality to your site.

Disclaimer: I’m sure there’s a much better way to do this, but, this method works just fine for me.

First thing – create a new CMS page (or put this code in one of your templates). I used a new CMS page called “Quick Order.” Here’s the code for the form. You can style it up however you’d like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<h2>Quick Order</h2>
 
<div class="quick-order">
<form action="/scripts/add-sku-to-cart.php" method="post">
<p><strong>Product ID: </strong> <input class="input-text" type="text" name="sku-entry">
<strong>Qty: </strong>
<select name="qty" id="qty">
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
<option value='6'>6</option>
<option value='7'>7</option>
<option value='8'>8</option>
<option value='9'>9</option>
<option value='10'>10</option>
<option value='11'>11</option>
<option value='12'>12</option>
<option value='13'>13</option>
<option value='14'>14</option>
<option value='15'>15</option>
<option value='16'>16</option>
<option value='17'>17</option>
<option value='18'>18</option>
<option value='19'>19</option>
<option value='20'>20</option>
</select>
<input type="submit" value="Add Product" /></p>
</form>
</div>

Next, we add the form processor. This code will take the sku and get the actual Magento product id, initialize the cart session, and produce an error on the cart page if the SKU was not found. If it was found, the product will easily be added right to the cart.

I put this file off the root in /scripts/ and I called it “add-sku-to-cart.php”. If you want to have the file elsewhere, you’ll have to modify the form above and the path to Mage.php. Here’s the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
include_once '../app/Mage.php';
Mage::app();
Mage::getSingleton('core/session', array('name' => 'frontend'));
 
$sku = $_POST['sku-entry'];
 
if (!isset($_POST['qty'])) { $qty = '1'; } else { $qty = $_POST['qty']; }
 
$id = Mage::getModel('catalog/product')->getIdBySku("$sku");
 
if ($id == '') {
	$id = $sku;
	Mage::getSingleton('checkout/session')->addError("<strong>Product Not Added</strong><br />The SKU you entered ($sku) was not found.");
}
 
header('Location: /checkout/cart/add?product='.$id.'&qty='.$qty);

Hope you find this useful! Much thanks to the guys in #magento IRC. I probably wouldn’t have figured this out any time soon without them. Thanks!

This entry was posted in Magento. Bookmark the permalink.

12 Responses to Magento: Add Product To Cart By SKU – Form

  1. Ben says:

    #magento is a great resource. Thanks for sharing this – I know there are many out there who will find this useful.

  2. Lee Saferite says:

    I know it’s almost overkill, but you could put this script into a module as a controller and keep it in line with the rest of magento

  3. Bivek says:

    Hi there,

    what if i need to add an extra input field for size for example.

    where is the action page?

    Thank you.

  4. MXWest says:

    EXCELLENT post. In a wholesale environment this is key. Thanks much, I’ll let you know if we adapt this for our use.

  5. brian says:

    how about multiple orders?

  6. Jason says:

    You are amazing! Just saved me hours of headaches. Thanks a lot!

  7. Andrew says:

    Had to add this to a site and though I’d search to see if it had been done. Thanks so much for sharing!

  8. dennis says:

    Thank you ver much! It works with magento 1.4, too.

    Good Work!

  9. Jessica Hoel says:

    I’m trying to get this to work… but I think the backend sees products by “Id” not sku. The page LOOKS like it works but I get an error with any product I enter…

  10. TyreXWolF says:

    Wouarh !! Excellent !
    that’s exactly what I wanted.
    In addition to a CMS page, it’s perfect and it works.
    Thank you very much for sharing the tip.

  11. Srikanth says:

    Hi,
    For adding one product it is working very good. But I wanted to add multiple sku and qty and then add to cart how can I do that please help me.

    Regards,
    Srikanth Thandra

  12. Dennis says:

    Hello
    I need some help.
    Is there a way to add one picture to 816 products? They all have the same ID but they have an individual SKU. Doing them ten at a time will take some time. I am hoping there is a script out there or some other way to add the photo to the product page of all of them at once. Is there a way??

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">