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.

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

  1. Sebastian says:

    Hi this seems to work fine, but I would like to add this quick order function in the cart. Can you tell me how to include it there?

  2. I would love to create a wholesale/bulk entry order template with this approach but would need to hire some help.

    My store manger would use the template to create new page (new_assort), specify a list of items (part 1,2,3,etc) in order for the page, and then the page would let the user order a quantity of the items and take them all at once to the cart.

    The page would have columns and rows like this:
    thumbnail, Name/sku, Qty entry

    IMG1 part 1 $x.xx [ 0]
    IMG2 part 2 $x.xx [ 0]
    etc.

    [Submit] [clear]

  3. lan says:

    it’s awesome. Thanks for sharing

  4. 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??

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

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

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

  8. dennis says:

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

    Good Work!

  9. 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!

  10. Jason says:

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

  11. brian says:

    how about multiple orders?

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

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

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

  15. Ben says:

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