Friday, 19 July 2013

Discount per product in magento

steps for apply discount per product by percent & also create by flat amount

1.step create attribute 'discount' data-type texfield
 create another attribute 'appludiscount' data-type yes/no  default value yes

2.assign attribute in attribute set's price tab

3.add following code in catalog/product/view.phtml file just after
product name
   
    <?php $discount = $_product['discount']; ?>
        <?php if($discount !=""){?>
            <div class="product-discount">
            <?php echo $discount."% Discount"; ?>
            </div>
        <?php }?>

4.add following code in checkout/cart/item/default.phtml file after line no 41
<?php
$custom = Mage::getModel('catalog/product')->load($_item->getProductId());
$discount= $custom['discount']; ?>
<?php if($discount !=""){?>
<div class="discount"> <?php if($custom->getAttributeText('applydiscount') == "No")
{echo Flat." ".$discount." ". Discount; }
else
{
    echo $discount."% Discount";}?></div>
<?php }?>

5. now create observer file for it so create module
app/code/local/plumtree/Discount/model/observer.php file
<?php

class Plumtree_Discount_Model_Observer
{
   
    public function modifyPrice(Varien_Event_Observer $observer)
    {
       
       $item = $observer->getQuoteItem();
    if ($item->getParentItem()) {
        $item = $item->getParentItem();
        //echo "<pre>";
        //print_r($item->getData());exit;
    }

   
    $pid= $item->getProductId();
   $product = Mage::getModel('catalog/product')->load($pid);
   $productData  = $product->getData();
    $discount = $productData['discount'];
                   /* flat ammount Discount code starts here*/
    if( $product->getAttributeText('applydiscount') == "No"){
        $specialprice = $product->getFinalPrice();
        $new_price = $specialprice - $discount;
               if ($new_price > 0) {
        $item->setCustomPrice($new_price);
        $item->setOriginalCustomPrice($new_price);
        $item->getProduct()->setIsSuperMode(true);
            }
        }
       
         /* flat ammount Discount code ends here*/
       
          /* flat percent Discount code starts here*/
        else{
            $specialprice = $product->getFinalPrice();
            $new_price = ($specialprice*(100-$discount))/100;
                   if ($new_price > 0) {
         $item->setCustomPrice($new_price);
         $item->setOriginalCustomPrice($new_price);
         $item->getProduct()->setIsSuperMode(true);
              }
            }
           
             /* flat percent Discount code ends here*/
}
}

6.create app/code/local/plumtree/Discount/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Plumtree_Discount>
            <version>0.1.0</version>
        </Plumtree_Discount>
    </modules>
       <frontend>
         <events>
            <checkout_cart_product_add_after>
                <observers>
                   <Plumtree_Discount_Model_Observer>
                      <type>singleton</type>
                      <class>Plumtree_Discount_Model_Observer</class>
                      <method>modifyPrice</method>
                   </Plumtree_Discount_Model_Observer>
               </observers>
            </checkout_cart_product_add_after>
        </events>
      </frontend>
</config>
enjoy the code.

No comments:

Post a Comment