Hiding Magento's Layered Navigation from Search Engines - magento

I’m hoping someone can help me with this problem I’ve been trying to solve for the past few days. I want to hide Magento’s Layered Navigation from the search engines entirely, but make it available to users. For SEO reasons, I don’t want to settle for NoFollowing all the links, or using noindex follow meta tags, or even blocking it entirely with Robots.txt. The most effective way of handling this would be only showing the layered Navigation to users with Cookies enabled, since Google doesn’t use cookies. The same effect could probably be achieved with JavaScript as well, but I’ve chosen the Cookie method.
So far I’ve managed to implement a crude piece of JS to check if cookies are enabled once the page has loaded (adapted from another thread on this forum). If cookies are enabled, it does nothing and layered nav displays, but if cookies are not enabled, I want to remove the “catalog.leftnav” block. I can’t for the life of me figure out how to do this from my JS script. All I’ve been able to achieve is removing the div element, or setting style.display to none etc., and while all of these techniques remove the links from the frontend, Google can still see them all. Here’s an example of the code I have so far in template/catalog/layer/filter.phtml
<div id="shop-by-filters">
<ol>
<?php foreach ($this->getItems() as $_item): ?>
<li>
<?php if ($_item->getCount() > 0): ?>
<?php echo $_item->getLabel() ?>
<?php else: echo $_item->getLabel() ?>
<?php endif; ?>
<?php if ($this->shouldDisplayProductCount()): ?>
(<?php echo $_item->getCount() ?>)
<?php endif; ?>
</li>
<?php endforeach ?>
</ol>
</div>
<script type="text/javascript">
if (navigator.cookieEnabled) {
return true;
} else if (navigator.cookieEnabled === undefined) {
document.cookie = "testcookie";
if (cookie_present("testcookie"))
return true;
} else {
var elem = document.getElementById('shop-by-filters');
elem.parentNode.removeChild(elem);
}
</script>
Can anyone help me with this, or is there a better way of going about it? Please keep in mind that I am still trying to get my head around Magento, so I might need some instructions if the implementation is complicated.
Thank you.
Brendon

I'm not sure if the Google robot will reliably parse your javascript.
You may be better off hiding the layered nav based on the current session with php.
<?php if (Mage::getSingleton('customer/session')): ?>
...your nav code...
<?php endif ?>

First of all, Javascript will do nothing to stop Google from indexing that content.
Why don't you want 'to settle for NoFollowing all the links'? That is exactly what NoFollow is for. You can also tell Google to not pay attention to the qualifiers/query strings in Webmaster Tools.
If for some reason you really wanted to hide that block from Google, edit the template and string compare $_SERVER['HTTP_USER_AGENT'] against Google's very public list of user agents here http://support.google.com/webmasters/bin/answer.py?hl=en&answer=1061943
EDIT -- string compare
<?php if (stripos($_SERVER['HTTP_USER_AGENT'], 'Googlebot') !== false): ?>
<div id="shop-by-filters">
<ol>
<?php foreach ($this->getItems() as $_item): ?>
<li>
<?php if ($_item->getCount() > 0): ?>
<?php echo $_item->getLabel() ?>
<?php else: echo $_item->getLabel() ?>
<?php endif; ?>
<?php if ($this->shouldDisplayProductCount()): ?>
(<?php echo $_item->getCount() ?>)
<?php endif; ?>
</li>
<?php endforeach ?>
</ol>
</div>
<?php endif; ?>

It is a slick subject. We used this code to hide the layered navigation from Google, but we are not sure is it working...
<div id="filters-no-follow"></div>
<?php
function prepare_for_echo($string) {
$no_br = trim(preg_replace('/\s+/', ' ', $string));
$no_slashes = str_replace('\'', '\\\'', $no_br);
return $no_slashes;
}
?>
<script>
function please_enable_cookies() {
var f = document.getElementById('filters-no-follow');
f.innerHTML = '<div class="no-cookies-error">Enable cookies to choose filters.</div>';
}
function please_load_filters() {
var f = document.getElementById('filters-no-follow');
f.innerHTML = '<?php if ( !empty($filtersHtml) || !empty($stateHtml) ): ?>'
+ '\n<div class="block block-layered-nav">'
+ '\n <div class="block-title">'
+ '\n <strong><span><?php echo prepare_for_echo($this->__('Shop By')); ?></span></strong>'
+ '\n </div>'
+ '\n <div class="block-content">'
+ '\n <?php echo prepare_for_echo($this->getStateHtml()); ?>'
+ '\n <?php if ($this->canShowOptions()): ?>'
+ '\n <p class="block-subtitle"><?php echo prepare_for_echo($this->__('Shopping Options')); ?></p>'
+ '\n <dl id="narrow-by-list">'
+ '\n <?php echo prepare_for_echo($filtersHtml); ?>'
+ '\n </dl>'
+ '\n <?php endif; ?>'
+ '\n </div>'
+ '\n</div>'
+ '\n<?php endif; ?>';
}
function are_cookies_enabled()
{
var cookieEnabled = (navigator.cookieEnabled) ? true : false;
if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled)
{
document.cookie="testcookie";
cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
}
return (cookieEnabled);
}
if(are_cookies_enabled()) {
please_load_filters();
} else {
please_enable_cookies();
}
</script>

