Google+

Pages

Friday, August 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)

Wednesday, August 8, 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.. :) 

Thursday, August 2, 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)

Magento: Catalog Search not working in magento

If quick search is not working in your magento store, don't worry. Here is a simple solution for this problem.

1.    Login to Admin Panel.
2.    Go to System > Configuration > Catalog > Catalog Search.
3.    You will find an option there, Search Type. Just change it to Like & Full Text.

That's done.. Now just flush Magento Cache and enjoy.. :)

Note:  This solution has been tested on Magento version 1.7.0