Tuesday, June 7, 2011

Add category names to the product list

In order to add category names to the product list (grid)
you have 2 options. For both of them you have to edit app/design/frontend/{interface}/{theme}/template/catalog/product/list.phtml
1. Add the category (categories) link(s) for all the products in any list (search results included). Keep in mind that any product can be in one or more categories (or none).
At the top of the file add this:
<?php $_category = Mage::regsitry('current_category');?>
<?php if ($_category) : ?>
<a href="<?php echo $_category->getUrl();?>"><?php echo $_category->getName();?></a>
<?php endif;?>
2.
At the top of the file add this:
<?php $_categories = array();?>
This will work as some kind of cache.
Now below the $_product item (inside the foreach) add this
<?php foreach ($_product->getCategoryIds() as $categoryId) : ?>
 <?php if (isset($_categories[$categoryId])) : ?>
  <?php $_category = $_categories[$categoryId];?>
 <?php else:?>
  <?php $_category = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($categoryId);?>
  <?php $_categories[$categoryId] = $_category;?>
 <?php endif;?>
 <a href="<?php echo $_category->getUrl()?>"><?php echo $_category->getName();?></a>
 <?php //if you want only the first category link add a break here; ?>
<?php endforeach;?>
This second method covers all the cases but it's a little bit slower than the first one.

Thursday, June 2, 2011

How to change 'My account' menu link in the customer name for logged in users

Hello.
Here is how you can change the 'My account' menu link into the customer name (John Doe) for logged in users.
I'm going to use the same system that the checkout module uses to add the cart and the checkout link.
First of all create a new module that will extend the Mage_Customer module. We will call it Custom_Customer.
In app/code/local folder create a folder named 'Custom'.
[UPDATE]
Inside the Custom folder create a folder 'Customer'.
/* Thanks for the comment 'Anonymous' - see first comment*/
[/UPTATE]
In this folder create a subfolder called 'Block' (because we need a new block).
Inside 'Block' folder create a php file named 'Links.php' with this content
<?php 
class Custom_Customer_Block_Links extends Mage_Core_Block_Template
{
/**
ads my account link to some block
*/
    public function addAccountLink()
    {
        $parentBlock = $this->getParentBlock();//this block will have a parent
        if ($parentBlock && Mage::helper('core')->isModuleOutputEnabled('Mage_Customer')) {//check if the customer module is not disabled
            $customerSession = Mage::getSingleton('customer/session');//the customer session

            if( $customerSession->isLoggedIn() ) {//if the customer is logged in get the name
                $text = $customerSession->getCustomer()->getName();
            } else {//if the customer is not logged in print 'My account'
                $text = Mage::helper('customer')->__('My Account');
            }
//add the block to the set of links.
            $parentBlock->addLink($text, Mage::helper('customer')->getAccountUrl(), $text, false, array(), 10, null, '');
        }
        return $this;
    }
}

Now create the config.xml file for the module.

In folder 'Custom' create a subfolder 'etc' and inside it the config.xml with this content.
[UPDATE]
inside the Customer folder create a subfolder 'etc' and inside it the config.xml with this content.
/* Thanks for the comment 'Anonymous' - see first comment*/
[/UPTATE]
<?xml version="1.0"?>
<config>
    <modules>
        <!-- the  version of the module-->
        <Custom_Customer>
            <version>0.0.1</version>
        </Custom_Customer>
    </modules>
    <!-- Hack to tell magento that our block overrides a core block (that does not exits) -->
    <global>
     <blocks>
      <customer>
       <rewrite>
        <links>Custom_Customer_Block_Links</links>
       </rewrite>
      </customer>
     </blocks>
    </global>
</config>
Now we have to use out block in the design.
in app/design/frontend/{interface}/{theme}/layout/customer.xml replace this:
<default>
        <!-- Mage_Customer -->
        <reference name="top.links">
            <action method="addLink" translate="label title" module="customer"><label>My Account</label><url helper="customer/getAccountUrl"/><title>My Account</title><prepare/><urlParams/><position>10</position></action>
            
        </reference>
    </default>
with this
<default>
        <!-- Mage_Customer -->
        <reference name="top.links">
            <!-- THe block we've just created and the method to add the link -->
            <block type="customer/links" name="customer_links">
             <action method="addAccountLink" />
            </block>
        </reference>
    </default>

Now all we have to do is to tell Magento that we have a new module.
in app/etc/modules create a new file called Custom_Customer.xml (the name is not important) with this content:
<?xml version="1.0"?>
<config>
    <modules>
        <Custom_Customer>
            <active>true</active>
            <codePool>local</codePool>
            <depends><Mage_Customer /></depends>
        </Custom_Customer>
    </modules>
</config>

Clear the contents of 'var/cache' and enjoy.

Tested on Magento CE 1.5.1.0