Related

How to display category description only on the first page in Magento

I'm trying to display category description only on the first page. The following code is not working - any idea how to fix it?
<div class="category-description std">
<?php
if (strlen($_SERVER['QUERY_STRING']) = 0 || $_GET['p'] = '1')
{
echo $_helper->categoryAttribute($_category, $_description, 'description');
}
?>
</div>
Please try this one :
<?php if($_description=$this->getCurrentCategory()->getDescription()): ?>
<?php $currentPage = (int) Mage::App()->getRequest()->getParam('p');
if($currentPage <= 1):
?>
<div class="category-description std">
<?php echo $_helper->categoryAttribute($_category, $_description, 'description') ?>
</div>
<?php endif; ?>
<?php endif; ?>'
You can use the products list toolbar to know if you're on the first page :
if ($this->isContentMode()
|| $this->getChild('product_list')->getToolbarBlock()->isFirstPage()) {
// Display description (assuming that content mode is always first page)
}

Magento: textbox instead of multi select in layered navigation

Is there a possibility in Magento to have a multi-select attribute, for which I would use a textbox in layered navigation instead of showing all items from multi-select?
I have an attribute where I will have hundreds of options and need to use it in layered navigation.
When customer uses invalid value, an error should show up.
Edit: After the help from FlorinelChis I have folowing code in filter.phtml:
<ol>
<?php foreach ($this->getItems() as $_item): ?>
<?php
$attributeModel = $this->getAttributeModel();
$attribute_code = $attributeModel->getAttributeCode();
?>
<li>
<?php if ($attribute_code != 'available_zip'): ?>
<?php if ($_item->getCount() > 0): ?>
<?php echo $_item->getLabel() ?>
<?php else: echo $_item->getLabel() ?>
<?php endif; ?>
<?php if ($this->shouldDisplayProductCount()): ?>
(<?php echo $_item->getCount() ?>)
<?php endif; ?>
<?php endif; ?>
</li>
<?php endforeach ?>
</ol>
<?php
if ($attribute_code == 'available_zip'):
$cat = Mage::registry('current_category')->getUrlPath() ;
$url = Mage::getBaseUrl();
/*$sendUrl = $this->urlEscape($_item->getUrl()).'+'.$url.$cat.'?'.$attribute_code.'='.$_item->getValue();*/
echo '<form action="" method="get">';
echo '<input type="text" size="5" maxlength="5" name="'.$attribute_code.'" />';
echo '<button type="submit">OK</button>';
echo '</form>';
endif; ?>
I have one more thing now:
how to send the form with attribute id instead of value?
First, let's find out how Magento displays the filters in left hand navigation
1) Enable Template Path Hints:
and here is the result:
2) Let's take a look at app/design/frontend/base/default/template/catalog/layer/filter.phtml (you should copy this file in your theme folder structure)
3) The block ($this in the template file is an instance of Mage_Catalog_Block_Layer_Filter_Attribute which extends Mage_Catalog_Block_Layer_Filter_Abstract)
4) You can see that in Mage_Catalog_Block_Layer_Filter_Abstract a getName() method exists, so you could rely on this function to identify when you attribute is displayed. Don't! The label can change and your code won't be useful any more.
Back to our template file, you can get the attribute_code (which is reliable)
//$this is instance of Mage_Catalog_Block_Layer_Filter_Attribute
$attributeModel = $this->getAttributeModel();
$attribute_code = $attributeModel->getAttributeCode();
5) On your template you can have a check based on attribute code, so you display either the standard list or your custom html code with a textarea instead of a huge list.

Magento - Adding layered navigation to custom page

