12/03/2012

Magento: Upgrading mysql setup of module


Suppose, you are working on a module, and you need to:

* Add a new table, or
* Modify current tables, or
* Insert some default data in existing table, or
* Add/ modify some attributes

In every case, either you will
1. goto core_resource table
2. delete module's entry
3. and delete current tables of module

which is a poor solution, because sometimes, you don't have direct access to database, so you can't delete/ modify data in db in that case.
There is a simple solution for this. You can do it by simply upgrading you module version and write a new mysql upgrade file.


Suppose, you have a module named Mymodule. Its version is 0.1.1. You have the mysql setup file (mysql install file) mysql4-install-0.1.1.php in Mymodule/sql/mymodule_setup folder of your module.

You don’t need to make direct changes to database. To fulfill any of the above tasks in your module,


1) First create a new mysql upgrade file inside Mymodule/sql/mymodule_setup folder.

Let us suppose that you are going to change the version of your module from 0.1.1 to 0.1.2.

The name of mysql upgrade file should be mysql4-upgrade-0.1.1-0.1.2.php

2) Write necessary sql statements in this mysql upgrade file.

3) Now, you have to change the version in config.xml in Mymodule/etc folder as well.

Change the version like 0.1.2. Which is the new version for our module.

4) Reload your site. And you are done!


Note: The upgrade format is like mysql4-upgrade-CURRENT_VERSION-UPGRADED_VERSION.php


Enjoy.. :)

10/29/2012

Magento: How to remove parent category path from sub category url in Magento

Hi friends,

Today I would like to tell you, how you can remove parent category path from sub category url in Magento.

To do so, Go to:
app/code/core/Mage/Catalog/Model folder and copy Url.php file to override it in your local package.

Open this file (Url.php) and find the following:
         public function getCategoryRequestPath($category, $parentPath)

With in this function, find the following code:

        if (null === $parentPath) {
            $parentPath = $this->getResource()->getCategoryParentPath($category);
        }
        elseif ($parentPath == '/') {
            $parentPath = '';
        }
and comment it like this:

 //     if (null === $parentPath) {
 //         $parentPath = $this->getResource()->getCategoryParentPath($category);
 //     }
 //     elseif ($parentPath == '/') {
            $parentPath = '';
 //       }
and save the file.

Note: It should be noted that the code $parentPath = ''; in else part should not be commented.
Now login to admin panel of your site, and go to:
System->Config->Index Management and click on "select all" then select Reindex Data from the Action Dropdown, then click on submit.

That's all.. Enjoy.. :)


10/23/2012

Magento: How to view Magento version

Hi friends,

Have you faced problem when you were working on a complete customized theme and you were unable to know current magento version.

Just try this solution to find current Magento version of your website:

Create file versionTest.php parallel to index.php in root folder.
Now copy and paste following code in versionTest.php:
<?php
      include_once(‘App/Mage.php’);
      Mage::app();
      echo Mage::getVersion();
?>


Now just access this file using browser. Enjoy.. :)

Magento: Difference between Mage::getSingleton() and Mage::getModel() in Magento


Hi friends,
Some people ask about the difference between Mage::getModel() and Mage::getSingleton() in Magento.
Here is the difference between these two:

Mage::getModel('module/model') will create a new instance of the model object (if such object exists in configuration)
So if we write:
Mage::getModel('catalog/product'), it will create new instance of the object product each time. So,

$p1 = Mage::getModel('catalog/product');
$p2 = Mage::getModel('catalog/product');
Then $p1 is a one instance of product and $p2 is another instance of product.

While Mage::getSingleton('catalog/product') will create reference on existing object (if object does not exist then this method will create an instance using ::getModel() and returns reference to it).
So, if
$p1 = Mage::getSingleton('catalog/product');
$p2 = Mage::getSingleton('catalog/product');
Then $p1 and $p2 both refer to the same instance of product as reference $p2.

That's all.. Cheers.. :)

8/10/2012

Magento: Get all related products of current product

Obviously, we can get all related products of current product by calling related.phtml. But there are many cases, in which we can not call related.phtml. For example:let us assume that we are showing two or more products on a page and we may want to show small image and name of these two products at a time.

Many other situations can be there, when we may not have option, but to write custom code to get related products of currently loaded product.

For such situations, try this code:

$collection = Mage::getModel('catalog/product_link')
                    ->getCollection()
                    ->addFieldToFilter('product_id',$product_id)
                    ->addFieldToFilter('link_type_id','1');
$related_products = $collection->getData();
This will give you list of all related products of a product.
Note: Here $product_id is the id of currently loaded product.
That's all.. Cheers.. :)

COURTESY: Mr. Neeraj Gupta (My dear Friend)

8/08/2012

Magento: How to get last order id

There are many ways to get last order id:
 
1.  From checkout session:
    $lastOrderId = Mage::getSingleton('checkout/session')
->getLastRealOrderId();


$orderId = Mage::getModel('sales/order')
->loadByIncrementId($lastOrderId)
->getEntityId();

Above solution will not work, if you want to get last order id in admin.
For this you can try this solution:
2.  From model:
 
$orders = Mage::getModel('sales/order')->getCollection()
->setOrder('increment_id','DESC')
->setPageSize(1)
->setCurPage(1);
$orderId = $orders->getFirstItem()->getEntityId();
 
But this method will not work, if there are multi store in a single magento setup. So a better solution would be:

$orders = Mage::getModel('sales/order')->getCollection()
->setOrder('created_at','DESC')
->setPageSize(1)
->setCurPage(1);
$orderId = $orders->getFirstItem()->getEntityId();
 
 
That's all.. Enjoy.. :) 

8/02/2012

Magento: Debug Layout within Controller's Action

Hi,

I found a magical code to debug layout from controller's action.

var_dump(Mage::getSingleton('core/layout')->getUpdate()->getHandles());
exit("Your Layout Path is ".__LINE__." in ".__FILE__);

Just add this code and you can debug your layout.

Enjoy.. :)

COURTESY: Mr. Ankur Singhania(My dear Friend)