Friday, August 31, 2012

Magento:: how to show admin path hints


>> Go to your phpMyAdmin and Run this query :

    INSERT INTO core_config_data SET scope = 'default', scope_id = 0,
            path = 'dev/debug/template_hints', value = 1;
    INSERT INTO core_config_data SET scope = 'default', scope_id = 0,
            path = 'dev/debug/template_hints_blocks', value = 1;

                                         To revert it change the value 1 to 0.



                ***********         Good Luck        **********

Monday, August 27, 2012

Magento::download zip from extension key


>> Place the extension key here and click submit button. After that you can see the download link on that page. see the link.

                    http://freegento.com/ddl-magento-extension.php

Friday, August 24, 2012

Create stylish select box in jquery


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>Stylish Select Box</title>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
            <script type="text/javascript">
            // JavaScript Document
            jQuery(document).ready(function(){
                // select element styling
                jQuery('select').each(function(){
                    var title = jQuery('option:first', this).text();
                
                    if( jQuery('option:selected', this).val() != ''  ) title = jQuery('option:selected',this).text();
                
                      if(jQuery(this).prev('span').text() == '' && jQuery(this).is(':visible')) {
                        jQuery(this)
                            .css({'z-index':10,'opacity':0,'-khtml-appearance':'none'})
                            .before('<span class="select">'+ title + '</span>')
                            .change(function(){
                                val = jQuery('option:selected',this).text();
                                jQuery(this).prev().text(val);
                            })
                    }
                })
            });
            </script>
            <style type="text/css">
            .option_select_box select{
                color: #57462B;
                cursor: pointer;
                font-size: 12px;
                font-style: italic;
                height: 28px !important;
                line-height: 28px;
                margin: 0;
                position: relative;
                width: 115px !important;
            }
            .selecttest {
                background: url(" http://justexample.netne.net/selectbox/images/select_box6.png ") no-repeat scroll 0 0 transparent;
                border: medium none;
                padding-right: 10px;
            }
            .option_select_box{
                position: relative;
                z-index: 1;
            }
            .option_select_box span{
                background: url(" http://justexample.netne.net/selectbox/images/select_box6.png ") no-repeat scroll 0 0 transparent;
                color: #57462B;
                cursor: default;
                float: left;
                font-size: 12px;
                height: 28px;
                left: 0;
                line-height: 28px;
                overflow: hidden;
                position: absolute;
                text-align: left;
                text-indent: 10px;
                top: 0;
                width: 115px;
                z-index: 5;
            }
            </style>
    </head>
   
    <body>
            <div class="option_select_box">
              <select class="selecttest" name="">
                <option value="1">15 days</option>
                <option value="2">30 days</option>
                <option value="3">60 days</option>
              </select>
            </div>
    </body>
</html>

see the Demo here. 


Wednesday, August 22, 2012

Product list with pagination in home page in Magento


>> In the backend Go to
  
   Go to Admin=>CMS->home page and  put the following code in the layout update xml

<reference name="content">
      <block type="catalog/layer_view" name="catalog.leftnav" before="-" template="catalog/layer/view.phtml"/>
      <block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
              <action method="setCategoryId">
              <category_id>3</category_id>
              </action>
              <block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
              <block type="page/html_pager" name="product_list_toolbar_pager"/>
              </block>
              <action method="addColumnCountLayoutDepend">
              <layout>two_columns_right</layout>
              <count>4</count>
              </action> <action method="setToolbarBlockName">
              <name>product_list_toolbar</name>
              </action>
      </block>
      <update handle="page_two_columns_right" />
</reference>

Monday, August 20, 2012

Add first/last name in newsletter module Magento


 >> First add two fields ( subscriber_firstname, subscriber_lastname) in newsletter_subscriber table and app\design\frontend\THEME\default\template\newsletter\subscribe.phtml.

    >> Now edit this page : app\code\core\Mage\Newsletter\controllers\SubscriberController.php

        Here you'll find "public function newAction()"
            Paste the below code after this following code:
            $status = Mage::getModel('newsletter/subscriber')->subscribe($email);    //line (63)

     ///////////////////////// First Name //////////////////////////////

            if ($this->getRequest()->getPost('subscriber_firstname'))
                {
                     $subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
                     $name     = (string) $this->getRequest()->getPost('subscriber_firstname');
                     $subscriber->setSubscriberFirstname($name);
                     $subscriber->save();
                }

     //////////////////////  END  ///////////////////////////////////////

            Do same for field Last Name....

    >> The above code will effect your frontend. If you want to see the information from Admin too, you have to implement a little bit.

    Go to this link : app\code\core\Mage\Adminhtml\Block\Newsletter\Subscriber\grid.php
    Add this code :

    //It will show the information to the admin on News Letter Subscriber Section.
    $this->addColumn('subscriber_firstname', array(
            'header'    => Mage::helper('newsletter')->__('Subscriber First Name'),
            'index'     => 'subscriber_firstname',
            'default'   =>    '----'
        )); 

    *Note :- I would suggest to override the subscriber controller and admin blocks. In this post I've focused in the logic only and shown in 3 steps to understand you its very simple. But it's not good programming. You should follow the magento module structure. In my another post you'll see that how to override frontend and admin controllers.
  

                                        ***********         Good Luck        **********

           

