Friday, 25 January 2013

Quickly Add to Cart via Quantity and SKU of the product

 Put This code to your home page or where you want to add this module:-


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

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

    if($sku = $_POST['sku']){
        $sku = $_POST['sku'];
        $p = Mage::getModel('catalog/product')->getIdBySku($sku);

        // 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($p);
   
        $cart->addProduct($product, array(
            'qty' => $_POST['qty'],
            'super_attribute' => array( key($_POST['super_attribute']) => $_POST['super_attribute'][525] )
            )
        );
    }
    if($sku1 = $_POST['sku1']){
        $sku1 = $_POST['sku1'];
        $p1 = Mage::getModel('catalog/product')->getIdBySku($sku1);

        // 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($p1);
   
        $cart->addProduct($product, array(
            'qty1' => $_POST['qty1'],
            '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);
}else{
?>
<html>
<head></head>
<body>
<div style="width: 400px; margin: auto">
   
   
    <div style="width: 180px; margin:auto; line-height: 30px">
        <form action="#" method="post">   
            Quantity: <input type="text" name="qty" size="1" />
            SKU: <input type="text" name="sku" size="1" />
            Quantity: <input type="text" name="qty1" size="1" />
            SKU: <input type="text" name="sku1" size="1" />

            <input type="submit" name="submit" value="Add This">
           
        </form>
    </div>
    <hr>
    <strong><?php echo "Cart Qty: ".$cart->getItemsQty();?></strong>
</div>
</body>
</html>
<?php } ?>

No comments:

Post a Comment