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

Get Last Order ID From The Session in Magento

If you want to get last order Id from session, just use this code:


Mage::getSingleton('checkout/session')->getLastOrderId();


This will give you last order id.


That's all.. :)

7/27/2012

Getting records between dates using addAttributeToFilter


To get records between two dates using addAttributeToFilter, use code like this:

$collection->addAttributeToFilter('start_date', array('date' => true, 'to' => $todaysDate))
    ->addAttributeToFilter('end_date', array(array(
        array('date' => true, 'from' => $todaysDate),
        array('null' => true)
    ), 'left');

Note: Where $collection is collection from your model.
Note: $todaysDate can be replaced by date of your choice.

That's all.. cheers.. :)

7/24/2012

How to change the Magento Admin Panel Title?

If you want to change default title of Magento Admin, there is an easy solution:
Just open the main.xml file, which is located in app/design/adminhtml/default/default/layout folder.
Find line like below: 
<action method="setTitle" translate="title"><title>Magento Admin</title></action>
 and replace the words Magento Admin with the words you like to be in default title.

That's all.. Enjoy.. :)

7/23/2012

FIX: Magento Contact Form Email showing HTML

Some times we face a problem with Magento contact form, where the contact email shows all the HTML code.

In order to fix this problem, try this:

Go to [Magento Root Folder]/app/locale folder, then go to your language folder (such as en_US) and place the contactemail.html file in template/email folder from base language package.


Thats all.. Enjoy.. :)

7/22/2012

Magento: Disable category from top menu if list is empty for that category

Hi friends,

Many times we face a problem that we add categories to our magento store, but for one or more categories, we don't have products to show customer at that time.

In that case, we may not want to show that category in top menu, as it gives a bad impression to customers that there is no product in category.

You can hide that particular category from Top Menu, if it has no product. To do this, go to:

app/code/core/Mage/Catalog/Block Folder and copy Navigation.phpOverride Navigation.php in your local package.

Open Navigation.php of your package and paste below code in this file:


if ($category->getIsActive()) {
    $cat = Mage::getModel('catalog/category')->load($category->getId());
    $products = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($cat);
    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
    Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($products);
    if(count($products)==0)
       return;
}

Save file and enjoy.. :)