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............... :)