PHP SOLUTIONS

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

Thursday, 24 January 2013

Magento : Add custom field in registration form and in admin side

HI !!!

Here I am add the Custom Field for the Customer Registration Form using the database,

It's very simple to add Custom Field in Registration Form and also in Manage Customer Account Information of Admin,

This is the easiest way to Create field using Database Query !!!!!

A) First, Open your Database of Magento

    Click on SQL tab of  your Database and simplly write Below Query in that

    1)  Insert into eav_attribute set      entity_type_id="1",attribute_code="occupation",backend_type="text",frontend_input="text",frontend_label="Occupation",is_required=0,is_user_defined=0,is_unique=0

   2) Copy the Last inserted Id of  "Eav_attribute" table

   3)  Insert into `eav_entity_attribute` set entity_type_id=1,attribute_set_id=1, attribute_group_id=1,attribute_id=134,sort_order=111

   Here, you mention that change your attribute id with the Last inserted id of Eav_attribute table.

   attribute_id = "Last Inserted Id of Eav_attribute" table

   4) insert into  `customer_eav_attribute` set attribute_id="134",is_visible=1,multiline_count=1,is_system=0,sort_order=111

  same as Point (3) change attribute_id

  5) insert into  `customer_form_attribute` set form_code="adminhtml_customer",attribute_id=134

 same as Point (3) change attribute_id

  6) insert into  `customer_form_attribute` set form_code="checkout_register",attribute_id=134

  same as Point (3) change attribute_id

 7) insert into  `customer_form_attribute` set form_code="customer_account_create",attribute_id=134

 same as Point (3) change attribute_id

8) insert into  `customer_form_attribute` set form_code="customer_account_edit",attribute_id=134

 same as Point (3) change attribute_id



B) Now, some code for Create Field to saw in Front end,

   1) open Customer/Form/registration.phtml

   simply Copy-Paste below code

 Copy this .......

   <li>
        <label for="occupation"><em></em><?php echo $this->__('Occupation') ?></label>

        <div class="input-box">

         <input type="text" name="occupation" id="occupation" value="<?php echo $this->htmlEscape($this->getCustomer()->getOccupation()) ?>" title="<?php echo $this->__('Occupation') ?>" class="input-text" />

         </div>
    </li>

   2) open Customer/Form/edit.phtml

    simply Copy-Paste below code

    Copy this .......

   <li>
        <label for="occupation"><em></em><?php echo $this->__('Occupation') ?></label>

        <div class="input-box">

         <input type="text" name="occupation" id="occupation" value="<?php echo $this->htmlEscape($this->getCustomer()->getOccupation()) ?>" title="<?php echo $this->__('Occupation') ?>" class="input-text" />

         </div>
    </li>


Enjoy!!!!!

This is work for me in Magento 1.7


 

Thursday, 3 January 2013

Magento : Add Fixed price to same Product for a Multiple Qty in cart

Hi,

 Actually This code for set custom price to each product in cart when it add to cart from Category Page,

That's why I found some solution for that add custom value to the Row Total of product

For that

A)  Open code\core\Mage\Sales\Model\Quote\Item\Abstract.php

     Find,  Function calcRowTotal()

  You can see this lines,  

    $total      = $this->getStore()->roundPrice($this->getCalculationPriceOriginal() * $qty;
    $baseTotal  = $this->getBaseCalculationPriceOriginal() * $qty;


    Simply, Add your custom value here

   $temp = 10;  /* Now $10 inserted in to the Row total of Product in cart */

    $total      = ($this->getStore()->roundPrice($this->getCalculationPriceOriginal()) * $qty) + $temp ;
    $baseTotal  = ($this->getBaseCalculationPriceOriginal() * $qty) + $temp;


Here the Basic idea for, Where to change for add custom value of row total of product
you can also set this dynamic as per your requirement,

Enjoy ::


   


Magento : Remove Shipping Method form Checkout

Hi,

  If in any case need to remove the Shipping Method from the site, then Now easily remove that by macking change in  following steps,

  For Remove Shipping Methods you need to change its function 

  This code is working for me in 1.7.0.2 but you can try this for older version also

A)  Open Mage/checkout/controllers/Onepagecontroller.php

     In this File Go to on  "function saveBillingAction() "

    Check this line in this function and change

     From

       elseif (isset($data['use_for_shipping']) && $data['use_for_shipping'] == 1) {
                    $result['goto_section'] = 'shipping_method';
                    $result['update_section'] = array(
                        'name' => 'shipping-method',
                        'html' => $this->_getShippingMethodsHtml()
                    );

     To

       elseif (isset($data['use_for_shipping']) && $data['use_for_shipping'] == 1) {
                    $result['goto_section'] = 'payment';
                    $result['update_section'] = array(
                        'name' => 'payment-method',
                        'html' => $this->_getPaymentMethodsHtml()
                    );

 B)  Now, In Onepagecontroller.php

   Go to on this function  "public function saveShippingAction()"

  change this function

  From

   public function saveShippingAction()
    {
        if ($this->_expireAjax()) {
            return;
        }
        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost('shipping', array());
            $customerAddressId = $this->getRequest()->getPost('shipping_address_id', false);
            $result = $this->getOnepage()->saveShipping($data, $customerAddressId);

            if (!isset($result['error'])) {
                $result['goto_section'] = 'shipping_method';
                $result['update_section'] = array(
                    'name' => 'shipping-method',
                    'html' => $this->_getShippingMethodsHtml()
                );
            }
            $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
        }
    }      


