Magento – undefined index in “toolbar.phtml” – after changing default sort by

Recently I added a new product-attribute and it was set to be the default sortby attribute. To my surprise, I used to get an undefined index on some categories.
I failed to get a clear solution in any of the article from Magentocommerce site. On further investigation I realised that just setting the default sort by option will not be sufficient.
It is also necessary to check if you have selected this new attribute in the list of “Available Product Listing Sort by” for the category which is causing this issue.
So I logged into Magento BE, and used following path
Catalog >> Manage Categories >> Selected the category that raised the error >> Display Settings [TAB] >> Available Product Listing Sort by
Her I noticed that the new product attribute was not selected!!! Seems Simple, still…!!!

I am not sure if this error can also get triggered when we use default Toolbar.phtml without any modification.

Additional info:

Related to: A custom designed toolbar.phtml which was primarily used to modify list of sortby options.
Scenario: Error gets displayed when I click on a root category or when I try to use magento search.
Quick Solution: Make sure you have selected this new attribute in the list of “Available Product Listing Sort by” for the category that caused this error.

I am sure this might be rare case, but it can happen. Happy shopping!

Adding a new product attribute to magento and changing the default sort by option in magento

The magento screencast on adding a new product attribute is really what would give a clear idea on adding prodcut attribute.

Step 1: Follow the screencast and add new attribute
http://www.magentocommerce.com/media/screencasts

Step 2: Since you want this attribute to be used for sorting on list view, make sure the option “Used for sorting in product listing” is set to YES.

Step 3: Add the new attribute to the required attribute set.

Step 4: Go to System > configuration > Catalog > Catalog > Frontend

Step 5: Set the “Product listing sort by” option to the new field you added.

Click on the screenshot above for details.

That’s all, save the configuration, clear cache (if enabled) and relaod the list view. You should see the new attribute.

NOTE:
If you are using a custom template /package, chances are that you may have to change the code manually to view the new sort by field in the selection list.
The code is usually located in in catalog/product/list/toolbar.phtml

Adding search by Category feature to Magento Advanced search and catlog search in Product list view

I recently had to add a search form in the Magento product list view and I found the following Wiki article:

http://www.magentocommerce.com/wiki/how-to/how_to_add_search_by_category_to_advanced_search

This article would work 100% perfect, however the steps given in article is not really upgrade safe.

There were hence two options either to include these steps in a seperate magento module, or to follow the steps I have given here.
This is not really a perfect practice, however, if we are not aware of writing a magento module then this can be a very quick alternative.
This step also protects us from our work being over-written when we make a magento upgrade.

Step 1:
Create a new folder called “Mage” inside app/code/local

Now we have anew folder app/code/local/Mage

Step 2:
If I see the steps given in Magento wiki, we notice that the they are extending the features of the CatalogSearch module that is located in the namespace Mage,
So just go to “app/code/core/Mage” and copy the entire folder named “CatalogSearch” into app/code/local/Mage

Now we have the entire “CatalogSearch” module located in app/code/local/Mage/CatalogSearch

Step 3:
Now all we need to do is follow the exact steps given in the wiki article, however make sure to edit files located in app/code/local/Mage/CatalogSearch only.

Link to the wiki: http://www.magentocommerce.com/wiki/how-to/how_to_add_search_by_category_to_advanced_search

How to add catalog search in Magento product list view or the Search by category feature in Magento product list view.

This simply involves adding a search form in prodcut list view we however need to ensure that the category id is passed to the advanced search form using a hidden input.

Add the following code to

“app\design\frontend\YOURPACKAGENAME\default\template\catalog\product\list\toolbar.phtml”
OR
“app\design\frontend\YOURPACKAGENAME\default\template\catalog\product\list.phtml”

<?php if( !isset($_GET[‘category’]) ): ?>
<form action=”<?echo $this->getUrl(‘catalogsearch/advanced/result’)?>” method=”get” id=”category_search_form”>
<?php

if ($this->helper(‘catalog/data’)->getCategory()) {
$_ccat = $this->helper(‘catalog/data’)->getCategory();
} else {
$_ccats = $this->helper(‘catalog/data’)->getProduct()->getCategoryIds();
$_ccat = Mage::getModel(‘catalog/category’)->load($_ccats[0]);
};
$_ccatID = $_ccat->getId();
?>
<input type=”hidden” id=”category_search_field” name=”category” value=”<?php echo $_ccatID ?>” />
<div class=”left”>
<input name=”name” id=”searchcat” value=”<?php echo $this->__(‘Search Category’) ?>” title=”Name” class=”input-text ” type=”text” />
<input type=”image” alt=”<?php echo $this->__(‘Search’) ?>” src=”<?php echo $this->getSkinUrl(images/ico_s_search.png’)?>” class=”cw_buttonicon” />
</div>
</form>
<script type=”text/javascript”>
//<![CDATA[
var searchForm = new Varien.searchForm(‘category_search_form’, ‘searchcat’, ‘<?php echo $this->__(‘Search Category’) ?>’);
searchForm.initAutocomplete(‘<?php echo $this->helper(‘catalogSearch’)->getSuggestUrl() ?>’, ‘search_autocomplete’);
//]]>
</script>
<?php endif ?>

Note:
1.The first if statement <?php if( !isset($_GET[‘category’]) ): ?> makes sure that we will not have two forms in the advanced search result page.
You should retain this if statement because when we are in advanced search we will no longer be in the previously selected category!

2.
You may need to change the path of search image.

3. I have also included the autocomplete feature, so be careful if you are changing the name or ID attribute of the form or the input tags.

Hope this article would be useful, kindly do not forget to write your comments here. Thank you.

How to change Base URL in Magento

Sometimes it may be essential to change the BASE URL of Magento.
(Usually when you want to make a similar copy of Magento on another server!)

There are 2 – 3 simple steps involved in this:

Step 1: Edit old URL values in database
Table: core_config_data
In this table you need to edit the field named value containing specific values in the field named path!
Precisely saying you need to search for web/unsecure/base_url and web/secure/base_url and replace them with new values.

Please refer the images for an example.
OLD Values:
 image

New Values:
image

Step 2: Login to backend and clear Configuration cache!
This might be simply not possible and hence skip to next step!

Step 3: Find All entries of old domain name in the cache folder and replace them one by one.
Magento retains all configuration cache in /var/cache folder.
This step is  good for Advanced Users who can do file search and replace such strings in one shot 😉
If you are migrating a very big site, then this step will be certainly worth.

I use this “Unix Find Command” very useful in searching:


find . -exec grep -q www.magdummy.cws '{}' \; –print

Step 3: Now it’s time to clear the cache! (Normal User)
As already specified in step2, we know that Magento retains all configuration cache in /var/cache folder.
You can clear the entire contents in this cache folder. Magento regenerates all these files when needed.

Step4: View your new site in the browser.
It should now be working fine like the old one 😉

I hope this would work fine for you.
Please leave your comments or I would appreciate any idea that suits best in this case.