I'm working on a magento module that shows particular product collections under a new controller and front-name.
Some of these collections get big, so I'd like to add layered navigation to the side of the page. (And hey, pagination and sort while we're at it.)
I can add the layered navigation block with
<reference name="left">
<block type="catalog/layer_view" name="catalog.leftnav" template="landing/layer.phtml"/>
</reference>
What I get with that is the layered navigation as applied to the whole catalog, with categories broken, and no interface with the on-page product collection.
How would I go about wiring up the layered navigation (and hey, pagination and sort) to this custom product collection?
No answers were forthcoming here, and I changed direction and never completed this path of development. Figured I'd post what I'd learned.
The approach above is sound. The work basically entails recreating the functionality on the catalog or catalogsearch modules. You'll have to subclass the models and blocks of catalog, modifying the product collection and current category.
This post heads vaguely in the right direction, but doesn't get there.
http://www.webdesign-gm.co.uk/news/layered-navigation-on-home-page-or-any-cms-page-magento.php
If you make more headway on this, feel free to post.
I've had a similar request from a client to include specific filterable attributes under a mega menu.
Using a static block i've added this line to everywhere i needed a listing of attributes for specific category
{{block type="core/template" attribute_code="age_specific" category_id="12" template="megamenu-custom-filter-list.phtml"}}
And then the "megamenu-custom-filter-list.phtml"
<?php if($this->getCategoryId() && is_numeric($this->getCategoryId()) && $this->getAttributeCode()): ?>
<?php
$visible_items = 12;
$category = Mage::getModel('catalog/category')->load($this->getCategoryId());
$attrCode = $this->getAttributeCode();
unset($layer);
$layer = Mage::getModel("catalog/layer");
$layer->setCurrentCategory($category);
$attributes = $layer->getFilterableAttributes();
foreach ($attributes as $attribute):
if($attribute->getAttributeCode() != $attrCode){
continue;
}
if ($attribute->getAttributeCode() == 'price') {
$filterBlockName = 'catalog/layer_filter_price';
} elseif ($attribute->getBackendType() == 'decimal') {
$filterBlockName = 'catalog/layer_filter_decimal';
} else {
$filterBlockName = 'catalog/layer_filter_attribute';
}
$result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
?>
<div>
<h4 class="menu-block-title"><?php echo $this->__($attribute->getFrontendLabel()) ?></h4>
<ul class="menu-attr-list">
<?php $counting = 1; ?>
<?php foreach($result->getItems() as $option): ?>
<?php $optionUrl = $category->getUrl() . "?" . $attribute->getAttributeCode() . "=" . $option->getValue(); ?>
<?php if(!$option->getCount()){ continue; } ?>
<li class="<?php echo ($counting >= $visible_items+1)?"visible-on-showmore":"" ?>" style="list-style: none;">
<a class="cube" href="<?php echo $optionUrl ?>">
<?php echo $option->getLabel() ?> <?php /* (<?php echo $option->getCount() ?>) */ ?>
</a>
</li>
<?php $counting++; ?>
<?php endforeach; ?>
<?php if($counting >= $visible_items+1): ?>
<li class="show-more-menuitem">
<a href="javascript:void(0)" onclick="showMoreMenuItems(this); return false;" class="">
<?php echo $this->__('Visa fler') ?>
</a>
</li>
<?php endif; ?>
</ul>
</div>
<?php endoreach; ?>
<?php endif; ?>
Of course, this can be extended to allow all attributes to be shown and not limit them to 12 (as i did here). Although, i've not build a feature to show this for a custom collection, but i believe that following the "catalog/layer" model and see where the collection was loaded, you can see how to overwrite it and load your own custom collection.

How to add submenu on hover effect in left navigation in magento?