To

public function saveShippingAction()
    {
        if ($this->_expireAjax()) {
            return;
        }
        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost('shipping', array());
            $customerAddressId = $this->getRequest()->getPost('shipping_address_id', false);
            $result = $this->getOnepage()->saveShipping($data, $customerAddressId);

            if (!isset($result['error'])) {
                $result['goto_section'] = 'payment';
                $result['update_section'] = array(
                    'name' => 'payment-method',
                    'html' => $this->_getPaymentMethodsHtml()
                );
            }
            $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
        }
    }


C) After that Open Code/core/mage/sales/model/service/Quote.php

  Find this function " protected function _validate() "

  Here comment below code

  if ($addressValidation !== true) {
                Mage::throwException(
                    Mage::helper('sales')->__('Please check shipping address information. %s', implode(' ', $addressValidation))
                );
            }
            $method= $address->getShippingMethod();
            $rate  = $address->getShippingRateByCode($method);
            if (!$this->getQuote()->isVirtual() && (!$method || !$rate)) {
                Mage::throwException(Mage::helper('sales')->__('Please specify a shipping method.'));
            }

D) Open file code/core/mage/checkout/block/onepage/shipping/Method.php

   This is hide the Shipping Method tab from the Onepage checkout during Place the order

   Find below function

    public function isShow()
    {
        return !$this->getQuote()->isVirtual();
    }

   Change it with

    public function isShow()
    {
                return false;
    }


   Now, Finally you can check that your Shipping Method will remove from your site.

  Enjoy ::





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

Wednesday, 28 November 2012

Magento : Remove TAX / VAT from grand total in checkout


Here, We can Remove Tax from Grand Total.

If we want to removed tax for all customer we simply remove Tax class from the produc's then It will remove automatically.

But If you want to remove Tax for specific customer you can do using below steps.

Here Example for Remove Tax for that Customer Who have their Business VAT Numbers.

1) Goto " app/code/core/mage/tax/model/calculation.php  "

2) Find this Function "  public function getRateRequest() "

3) You can get this at the end of the function

    $request = new Varien_Object();
        $request
            ->setCountryId($address->getCountryId())
            ->setRegionId($address->getRegionId())
            ->setPostcode($address->getPostcode())
            ->setStore($store);
            ->setCustomerClassId($customerTaxClass);

4) Now Here for who have VAT/TAX numbers, no apply Tax in Grand Total

    $eutax = $customer['taxvat'];

    if($eutax != '')
   {
            //Who have to insert their VAT Number then remove TAX from Grand Total
            $request = new Varien_Object();
            $request
                       ->setCountryId($address->getCountryId())
                       ->setRegionId($address->getRegionId())
                       ->setPostcode($address->getPostcode())
                       ->setStore($store);                      
    }
    else
    {
           //Who have to not insertd their VAT Number then add TAX in Grand Total
           $request = new Varien_Object();
           $request
                      ->setCountryId($address->getCountryId())
                      ->setRegionId($address->getRegionId())
                      ->setPostcode($address->getPostcode())
                      ->setStore($store);
                      ->setCustomerClassId($customerTaxClass);


    }

5) Now check Tax is Remove from Grand Total in checkout.