Saturday, September 19, 2015

Product price change programatically after add to cart in magento

Step 1: Create an Attribute for the products
Example :-  Change Price Set its type yes/no for the product 


Step 2: Open File

code/core/mage/checkout/model/cart.php


Step 3: Goto on this function

 public function save()
    {
      ........
      ........
     }
Step 4: Copy and Paste below code,
public function save()
    {
        Mage::dispatchEvent('checkout_cart_save_before', array('cart'=>$this));

        $this->getQuote()->getBillingAddress();
        $this->getQuote()->getShippingAddress()->setCollectShippingRates(true);
        $this->getQuote()->collectTotals();
               
        foreach($this->getQuote()->getAllItems() as $item) {             
          $productId = $item->getProductId();
          $product = Mage::getModel('catalog/product')->load($productId);
          if($product->getAttributeText('change_price') == 'Yes')
         {
                 $price = 5;
                 $item->setCustomPrice($price);
                 $item->setOriginalCustomPrice($price);
          }
        }  
        $this->getQuote()->save(); 
        $this->getCheckoutSession()->setQuoteId($this->getQuote()->getId());
        
        /**
         * Cart save usually called after changes with cart items.
         */
        Mage::dispatchEvent('checkout_cart_save_after', array('cart'=>$this));
        return $this;
      }
Step 5: Now, Insert Product  Admin side  and set Attribute Change Price value yes for that product
Step 6: Add that product  to cart you can see that, product  price will change to $5.

 Enjoy!!!!!

Add Increase and Decrease Quantity Buttons in Magento


open addtocart.phtml file replace this code

Path :- app\design\frontend\default\your theme\template\catalog\product\view\addtocart.phtml


<?php $_product = $this->getProduct(); ?>
<?php $buttonTitle = Mage::helper('core')->quoteEscape($this->__('Add to Cart')); ?>
<?php if($_product->isSaleable()): ?>
    <div class="add-to-cart">
        <?php if(!$_product->isGrouped()): ?>
        <div class="qty-wrapper">
            <label for="qty"><?php echo $this->__('Qty:') ?></label>
            <input type="text" pattern="\d*" name="qty" id="qty" maxlength="12" value="<?php echo max($this->getProductDefaultQty() * 1, 1) ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Qty')) ?>" class="input-text qty" / 
           <button class="button-arrow button-up" type="button">Increase</button>
           <button class="button-arrow button-down" type="button">Decrease</button>
        </div>
        <?php endif; ?>
        <div class="add-to-cart-buttons">
            <button type="button" title="<?php echo $buttonTitle ?>" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $buttonTitle ?></span></span></button>
            <?php echo $this->getChildHtml('', true, true) ?>
        </div>
    </div>
<?php endif; ?>

<script type="text/javascript">

        //&lt;![CDATA[

        jQuery(function($) {

            $('.add-to-cart .button-up').click(function() {

                $qty = $(this).parent().find('.qty');

                qty = parseInt($qty.val()) + 1;

                $qty.val(qty);

            });

            $('.add-to-cart .button-down').click(function() {

                $qty = $(this).parent().find('.qty');

                qty = parseInt($qty.val()) - 1;

                if (qty < 0)

                    qty = 0;

                $qty.val(qty);

            });

        });

        //]]&gt;


        </script>