Wednesday, September 5, 2012

Bestseller Products in Magento


>>  You can easily get the bestseller products using this code :

$storeId = Mage::app()->getStore()->getStoreId();
$sales_order = Mage::getSingleton('core/resource')->getTableName('sales_flat_order');
$sales_order_item = Mage::getSingleton('core/resource')->getTableName('sales_flat_order_item');

$_productCollection = Mage::getModel('sales/order_item')->getCollection();

$_productCollection = $_productCollection->addAttributeToSelect('order_id')
                ->addAttributeToSelect('product_id')
                ->addAttributeToFilter('main_table.store_id', $storeId)
                ->addAttributeToFilter('main_table.parent_item_id', array('null' => true));
               
$_productCollection->getSelect()
                   ->join(array('so' => $sales_order), 'main_table.order_id = so.entity_id',  array('so.*'))
                   ->where("so.status <> 'canceled'")
                   ->columns('SUM(main_table.qty_ordered) AS total_qty')
                   ->order(array('total_qty desc'))
                   ->group(array('main_table.product_id'));

foreach($_productCollection as $order)
{
    $_product = Mage::getModel('catalog/product')->load($order->getProductId());
    ...........................................................
    ............................................................// show product info here
}




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




Add attribute option programmatically in magento

 You can easily create attribute options from outside magento using following code :

<?php
require_once 'app/Mage.php';
umask(0);
Mage::app();

function addAttributeOption($attribute_code, $attribute_value) {
    $attribute_model = Mage::getModel('eav/entity_attribute');
    $attribute_options_model= Mage::getModel('eav/entity_attribute_source_table') ;
   
    $attribute_id = $attribute_model->getIdByCode('catalog_product', $attribute_code);
    $attribute = $attribute_model->load($attribute_id);
   
    $attribute_table = $attribute_options_model->setAttribute($attribute);
    $options = $attribute_options_model->getAllOptions(false);

   foreach($options as $option) {
        // checking if already exists
        if ($option['label'] == $attribute_value) {
$optionId = $option['value'];
                return $optionId;
        }
    }

    $value['option'] = array($attribute_value,$attribute_value);
    $result = array('value' => $value);
    $attribute->setData('option',$result);
    $attribute->save();
}

addAttributeOption('color','white');

?>

Note : In this example I've added an option "white" to an attribute "color" of select type.

Magento get Attribute Options


>> Get all Options of an Attribute  : 

    $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'attribute_code');        // attribute code here
    $ingredientArr = array();
    foreach ( $attribute->getSource()->getAllOptions(true, true) as $option)
    {
        $ingredientArr[] = array(
            'value' => $option['value'],
            'label' => $option['label']
        );
    }

Note : $option['value'] is the id and $option['label'] is the option name.

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>