10/23/2012

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)

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

7/30/2012

Hide Attributes which have no value in Magento

If you don't provide a value of attribute for a product in Magento, it displays as 'No' rather than hiding that attribute. This means attributes that aren't relevant for a product will still appear confusing the user. Which is a bad impression of your website.

To hide these unnecessary attributes, do the following:


Open attribute.phtml, which is located in app/design/frontend/[your package]/[your theme]/template/catalog/product/view folder, and find code like this:



<?php foreach ($_additional as $_data): ?>
    <tr>
        <th><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
        <td><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
    </tr>
<?php endforeach; ?>


Now replace this by:



<?php foreach ($_additional as $_data): ?>
    <?php $_attribute = $_product->getResource()->getAttribute($_data['code']);
    if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != ")) { ?>
        <tr>
            <th><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
            <td><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
        </tr>
    <?php } ?>
<?php endforeach; ?>


and you are done.. enjoy.. :)


Get The Current Category in Magento


Try below code to get currently loaded product id:

$category_id = Mage::getSingleton('catalog/layer')
               ->getCurrentCategory()
               ->getId();

When you don’t have access to $this, you can use Magento registry:

$category_id Mage::registry('current_category')->getId();

Enjoy.. :)