Sunday, 15 April 2012

Implementing Fly Cart In Magento

1. Download the j2t-ajax-cart
http://www.magentocommerce.com/magento-connect/j2t-ajax-cart-lite.html
2. Download the jquery library don't forget to add
     var $j = jQuery.noConflict();  to avoid the conflict between js.
    in my case I dowloaded the js in js/jquery/jquery-1.3.2.min.js
3. Add your js to page.xml
              <block type="page/html_head" name="head" as="head">
                <action method="addJs"><script>jquery/jquery-            1.3.2.min.js</script></action>

4. skin/frontend/default/default/js/j2t/ajax_cart.js
  replace onComplete: function (xhr) with the following line of code
onComplete: function (xhr)
            {
                /**********************************************/

var pr_id = url.substring(url.indexOf('/product/')+9,url.lastIndexOf("/"));

var pr_x = $j("#prid_"+pr_id).offset().left;
var pr_y = $j("#prid_"+pr_id).offset().top;




var cart_X = $j(".block-cart").offset().left;
var cart_Y = $j(".block-cart").offset().top;

var gotoX = cart_X - pr_x;
var gotoY = cart_Y - pr_y;

var newImageWidth = $j("#prid_"+pr_id).width() / 3;
var newImageHeight = $j("#prid_"+pr_id).height() / 3;

$j("#prid_"+pr_id).find('.aishimg')
.clone()
.prependTo("#prid_"+pr_id)
.css({'position' : 'absolute'})
.animate({opacity: 0.6}, 100)
.animate({opacity: 0.6, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, 1200, '', function() {
$j(this).remove();
});

/**********************************************/
5.  After this line of code replaceDelUrls(); comment out these code
             /*   if (ajax_cart_show_popup){
                    showConfirm();
                } else {
                    hideJ2tOverlay();
                }*/
6.function sendcart(url, type){
    //showLoading();
  comment out the showLoding as well.
7. Go the template/catalog/product/list.phtml

Go to the
<?php // Grid Mode ?>
add these two line as I shown in below code.

1.  id = '<?php echo "prid_".$_product->getId(); ?>'
2. img class = "aishimg"

 <li id = '<?php echo "prid_".$_product->getId(); ?>' class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
                <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img class = "aishimg" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>
                <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h2>
                <?php if($_product->getRatingSummary()): ?>
                <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                <?php endif; ?>
                <?php echo $this->getPriceHtml($_product, true) ?>
                <div class="actions">
                    <?php if($_product->isSaleable()): ?>



Saturday, 7 January 2012

How to rewrite a block in magento


Go to admin panel Catalog->Categories->Manage Categories
There you can observe, There are 4 tabs

Go to the last tab Category Product and you can observe that there is no created_at time.
In this blog i am going to bring these  extra column with their respective created_at time



Step2. Binding all the folder

<?xml version="1.0" encoding="UTF-8"?>
<!--
      Document   : config.xml
    Created on : Jan 7, 2012, 4:52 PM
    Author     : V.V.N.Pradeep
    Description:
        Purpose of the document follows.

        This configuration is used to bind the all folder
        inside the Pradeep module.

        How to rewrite a Block
-->

<config>
 
  <global>
        <!-- This section is to bind the block with our module Pradeep -->
        <blocks>
            <pradeep>
            <class>Veera_Pradeep_Block</class>
            </pradeep>

           <!-- rewritting the adminhtml blog
                app/code/core/Mage/Block/Catalog/Category/Tab/Product.php
                              to
                app/code/local/Veera/Pradeep/Block/Product.php
            -->

            <adminhtml>
                <rewrite>
                    <catalog_category_tab_product>Veera_Pradeep_Block_Product</catalog_category_tab_product>
                </rewrite>
                <!-- Multiple rewrite -->
          <!--      <rewrite>
                    <catalog_category_tabs>Veera_Pradeep_Block_Tabs</catalog_category_tabs>
                </rewrite> -->
            </adminhtml>

        </blocks>

        <!-- End of block-->
   </global>
</config>

/******************************/
Step4: Writng Block
<?php
/*
 * @author  : V.V.N.Pradeep
 * @description --
 *    Purpose of the document follows.
 *  In this tutorial I am going to rewrite the
 *  adminhtml blog
 *
 */

?>

<?php
class Veera_Pradeep_Block_Product extends Mage_Adminhtml_Block_Widget_Grid
{
     public function __construct()
    {
        parent::__construct();
        $this->setId('catalog_category_products');
        $this->setDefaultSort('entity_id');
        $this->setUseAjax(true);
    }

    public function getCategory()
    {
        return Mage::registry('category');
    }

    protected function _addColumnFilterToCollection($column)
    {
        // Set custom filter for in category flag
        if ($column->getId() == 'in_category') {
            $productIds = $this->_getSelectedProducts();
            if (empty($productIds)) {
                $productIds = 0;
            }
            if ($column->getFilter()->getValue()) {
                $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
            }
            elseif(!empty($productIds)) {
                $this->getCollection()->addFieldToFilter('entity_id', array('nin'=>$productIds));
            }
        }
        else {
            parent::_addColumnFilterToCollection($column);
        }
        return $this;
    }

    protected function _prepareCollection()
    {
        if ($this->getCategory()->getId()) {
            $this->setDefaultFilter(array('in_category'=>1));
        }
        $collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('*')
            ->addStoreFilter($this->getRequest()->getParam('store'))
            ->joinField('position',
                'catalog/category_product',
                'position',
                'product_id=entity_id',
                'category_id='.(int) $this->getRequest()->getParam('id', 0),
                'left');
        $this->setCollection($collection);

        if ($this->getCategory()->getProductsReadonly()) {
            $productIds = $this->_getSelectedProducts();
            if (empty($productIds)) {
                $productIds = 0;
            }
            $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
        }

        return parent::_prepareCollection();
    }

    protected function _prepareColumns()
    {
        if (!$this->getCategory()->getProductsReadonly()) {
            $this->addColumn('in_category', array(
                'header_css_class' => 'a-center',
                'type'      => 'checkbox',
                'name'      => 'in_category',
                'values'    => $this->_getSelectedProducts(),
                'align'     => 'center',
                'index'     => 'entity_id'
            ));
        }
        $this->addColumn('entity_id', array(
            'header'    => Mage::helper('catalog')->__('ID'),
            'sortable'  => true,
            'width'     => '60',
            'index'     => 'entity_id'
        ));
        $this->addColumn('name', array(
            'header'    => Mage::helper('catalog')->__('Name'),
            'index'     => 'name'
        ));
        $this->addColumn('sku', array(
            'header'    => Mage::helper('catalog')->__('SKU'),
            'width'     => '80',
            'index'     => 'sku'
        ));
        $this->addColumn('price', array(
            'header'    => Mage::helper('catalog')->__('Price'),
            'type'  => 'currency',
            'width'     => '1',
            'currency_code' => (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE),
            'index'     => 'price'
        ));
        $this->addColumn('created_at', array(
            'header'    => Mage::helper('catalog')->__('created_at'),
            'type'  => 'DateTime',
            'width'     => '150',
            'index'     => 'created_at'
        ));
        $this->addColumn('position', array(
            'header'    => Mage::helper('catalog')->__('Position'),
            'width'     => '1',
            'type'      => 'number',
            'index'     => 'position',
            'editable'  => !$this->getCategory()->getProductsReadonly()
            //'renderer'  => 'adminhtml/widget_grid_column_renderer_input'
        ));

        return parent::_prepareColumns();
    }

    public function getGridUrl()
    {
        return $this->getUrl('*/*/grid', array('_current'=>true));
    }

    protected function _getSelectedProducts()
    {
        $products = $this->getRequest()->getPost('selected_products');
        if (is_null($products)) {
            $products = $this->getCategory()->getProductsPosition();
            return array_keys($products);
        }
        return $products;
    }

}

?>
/******************************/
Output:
go to your admin panel  Catalog->Categories->Manage Categories 



 Enjoy.......... :)

How to rewrite a controller or rewrite a particular controller's action



Step2. Binding all the folder

<?xml version="1.0" encoding="UTF-8"?>
<!--
    Document   : config.xml
    Created on : Jan 7th, 2012, 3:19 PM
    Author     : V.V.N.Pradeep
    Description:
        Purpose of the document follows.

        This configuration is used to bind the all folder
        inside the Pradeep module.

        How to rewrite a controller

        Note: Don't forget to change the frontname to customer
-->

<config>
    <frontend>
        <routers>
            <pradeep>
                <use>standard</use>
                <args>
                    <module>Veera_Pradeep</module>
                    <!-- Don't forget to change the frontname to customer -->
                   <!-- <frontName>pradeep</frontName> -->
                   <frontName>customer</frontName>

                </args>
            </pradeep>
       </routers>

       <layout>
                <updates>
                    <pradeep>
                        <file>pradeep.xml</file>
                    </pradeep>
                </updates>
       </layout>
        <!-- End of layout tag -->
    </frontend>
    <global>
        <!-- This section is to bind the block with our module Pradeep -->
        <blocks>
            <pradeep>
            <class>Veera_Pradeep_Block</class>
            </pradeep>
        </blocks>

<!-- rewriting a controller -->
      <routers>
            <rewrite>
                <pradeep>
                    <from><![CDATA[#^/customer/account/create/#]]></from>
                    <to>/pradeep/account/create/</to>
                </pradeep>
            </rewrite>

                        <!-- If you want to rewrite all controller's action,
                            then below is the answer  -->
                      <!--  <rewrite>
                <pradeep>
                    <from><![CDATA[#^/customer/account/#]]></from>
                    <to>/pradeep/account/</to>
                </pradeep>
            </rewrite>

             -->   
<!--  multiple rewrite under same front end name-->

     <rewrite>
                <pradeep>
                    <from><![CDATA[#^/customer/address/#]]></from>
                    <to>/pradeep/address/</to>
                </pradeep>
     </rewrite>
    </routers>
      <!-- end of rewritting -->

        <!-- End of block-->
   </global>
</config>


/******************************/
Step3: Writng Controller
<?php
/*
 * @author  : V.V.N.Pradeep
 * @description --
 *    Purpose of the document follows.
 *  In this tutorial I am going to rewrite the
 *   http://demo.com/magento/customer/account/create action.
 *   It is not necessary to mention the sane Controller name or action name.
 *   You can give any name, but best practice is to keep the same name,

*  DS => Directory Separator
 *
 */

?>

<?php
require_once Mage::getModuleDir('controllers', 'Mage_Customer').DS.'AccountController.php';
class Veera_Pradeep_AccountController extends Mage_Customer_AccountController
{
    public function createAction()
    {
        echo "overriding customer controller's create action";
     
    }
}

?>
/******************************/

Output:
To see the output type the url into your browser

example:
http://demo.com/magento/customer/account/create/ [before rewriting check the output]
After rewriting Check the output

Enjoy............... :)

Tuesday, 27 December 2011

Creating Block layout and Template

Creating Block Layout and Template In Magento


Step1. Telling Magento that we are creating new module

under app/etc/modules/ Veera_Pradeep.xml
<?xml version="1.0" encoding="UTF-8"?>

<!--
    Document   : Veera_Pradeep.xml
    Created on : December 26, 2011, 4:47 PM
    Author     : V.V.N.Pradeep
    Description:
        Purpose of the document follows.
-->

<config>
    <modules>
        <Veera_Pradeep>
            <active>true</active>
            <codePool>local</codePool>
        </Veera_Pradeep>
    </modules>
</config>
/*************************/

Step2. Binding all the folder 

<?xml version="1.0" encoding="UTF-8"?>
<!--
    Document   : config.xml
    Created on : December 26, 2011, 5:31 PM
    Author     : V.V.N.Pradeep
    Description:
       Purpose of the document follows.

        This configuration is used to bind the all folder
        inside the Pradeep module.
-->

<config>
     <frontend>
        <routers>
            <pradeep>
                <use>standard</use>
                <args>
                    <module>Veera_Pradeep</module>
                    <frontName>pradeep</frontName>
                </args>
            </pradeep>
       </routers>
       <!-- Tell magento that we are going to use custom layout
       called pradeep.xml for this module -->

       <layout>
                <updates>
                    <pradeep>
                        <file>pradeep.xml</file>
                    </pradeep>
                </updates>
       </layout>
        <!-- End of layout tag -->
    </frontend>
    <global>
        <!-- This section is to bind the block with our module Pradeep -->
        <blocks>
            <pradeep>
            <class>Veera_Pradeep_Block</class>
            </pradeep>
        </blocks>
        <!-- End of blocks

        When you called
    <block type="pradeep/naga" name="pradeep" template="custom/welcome.phtml"/>
    inside your layout/pradeep.xml

    means you are calling the Naga.php (which is under pradeep/block module)
    file and instantiating the object & storing it under the name pradeep
    and then seting the template for our module
       -->

   </global>
</config>

/******************************/
Step3: Writng Controller
<?php
/*
 * @author  : V.V.N.Pradeep
 * @description --
 *    Purpose of the document follows.
 *  
 */
?>

<?php
class Veera_Pradeep_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        $this->loadLayout();
        $this->renderLayout();
    }

}

?>
/******************************/
Step4: Writng Block
<?php
/*
 * @author  : V.V.N.Pradeep
 * @description --
 *    Purpose of the document follows.
 *        To use custom define layout and template
 */

?>

<?php
class Veera_Pradeep_Block_Naga extends Mage_Core_Block_Template
{
   // this function generate the Mage_Core_Block_Template
    public function  _construct() {
       parent::_construct();
     }

     /*The above function is equalent to the below mention function
      *
      *
        public function __construct(){
          if ($this->hasData('template')) {
          $this->setTemplate($this->getData('template'));
         }
       }
      *
      *
      */
}

?>
/******************************/
Step5: Creating Layout
under app/design/frontend/base/default/layout/pradeep.xml
<?xml version="1.0" encoding="UTF-8"?>

<!--
 Document   : pradeep.xml
    Created on : December 27, 2011, 6:51 PM
    Author     : V.V.N.Pradeep
    Description:
        Purpose of the document follows.
        <remove name="right"/>
        <remove name="left"/>  is used to remove the left and right callouts

        Magaento By default provide 4types of template settings,
        1column.phtml,2columns-left.phtml,2columns-right.phtml,3columns.phtml

        You can create your own template settings also...

        you can find these phtml under

        app/design/frontend/base/default/template/page

-->

<layout version="0.1.0">
    <pradeep_index_index translate="label">
        <label>Welcome Page</label>
        <!-- Mage_Customer -->
        <remove name="right"/>
        <remove name="left"/>

        <reference name="root">
            <action method="setTemplate"><template>page/1column.phtml</template></action>
        </reference>
        <reference name="content">
            <block type="pradeep/naga" name="pradeep" template="custom/welcome.phtml"/>
        </reference>
    </pradeep_index_index>

</layout>

/******************************/
Step6: Creating Template
 under app/design/frontend/base/default/template/custom/welcome.phtml
<h1>Welocme to Veera Pradeep Blog</h1>

Output:
to see the output type the url into your browser

example:
http://demo.com/magento/pradeep/index/index

Monday, 26 December 2011

Create a module to display "Welocme to Veera Pradeep Blog" in Magetno

  

Step1. Telling Magento that we are creating new module
The first step is to tell magento that we are going to write a module called pradeep
under veera package name, which u can find in your local codePool.

Always start by writing below mentioned code in app/etc/modules/

syntax:- [your-package-name]_[your-module-name].xml
<?xml version="1.0" encoding="UTF-8"?>

<!--
    Document   : Veera_Pradeep.xml.xml
    Created on : December 26, 2011, 4:47 PM
    Author     : V.V.N.Pradeep
    Description:
        Purpose of the document follows.
        In order to learn xml visit http://www.w3schools.com/xml/

        <config> XML documents must contain a root element. This element is
                 "the parent" of all other elements. you can give any name but,
                 best practice is to give the name <config>
                
        <active> element contain boolean value true/false
        In magento we have 3types of <codePool> value
        1. The first  <codePool> is  community is for third party
        module/extensions which you can download via magento connect
        2. The next <codePool> is core which contains magento inbuild module
          never ever try to modify any of these files.
        3. The last <codePool> is local. all the modules which we creted we
        should place here.
-->

<config>
    <modules>
        <Veera_Pradeep>
            <active>true</active>
            <codePool>local</codePool>
        </Veera_Pradeep>
    </modules>
</config>

<!--
    Description: you can enable or disable your module Output through backend
                 got to your admin panel
                 System->Configuration->Advanced->Disable Module Output
-->

/*************************/
Step2. Binding all the folder 

<?xml version="1.0" encoding="UTF-8"?>

<!--
    Document   : config.xml
    Created on : December 26, 2011, 5:31 PM
    Author     : V.V.N.Pradeep
    Description:
        Purpose of the document follows.

        This configuration is used to bind the all folder
        inside the Pradeep module.

        <config> XML documents must contain a root element. This element is
                 "the parent" of all other elements. you can give any name but,
                 best practice is to give the name <config>
        <routers>  is parses a URL
        <use>standard</use>  There are three types of routers in magento standard, admin and default. We are telling magento use standard router for front end flow. admin for admin/back office functionality. 
        <frontName> can be any name, but best practice is
                    to keep your module name
-->

<config>
    <frontend>
        <routers>
            <pradeep>
                <use>standard</use>
                <args>
                    <module>Veera_Pradeep</module>
                    <frontName>pradeep</frontName>
                </args>
            </pradeep>
        </routers>
    </frontend>
</config>
/******************************/
Step3: Writng Controller
<?php
/*
 * @author  : V.V.N.Pradeep
 * @description --
 *    Purpose of the document follows.
 *   Magento is an MVC based framework. default controllers for an module is
 *   IndexController and default action is indexAction. you must always extend
 *   Mage_Core_Controller_Front_Action Why? Then google it
 */
?>

<?php
class Veera_Pradeep_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        echo "Welocme to Veera Pradeep Blog";
    }
}


/*
 * To see the output just type the url http://demo.com/magento/pradeep or
 *   http://demo.com/magento/pradeep/index/index
 *
 *    Syntax:-   http://demo.com/[frontname]/[controller name]/[action name]
 *
 * pradeep is your frontname which you mentioned in your config.xml
 * under <router> tag which parses the url.

 * How it parses the url google it.
 *
 *
 */


?>