All about Magento E-commerce Store.......MagentoForum: June 2011

Tuesday, June 28, 2011

How to use Magento debug tool


This tutorial will show you how to use the Magento debugging tools to easily locate the module names and core source files.
Let’s see what advantages give us the Magento debug tools. Here you can see the default Magento store home page without any products installed. You can hardly say what each block does and how to edit it.
With the Debug tools you can see the module system name and core file.
To enable the debug tools open the Magento admin panel, go to System > Configuration page. In the Current Configuration Scope block (left column top block) select Default Store View.
Now scroll down and click the Developer link and open the Debug tab.
For the options Template Path Hints and Add Block Names to Hints uncheck the Use default checkbox and select Yes. Then click Save Config button in the top left corner.

Monday, June 27, 2011

Magento:How to change admin url path

Here is a quick guide on how to change admin url path in Magento. This need to be done for security reason to be safe from hacking/cracking issue. Basically, this is done not to let any general user to access the admin page.
Generally, we do have 'admin' as the administrator path for Magento. So, the admin URL will be http://www.example.com/admin/

This article will show you, how you can change the admin url. Let's say from 'admin' to 'backend'. So, the new admin URL will be http://www.example.com/backend/
Here is how we do it:-
- Open app/etc/local.xml
- Find the following:-
<admin><routers><adminhtml><args><frontName>
<![CDATA[admin]]></frontName></args></adminhtml></routers></admin> 
- Change
<frontName><![CDATA[admin]]></frontName>
to your desired name. Like below:-
<frontName><![CDATA[backend]]></frontName> 
- Save the file
- Refresh the Cache from Magento Admin (System -> Cache Management)
Now, you should be able to access admin panel from http://www.example.com/backend/ instead of http://www.example.com/admin/

Saturday, June 11, 2011

Disabling customer logging


All the tables that start with 'log_' become pretty big after some uptime.
If you don't need these here is a solution to disable them.
Create your own Magento extension. Let's call it 'Custom_Log'.
For this you need to create in app/code/local the folder 'Custom' and inside it the folder 'Log'.
Each module needs a config file.
Create inside the 'Log' folder a folder named 'etc' and inside it a file called config.xml with this content:

<?xml version="1.0"?>
<config>
<modules>
<Custom_Log>
<version>0.0.1</version>
</Custom_Log>
</modules>
<frontend>
<!-- [+] Disable logging -->
<events>
<controller_action_predispatch>
<observers>
<log>
<type>disabled</type>
</log>
</observers>
</controller_action_predispatch>
<controller_action_postdispatch>
<observers>
<log>
<type>disabled</type>
</log>
</observers>
</controller_action_postdispatch>
<customer_login>
<observers>
<log>
<type>disabled</type>
</log>
</observers>
</customer_login>
<customer_logout>
<observers>
<log>
<type>disabled</type>
</log>
</observers>
</customer_logout>
<sales_quote_save_after>
<observers>
<log>
<type>disabled</type>
</log>
</observers>
</sales_quote_save_after>
<checkout_quote_destroy>
<observers>
<log>
<type>disabled</type>
</log>
</observers>
</checkout_quote_destroy>
</events>
<!-- [-] Disable logging -->
</frontend>
</config>

now you need to tell Magento to use your module.
In app/etc/modules create the file 'Custom_Log.xml' with this content:
<?xml version="1.0"?>
<config>
<modules>
<Custom_Log>
<active>true</active>
<codePool>local</codePool>
</Custom_Log>
</modules>
</config>

now just clear the cache (contents of folder var/cache) and you are done.
The downside of doing this is that you won't get any details of the customers visits. But you can live with that. There's Google Analytics for this

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;?>
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 $_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.

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

Here is how you can change the 'My account' menu link into the customer name (Amrit Heda) 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'.
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.
<?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

Friday, June 10, 2011

Sort latest product by ‘created date’ and ‘new from date’

Here is a quick tip to sort/order latest product by both created date and
new from date.
Created At date is automatically added when you add a new product. However,
you have to set New from Date manually while adding or editing product.
You may have product with same new from date. And, then you need created at
date to sort the latest product.
Here is the code to sort/order product by both created date and new from
date:-
$todayDate =
Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToFilter('news_from_date', array('date' => true, 'to' =>
$todayDate))
->addAttributeToFilter('news_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
->addAttributeToSort('news_from_date', 'desc')
->addAttributeToSort('created_at', 'desc');

Hope this helps. Thanks.