I have vertnav/left.phtml file code,
<div class="vertnav-container">
<div class="">
<h4 class="no-display"><?php echo $this->__('Category Navigation:') ?></h4>
<?php $store_categories = $this->toLinearArray($this->getStoreCategories()) ?>
<?php if ($count = count($store_categories)): ?>
<ul id="vertnav">
<?php endif; ?>
<?php foreach ($store_categories as $i => $_category): ?><?php $class = array() ?>
<?php if ($count == 1): ?>
<?php $class[] = 'only' ?>
<?php elseif (! $i): ?>
<?php $class[] = 'first' ?>
<?php elseif ($i == $count-1): ?>
<?php $class[] = 'last' ?>
<?php if (isset($store_categories[$i+1]) && $this->isCategoryActive($store_categories[$i+1])) $class[] = 'prev'; ?>
<?php if (isset($store_categories[$i-1]) && $this->isCategoryActive($store_categories[$i-1])) $class[] = 'next'; ?>
<?php echo $this->drawOpenCategoryItem($_category, 0, $class) ?>
<?php endforeach ?>
<?php if ($count): ?>
</ul>
<?php endif; ?>
</div>
</div>
and set System > Configuration > Catalog > Category Vertical Navigation to 2 as per my requirement, but now on mouseover on that displayed category subcategories should be shown
so how can i do customization to that and add hover effect code to this?
Please help me
If you take a closer look at the drawOpenCategoryItem of the Mage_Catalog_Block_Navigation you might notice that the method does a check whether the given category is part of the current category path. So only when this check returns true, the children categories of this category will be rendered. For other categories the script will not go into that part of the code.
This sounds to be case if I understand your question correctly.
if (in_array($category->getId(), $this->getCurrentCategoryPath())){
$children = $category->getChildren();
$hasChildren = $children && $children->count();
if ($hasChildren) {
$htmlChildren = '';
foreach ($children as $child) {
$htmlChildren.= $this->drawOpenCategoryItem($child);
}
if (!empty($htmlChildren)) {
$html.= '<ul>'."\n"
.$htmlChildren
.'</ul>';
}
}
}
Additional to this information. The drawOpenCategoryItem() is never actually called upon in the whole PHP codebase.
So to have these rollover effects you need to have code that generates the full tree structure, or at least a big enough part of it. Regarding the System > Configuration > Catalog > Category Vertical Navigation. I guess you customized that yourself?
To give you a few pointers. You might want to have a look at the following methods. These are used for the rendering of the top menu and are actually doing the thing you are planning to implement.
Mage_Catalog_Block_Navigation::renderCategoriesMenuHtml()
Mage_Catalog_Block_Navigation::_renderCategoryMenuItemHtml()
Hopes this helps you getting things underway.

Display Magento order comments in print.phtml (customers prinable order)

Just wondering if anyone has any idea how to show comments on the customers printable order - http://www.mydomain.com/sales/order/print/order_id/48/
I can see the file that I need to edit is “/public_html/app/design/frontend/default/mytemplate/template/sales/order/print.phtml” but am unsure what code I need to add to display the comments.
FYI: We are using this extension to make the order comments box show up on the order page - http://www.magentocommerce.com/magento-connect/catalog/product/view/id/10860/. The order comments are successfully displayed on the order email but we need them be be displayed on the customers order pages as well.
Thanks for your help in advance :)
+1 for code_break, who answered this nicely. Here's my own version for completeness:
$orders = Mage::getModel('sales/order')
->getCollection()
->addFieldToFilter('status',array('pending','processing'));
foreach ($orders as $order) {
$orderComments = $order->getAllStatusHistory();
foreach ($orderComments as $comment) {
$body = $comment->getData('comment');
if (strpos(strtolower($body),'some text') !== false) {
// do something cool here...
}
}
}
Use as you wish. Hope it helps.
The last post used the getVisibleStatusHistory method of the order object, but the first comment entered on an order is never visible. There are several methods for grabbing the status history and setting it in the order object.
That being said we might want to list all of the comments that are marked as visible on front-ed and the first comment entered when the order is created. I've substitued your formatting with a <p> tag.
<?php $_history = $order->getAllStatusHistory(); ?>
<?php $_buffer = array(); ?>
<?php $_i=1; ?>
<?php foreach ($_history as $_historyItem): ?>
<?php // Ignore the visibility for the first comment ?>
<?php if ( $_historyItem->getData('is_visible_on_front') == 1 || $_i == count($_history) ): ?>
<?php $_buffer[] = $_historyItem->getData('comment'); ?>
<?php endif; ?>
<?php $_i++; ?>
<?php endforeach; ?>
<?php if ( count($_buffer) > 0 ): ?>
<p><?php echo implode( $_buffer, '</p><p>' ); ?></p>
<?php endif ?>
As you are asking especially for the oder comment from MageMaven OrderComment this would be the easiest solution:
<p><?php echo nl2br($_order->getCustomerNote()); ?></p>
Hey try adding this code I havent tested it but i have a feeling it will work for you:
<?php $_history = $_order->getVisibleStatusHistory() ?>
<?php if (count($_history)): ?>
<div class="order-additional order-comments">
<dl class="order-about">
<?php foreach ($_history as $_historyItem): ?>
<dd>
<span class='lowcase'><?php echo $_historyItem->getComment()?></span>
</dd>
<?php endforeach; ?>
</dl>
</div>
<?php endif?>

Resources