Magento:: how to get Review/Rating information

>> Get the review collection :

    $reviews = Mage::getModel('review/review')->getResourceCollection();
    $reviews->addStoreFilter( Mage::app()->getStore()->getId() )
          ->addStatusFilter( Mage_Review_Model_Review::STATUS_APPROVED )
          ->setDateOrder()
          ->addRateVotes()
          ->load();      

>> Get the rating summary of a product:


    $summaryData  =  Mage::getModel('review/review_summary')
          ->setStoreId($storeId)
          ->load($_product->getId());
  
    foreach($summaryData as $s){
        $percentage = $s[rating_summary];
        $rev_count= $s[reviews_count];
        break;
    }

>> Get the rating collection :


    $rating_collection = Mage::getModel('rating/rating')->getEntitySummary($_review->getEntityPkValue(),true);
    $rating_count = $rating_collection->getData('count');
    if (!empty($rating_count)) {
        $rating_sum = $rating_collection->getData('sum') / $rating_count;
    }


Friday, August 17, 2012

Magento:: get Admin user details.


>>  Get admin information from admin page ::
    $userArray = Mage::getSingleton('admin/session')->getData();

    $user = Mage::getSingleton('admin/session');
    $userId = $user->getUser()->getUserId();
    $userEmail = $user->getUser()->getEmail();
    $userFirstname = $user->getUser()->getFirstname();
    $userLastname = $user->getUser()->getLastname();
    $userUsername = $user->getUser()->getUsername();
    $userPassword = $user->getUser()->getPassword();

>>  Get admin information from frontend::

    $getadmin=Mage::getModel('admin/user')->load(1);
    $adminname=$getadmin->getName();
    $adminemail=$getadmin->getEmail();




            ***********         Good Luck        **********


Magento:: get store and website information


 >> Get store data :
    Mage::app()->getStore();

    Store Id
    Mage::app()->getStore()->getStoreId();

    Store code
    Mage::app()->getStore()->getCode();

    Website Id
    Mage::app()->getStore()->getWebsiteId();

    Store Name
    Mage::app()->getStore()->getName();

    Is Active
    Mage::app()->getStore()->getIsActive();

    Store Home Url
    Mage::app()->getStore()->getHomeUrl();


                ***********         Good Luck        **********

Magento:: currency format


  >> Get currency symbol :
Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol();

  >> Change price format with currency symbol :
Mage::helper('core')->currency($price);



Magento:: how to move Email To Friend form in product page


>> Move email to friend form to product details page ::

         1. Go to admin=>system=>configuration=>Email To Friend and change "Limit Sending By" cookie to IP.
         And change "Allow for Guests" to Yes.
       
         2. Add to catalog.xml layout (in content):
         <block type="sendfriend/send" name="sendfriend.send" template="sendfriend/send.phtml" />
       
         3. Add to view.phtml template page:
         <?php echo $this->getChildHtml('sendfriend.send');?>
       
         4. In Mage_Sendfriend_Block_Send block row159
                 replace the method :
        public function getSendUrl()
        {
            return Mage::getUrl('*/*/sendmail', array(
                'id'     => $this->getProductId(),
                'cat_id' => $this->getCategoryId()
            ));
        }

                  with...
        public function getSendUrl()
        {
            return Mage::getUrl('sendfriend/product/sendmail', array(
                'id'     => $this->getProductId(),
                'cat_id' => $this->getCategoryId()
            ));
        }
        
                  ***********         Good Luck        **********

get unique value from an array in php


>> Get unique value in an array::

         $arra1 = array('12','15','18','17','12','18');
         $cntArr = array_count_values($arra1);
         $newArr = array();
         echo "<pre>"; print_r($arra1); echo "</pre>";
         echo "<pre>"; print_r($cntArr); echo "</pre>";
       
         foreach($cntArr as $key => $val){
             if ($val > 1){
             array_push($newArr,$key);
             }
         }
       
         echo "<pre>"; print_r($newArr); echo "</pre>";
       
       
                  ***********         Good Luck        **********


Thursday, August 16, 2012

Magento:: delete all Custom Option by sql



