![]()
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!
#magento is a great resource. Thanks for sharing this – I know there are many out there who will find this useful.
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
Hi there,
what if i need to add an extra input field for size for example.
where is the action page?
Thank you.
EXCELLENT post. In a wholesale environment this is key. Thanks much, I’ll let you know if we adapt this for our use.
how about multiple orders?
You are amazing! Just saved me hours of headaches. Thanks a lot!
Had to add this to a site and though I’d search to see if it had been done. Thanks so much for sharing!
Thank you ver much! It works with magento 1.4, too.
Good Work!