PHP SOLUTIONS

This is the blog for getting Idea regarding PHP, Magento, jQuery and JavaScript for Customization.

Monday 31 December 2012

Magento : Change price in cart programatically

Hi,

  Here for change the product price into cart programatically

  There is an simple way for change this product price and add custom price for the specific product into cart when add the product into cart in magento.

1) Create an Attribute for the products,

   Suppose an Ex. is Change Price
   Set its type yes/no for the product

2) Open File, code/core/mage/checkout/model/cart.php

3) Goto on this function,

     public function save()
    {
      ........
      ........
     }

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;
      }

5) Now, Insert Product form Admin and set its Change Price value yes for that product,

6) Add that product to cart you can see that, product Unit price will change to $5.

   This is work for me in 1.7.0.2

 Enjoy!!!!!

Monday 24 December 2012

Magento : Get custom options on category page

 Here, The Code for get Custom Options of Products on category Page, This is useful for reduce the time for customer purchase product and add to cart it, there is another methods are also but in that we can't add product with it's custom options, so using this we can add product to cart with its custom options.


 <?php
    $sandy = "";
    $attVal = $product->getOptions();
    if(sizeof($attVal))
    {
          foreach($attVal as $optionVal)
          {   
        $type = $optionVal->getType();
        if($type != 'field' && $type != 'file' )
         {
                $sandy .= $optionVal->getTitle().": ";
                $sandy .= "<select id='select_".$optionVal->getId()."' name='options[".$optionVal->getId()."]'>";
                foreach($optionVal->getValues() as $valuesKey => $valuesVal)
            {           
                if($valuesVal->getPrice() != "0.00000")
                {
                      $sandy .= "<option price='".$valuesVal->getPrice(true)."' value='".$valuesVal->getId()."'>".$valuesVal->getTitle() .'+'.  $valuesVal->getPrice(true) ."</option>";       
                  }
                  else
                  {
                $sandy .=  "<option price='".$valuesVal->getPrice(true)."' value='".$valuesVal->getId()."'>".$valuesVal->getTitle() ."</option>";
                }
            }
            $sandy .= "</select>";
        }
        else if($type == 'file')
        {
            $sandy .= $optionVal->getTitle().": ";
                $sandy .= "<input type='file' name='options_".$optionVal->getId()."_file'>";               
              }
        else
        {
            $sandy .= $optionVal->getTitle() ."  : ";
                $sandy .= "<input type='text' name='options[".$optionVal->getId()."]' id='options_".$optionVal->getId()."_text' onchange='javascript: getval();'>";                  
        }
        }
    }
    echo $sandy;
  ?>

  <?php echo $this->getPriceHtml($_product, true) ?>
    
  <?php if(!$_product->isGrouped()): ?>
  <label>Qty:</label>
        <input type="text" id="qty_main" maxlength="12" value="<?php echo $this->getMinimalQty($_product)==null?1:$this->getMinimalQty($_product) ?>" onblur="calculatetotal();"/>
        <button type="submit" id="addtocartbt_<?php echo $_product->getSku(); ?>" onclick="productAddToCartForm<?php echo $_product->getId() ?>.submit()">Add to Cart</button>
  <?php endif; ?>  
  </div>
                                        
  </form>
      
<script type="text/javascript">
       
var productAddToCartForm<?php echo $_product->getId() ?> = new VarienForm('product_addtocart_form_<?php echo $_product->getId() ?>');                       
    productAddToCartForm<?php echo $_product->getId() ?>.submit = function()
    {
            if (this.validator.validate())
        {   
            this.form.submit();                           
            }                   
        }.bind(productAddToCartForm<?php echo $_product->getId() ?>);
  </script>
  <?php else: ?>
      <div class="out-of-stock"><?php echo $this->__('Out of stock') ?></div>
  <?php endif; ?>