>>      SET FOREIGN_KEY_CHECKS=0;

         TRUNCATE TABLE `catalog_product_option`;
         TRUNCATE TABLE `catalog_product_option_price`;
         TRUNCATE TABLE `catalog_product_option_title`;
         TRUNCATE TABLE `catalog_product_option_type_price`;
         TRUNCATE TABLE `catalog_product_option_type_title`;
         TRUNCATE TABLE `catalog_product_option_type_value`;
       
         SET FOREIGN_KEY_CHECKS=1;
   
                  ***********         Good Luck        **********


Magento:: add javascript in admin form


>>    $eventElem=$fieldset->addField('nocode', 'checkbox', array(
                'label' => Mage::helper('customer')->__('Event Element'),
                'name'  => 'eventelem',
                'id'    => 'eventelem',
                'value'=>1,
                'onclick'=>'modifyTargetElement(this)',
            ));

        $eventElem->setAfterElementHtml('<script>
            function modifyTargetElement(checkboxElem){
                if(checkboxElem.checked){
                    $("target_element").disabled=true;
                }
                else{
                    $("target_element").disabled=false;
                }
            }
        </script>');
   
                  ***********         Good Luck        **********


Magento:: Change items count in List page for every category


>> Go to the admin Category. On the custom design tab in Custom Layout Update section place the follwing code:
         <reference name="product_list">
                  <action method="setTemplate"><template>catalog/product/list-only.phtml</template></action>
         </reference>
       
         <reference name="product_list_toolbar">
                  <action method="setDefaultListPerPage"><limit>20</limit></action>
                  <action method="addPagerLimit"><mode>list</mode><limit>20</limit></action>
                  <action method="addPagerLimit"><mode>list</mode><limit>40</limit></action>
                  <action method="addPagerLimit"><mode>list</mode><limit>60</limit></action>
                  <action method="addPagerLimit"><mode>list</mode><limit>80</limit></action>
                 
                  <action method="setDefaultGridPerPage"><limit>20</limit></action>
                  <action method="addPagerLimit"><mode>grid</mode><limit>20</limit></action>
                  <action method="addPagerLimit"><mode>grid</mode><limit>60</limit></action>
                  <action method="addPagerLimit"><mode>grid</mode><limit>90</limit></action>
                  <action method="disableViewSwitcher" />
         </reference>


  >>  Sometime you need to customize a product listing page. But others will be as usual.
Using this code you can call a separate page for list, toolbar, pager as follows:

<reference name="product_list">
      <action method="setTemplate"><template>vendor/classified.phtml</template></action>
</reference>
<reference name="product_list_toolbar">
      <action method="setTemplate"><template>vendor/toolbar.phtml</template></action>
</reference>
<reference name="product_list_toolbar_pager">
      <action method="setTemplate"><template>vendor/pager.phtml</template></action>
</reference>


>>  You can also change the Pager Frame length as you want. By default it set to 5.
e.g. Prev 1 2 3 4 5 Next
You can also manage it from Admin=>System=>Configuration=>Design=>Pagination=> Set value in Pagination Frame. You can do it in your new template which you have set with the above xml code.
Using this code in template/page/html/pager.phtml you can change the page frame.

<?php $this->setFrameLength(8); ?>


   
   
                  ***********         Good Luck        **********


Magento:: New module OR link in customer dashboard


:: create a new module to add a new link in My Dashboard and then showing some information ::

    >>  Using 'Magento Module Creator' I just create a new module named 'custommodule'

         Now if you want to bring this new module in the customer account dashboard (in the navigation panel) as well
       
         First edit the custommodule.xml file in app/design/frontend/default/default/layout/
       
         Previous content will be like as
       
         <?xml version="1.0"?>
         <layout version="0.1.0">
         <default>
         </default>
         <custommodule_index_index>
         <reference name="content">
         <block type="custommodule/custommodule" name="custommodule" template="custommodule/custommodule.phtml" />
         </reference>
         </custommodule_index_index>
         </layout>
       
         Replace with =================================
       
         <?xml version="1.0"?>
         <layout version="0.1.0">
         <default>
         </default>
       
         <customer_account>
         <reference name="customer_account_navigation">
         <action method="addLink" translate="label" module="custommodule"><name>custommodule</name><path>custommodule/</path><label>Custom Module</label></action>
         </reference>
         </customer_account>
       
         <customer_account_index>
         <reference name="right">
         <action method="unsetChild"><name>custommodule</name></action>
         </reference>
         </customer_account_index>
       
         <custommodule_index_index>
         <update handle="customer_account"/>
         <reference name="content">
         <block type="custommodule/custommodule" name="custommodule" template="custommodule/custommodule.phtml" />
         </reference>
         <reference name="right">
         <action method="unsetChild"><name>custommodule</name></action>
         </reference>
         </custommodule_index_index>
         </layout>
       
                 
                           ***********         Good Luck        **********


Re Index programmatically Magento::


>> if you want to reindex "Category Products" only then you can do as follows:-
         $process = Mage::getModel('index/process')->load(6);
         $process->reindexAll();
       
         If you want to reindex all data then you can loop through these indices and then reindex them:-
         for ($i = 1; $i <= 9; $i++) {
             $process = Mage::getModel('index/process')->load($i);
             $process->reindexAll();
         }


Add related products to order success page Magento::


Suppose you want to show the related products of the products that ordered. In the order success page you will see those related products. lets do this & enjoy :

>> Create a block in checkout.xml
    <reference name="right">
            <block type="catalog/product_list_relatedsuccess" name="catalog.product.relatedsuccess" before="-" template="catalog/product/list/related.phtml"/>
        </reference>
>> Then create the Relatedsuccess.php page copy from Related.php under app/code/core/Mage/Catalog/Block/Product/List

>> Change the class name Mage_Catalog_Block_Product_List_Related to Mage_Catalog_Block_Product_List_Relatedsuccess

>> Then find this method _prepareData()  and replace with following...
    protected function _prepareData()
        {
            $order_id = Mage::getSingleton('checkout/session')->getLastRealOrderId();
        $order = Mage::getModel('sales/order');
        $order->loadByIncrementId($order_id);
        $items = $order->getAllItems();
        $relIds = array();
        foreach($items as $_item){
            $relprodColl = Mage::getModel("catalog/product")->load($_item->getProductId())->getRelatedProductCollection();
            foreach($relprodColl as $prod){
                array_push($relIds, $prod->getId());
            }
        }
        $this->_itemCollection = Mage::getSingleton('catalog/product')->getCollection()->addAttributeToFilter('entity_id', array('in' => array_unique($relIds)));

            return $this;
       }

>> And load the product in the related.phtml page to get all contents.   


Tuesday, August 7, 2012

Create simple image slider using jQuery


<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
var speed = 1000;    // sliding speed
var show  = 5;
var img_width = 125;    // Adjust the width here if you change the width of li......



jQuery(document).ready(function(){
    var count = 1;
    var size = jQuery("#slideshow ul>li").size();
    var liwidth     = img_width;
    /*var liwidth      = jQuery("#slideshow ul>li").width();
    var limarginL = parseInt(jQuery("#slideshow ul>li").css("marginLeft"));
    var limarginR = parseInt(jQuery("#slideshow ul>li").css("marginRight"));
    liwidth = liwidth + limarginL + limarginR;*/
    var ul      = jQuery("#slideshow ul");
    var width = size*liwidth;
    var margin = -41;
   
    ul.css({width : width, marginLeft : margin});
    jQuery("#slideshow ul").parent().css("width", show * liwidth);
    if(show >= size){
            jQuery("#slideshow a.previous").addClass("disable");
            jQuery("#slideshow a.next").addClass("disable");
    }
    jQuery("#slideshow a.previous").click(function(){
        if(count < size-show){
            ul.animate({marginLeft : margin -= liwidth},speed);   
            count++;
        }
    });
    jQuery("#slideshow a.next").click(function(){
        if(count > 1){
            ul.animate({marginLeft : margin += liwidth},speed);
            count--;
        }
    });
});

</script>
<style type="text/css">

#slideshow { position:absolute; background-color:#CCC; width:900px;}
#slideshow .disable{ display:none;}
.slideshow-box {
    margin: 0 auto;
    overflow: hidden;
    padding-top: 2px;
    position: relative;
    width: 790px;
}
#slideshow li{
    float: left;
    margin:0 2px;
    text-align: center;
    width: 121px;
    height: 100px;
    background-color:#FFFFFF;
}
#slideshow ul{
    overflow:hidden;
    height: 100px;
    width: 800px;
}
#slideshow a.previous{
    float:left;
    top:24px;
    position:relative;
}
#slideshow a.next{
    float:right;
    bottom:36px;
    position:relative;
}
</style>

<div id="slideshow" class="slideshow">
    <a href="#" class="previous">Previous</a>
    <div class="slideshow-box">
        <ul>
            <li><a href="#"></a></li>
            <li><a href="#"></a></li>
            <li><a href="#"></a></li>
            <li><a href="#"></a></li>
            <li><a href="#"></a></li>
            <li><a href="#"></a></li>
            <li><a href="#"></a></li>
            <li><a href="#"></a></li>
            <li><a href="#"></a></li>
            <li><a href="#"></a></li>
        </ul>
    </div>
    <a href="#" class="next">Next</a>
</div>