Searchresult wrong. Mage::registry('current_layer') not set - magento

We redesign a magento shop. That shop had a very costumized searchresult page.
Now, after the redesign, the search does not work anymore.
I broke down the problem to the List.php (Mage\Catalog\Block\Product\List.php) where the function _getProductCollection() is called to get the results. This function gets a layer $layer = $this->getLayer(); and there is the main difference to the old version:
public function getLayer()
{
$layer = Mage::registry('current_layer');
if ($layer) {
return $layer;
}
return Mage::getSingleton('catalog/layer');
}
In the old version the Mage::registry('current_layer'); is set. In the new version its not set! And when it is not set it returns a new one return Mage::getSingleton('catalog/layer'); with all the products in it.
I can't think any further. How could I find the place where this layer is set in the old version?
What else could I do?

The search layer model is registered by the catalogsearch/layer block, which is usually added to the search page by the catalogsearch.xml layout file :
<catalogsearch_result_index translate="label">
<label>Quick Search Form</label>
<reference name="root">
<action method="setTemplate"><template>page/3columns.phtml</template></action>
</reference>
<reference name="left">
<block type="catalogsearch/layer" name="catalogsearch.leftnav" after="currency" template="catalog/layer/view.phtml"/>
</reference>
<!-- ... -->
</catalogsearch_result_index>
As it has certainly gone missing, you should add it back to your layout.

Related

magento layered navigation on home page - filtered items have wrong urls, causes 404

After a lot of searching, I finally found a solution to add Layered navigation to the Magento Home Page. At first glance, it was working properly with filtered results as expected.
However, there is a catch as the URLs for the filtered results all have an added 'root-catalog' in their urls. This causes a 404 - however, if I take out the 'root-catalog' the urls are working fine.
What am I missing? Please help!
Help is appreciated in advance!
Code to add layered navigation to home page:
<reference name="left">
<block type="catalog/navigation" name="catalog.cat.leftnav" before="sidenav.left" template="catalog/navigation/left.phtml"/>
<block type="catalog/layer_view" name="catalog.leftnav" after="catalog.cat.leftnav" template="catalog/layer/view.phtml"/>
<action method="unsetChild"><alias>right.reports.product.viewed</alias></action>
<action method="unsetChild"><alias>right.reports.product.compared</alias></action>
</reference>
<reference name="content">
<block type="catalog/product_list" name="product_home" template="catalog/product/list.phtml">
<action method="setCategoryId">[b]<category_id>3</category_id>[/b]</action>
<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
<block type="page/html_pager" name="product_list_toolbar_pager" />
</block>
<action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
</block>
</reference>
Layered navigation needs a category on which to base to get products, sub categories on which to filter, etc. On the homepage then, it takes the root category by default, and the corresponding product collection acts like it would do on other "common" category pages : it uses URL rewrites linking to the current category.
To prevent current category to be used in product URL, you can rewrite the layer model by copying the file app/code/core/Mage/Catalog/Model/Layer.php to app/code/local/Mage/Catalog/Model/, and changing the prepareProductCollection($collection) in it with something like this :
public function prepareProductCollection($collection)
{
$collection
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents();
if ($this->getCurrentCategory()->getId() == $this->getCurrentStore()->getRootCategoryId()) {
$collection->addUrlRewrite(0);
} else {
$collection->addUrlRewrite($this->getCurrentCategory()->getId());
}
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
return $this;
}

Pager toolbar in custom module in magento

I use this tutorial and display products on the basis of multiple categories but now I am getting in issue is that the pager tool bar not working on that.
My block code is :
<reference name="content">
<block name="mymodule" type="mymodule/product_listcategories" template="catalog/product/list.phtml">
<action method="setCategories">
<ids>2,3,4</ids>
</action>
</block>
</reference>
I also add this code with above
<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
<block type="page/html_pager" name="product_list_toolbar_pager"/>
</block>
<action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
</block>
It display toolbar but the toolbar is not working (limit,orderby).
My block code is
class Mymodule_Block_Product_Listcategories extends Mage_Catalog_Block_Product_List
{
protected function _getProductCollection()
{
$this->_productCollection = Mage::getModel('catalog/product')->getCollection();
$this->_productCollection->addAttributeToSelect('*');
if($this->getCategories()!="")
$this->_productCollection->addCategoriesFilter($this->getCategories());
return $this->_productCollection;
}
}
}
Does anyone know where is the problem? I think I'm missing some code for the pager? Thanks in advance
After search so much i got a solution.I didn't know whether it right or wrong way but it solve my problem.On my block i create intance of
$cpBlock = $this->getLayout()->getBlockSingleton('Mage_Catalog_Block_Product_List_Toolbar');
and access the pager core function like $this->_itemPerPage = $cpBlock->getLimit(); .In the above code itemperpage is the total no of items to be displayed on listing page.This code work if you create custom module and extend you block from list block.Thanks

Magento - move block from right to left

Not sure why this is so difficult. If I understand THIS correctly, I should be able to swiftly accomplish my goals... But no joy.
So - I'm building my first theme, and still getting my head around layout...
I'm working specifically on the Catalog Product View page, and am converting this page from a right column layout to a left column layout. I simply want to move the blocks from the right into the left.
In the default catalog.xml, product_list_related is defined:
</catalog_product_view>
//...
<reference name="right">
<block type="catalog/product_list_related" name="catalog.product.related" before="-" template="catalog/product/list/related.phtml"/>
</reference>
</catalog_product_view>
In my local.xml, I'm simply trying to move this block:
<layout>
// bunch other page directives
<catalog_product_view>
<reference name="root">
<action method="setTemplate"><template>page/2columns-left.phtml</template></action>
</reference>
<reference name="right">
<action method="unsetChild"><name>catalog.product.related</name></action>
</reference>
<reference name="left">
<action method="insert"><blockName>catalog.product.related</blockName></action>
// note that that "catalog.leftnav" gets inserted as expected
<block type="catalog/layer_view" name="catalog.leftnav" after="-" template="catalog/layer/view.phtml"/>
</reference>
</catalog_product_view>
</layout>
As noted - inserting catalog.leftnav works as expected, so I'm assuming everything else is set up correctly. The target block renders as expected if I leave the template and other directives unchanged, which tells me that the block should render once its been properly unset and inserted...
This is driving me nuts... but what else is new with Magento.
Cheers -
b[]x
UPDATE
Because I simply can not get a local.xml override to work, I'm just falling back on a modified catalog.xml. I'm a reasonably smart guy... it worries me that I can't get this to work (and that magento just silently fails, whatever the case ) - but I can't waste anymore time dicking around with this stupid issue.
Moving on.
UPDATE, again.
Spent some time, now, working in magento and getting more familiar with its complexities. I came back to this issue today as I need to get my local.xml working right.
I really don't know what I had wrong, but this set of directives finally worked.
<reference name="right">
<action method="unsetChild">
<alias>catalog.product.related</alias>
</action>
</reference>
<reference name="left">
<action method="insert">
<block>catalog.product.related</block>
</action>
</reference>
A key point I'll make note of for others dealing with this:
Layout xml directives call available methods within magento classes. In this case, Page.xmls "Left" block is of type Mage_Core_Block_Text, which inherits from Mage_Core_Block_Abstract which contains the methods unsetChild and insert.
from Mage_Core_Block_Abstract :
/**
* Unset child block
*
* #param string $alias
* #return Mage_Core_Block_Abstract
*/
public function unsetChild($alias)
{
if (isset($this->_children[$alias])) {
unset($this->_children[$alias]);
}
if (!empty($this->_sortedChildren)) {
$key = array_search($alias, $this->_sortedChildren);
if ($key !== false) {
unset($this->_sortedChildren[$key]);
}
}
return $this;
}
and
/**
* Insert child block
*
* #param Mage_Core_Block_Abstract|string $block
* #param string $siblingName
* #param boolean $after
* #param string $alias
* #return object $this
*/
public function insert($block, $siblingName = '', $after = false, $alias = '')
{
if (is_string($block)) {
$block = $this->getLayout()->getBlock($block);
}
if (!$block) {
/*
* if we don't have block - don't throw exception because
* block can simply removed using layout method remove
*/
//Mage::throwException(Mage::helper('core')->__('Invalid block name to set child %s: %s', $alias, $block));
return $this;
}
if ($block->getIsAnonymous()) {
$this->setChild('', $block);
$name = $block->getNameInLayout();
} elseif ('' != $alias) {
$this->setChild($alias, $block);
$name = $block->getNameInLayout();
} else {
$name = $block->getNameInLayout();
$this->setChild($name, $block);
}
if ($siblingName === '') {
if ($after) {
array_push($this->_sortedChildren, $name);
} else {
array_unshift($this->_sortedChildren, $name);
}
} else {
$key = array_search($siblingName, $this->_sortedChildren);
if (false !== $key) {
if ($after) {
$key++;
}
array_splice($this->_sortedChildren, $key, 0, $name);
} else {
if ($after) {
array_push($this->_sortedChildren, $name);
} else {
array_unshift($this->_sortedChildren, $name);
}
}
$this->_sortInstructions[$name] = array($siblingName, (bool)$after, false !== $key);
}
return $this;
}
Local xml parameters are important, then, not in name (specifically), but in order ie:
<reference name="left">
<action method="insert">
<block>catalog.product.related</block>
<siblingName>catalog.leftnav</siblingName>
<after>1</after>
<alias>catalog_product_related</alias>
</action>
</reference>
Ultimately, this makes local.xml a really powerful method of manipulating the system, but if you are unfamiliar with it and the magento system, get ready for weeks or months of work to really get your head around it.
Cheers
Yet another update
I've run into the problem several times now, where a block I want to move has been removed. This is a problem, as any block that has been removed from layout is nuked forever.
However, with Alan Storm's very handy Unremove Plugin you can undo whats been done:
<checkout_onepage_index>
<x-unremove name="left" />
<reference name="right">
<action method="unsetChild">
<alias>checkout.progress.wrapper</alias>
</action>
</reference>
<reference name="left">
<action method="insert">
<block>checkout.progress.wrapper</block>
</action>
</reference>
</checkout_onepage_index>
It manages this feat by watching the layout object, and building a list of removed blocks, which can be later referenced.
Nice!
This is a dusty thread now, but for the record, this is the final answer I went with.
<reference name="right">
<action method="unsetChild">
<alias>checkout.progress.wrapper</alias>
</action>
</reference>
<reference name="left">
<action method="insert">
<block>checkout.progress.wrapper</block>
</action>
</reference>
Magento recommends including this in your local.xml, and it has proved an effective technique.
An alternative approach is to rename the "right" block to "left" rather than move blocks from right to left. However, this will not work if the "right" block is being removed in the first place as I suggested might be the case in my other answer.
<remove name="left" />
<reference name="right">
<action method="setNameInLayout"><name>left</name></action>
</reference>
<reference name="root">
<action method="setChild"><alias>left</alias><name>left</name></action>
<action method="unsetChild"><alias>right</alias></action>
</reference>
<!-- make sure nothing referencing "right" comes after this! -->
If you are just looking to change the output from 2col-right, to 2col-left, it would have been much, much easier to just change
<reference name="right" ...
to
<reference name="left" ...
There is no need to unset or insert any children or re-declare anything. You are overcomplicating things and duplicating code needlessly.
If you are making your own very besoke design too, I would suggest starting copying the entire ./app/design/frontend/base directory to ./app/design/frontend/mypackage - then work from the mypackage/default directory to re-skin your site. Its cleaner and easier. Others may comment on the potential issues of upgrade-ability if you copy every file (rather than just the ones you intend to modify), but it is by far a better practice and much less prone to errors, difficulties and general maintenance.
Edit: To follow up on this in more detail - have a look https://magento.stackexchange.com/a/3794/361
The "right" block is probably being removed with a <remove name="right" /> at some point in the layout. If so, this will cause the "catalog.product.related" block to never be added to the layout in the first place so there is no block to insert into the "left" block.
I'm pretty sure this is the issue, but to confirm, add a Mage::log("Removed $blockName"); in Mage_Core_Model_Layout->generateXml() inside the for loop and check the log after loading the page.
If I'm right, simply copy the <block..> into your local.xml and remove the "unsetChild" and "insert" actions.
I would do it this way: In your local.xml under e.g.
<reference name="right"></reference>
you remove the blocks, e.g.:
<remove name="right.poll">
and then you add the blocks in
<reference name="left"></reference>
Copying base/default to a local theme is a horrible idea. Every update to a core theme file on a Magento version upgrade leads to the same vulnerability of modifying app/code/core files -- needing you to diff the files out on upgrades.
Your proper course of action is modifying a single local.xml with your additions or overrides. If that doesn't suffice, use the proper syntax to create a module in app/code/community with your layout definitions and custom layout XML files.
I think the best answer to modify Magento layouts is given on Classyllama.com
http://www.classyllama.com/development/magento-development/the-better-way-to-modify-magento-layout
When you use the remove tag, it removes any blocks with the specified name from the entire layout, regardless of the context. So, if I remove right.newsletter in the context and that name is used in say the context, then both blocks will be removed. Because remove operates on the global context, you can only remove an element once. Since is being called in catalogsearch.xml, we have to unset it, or else we'll get an error.
<action method="unsetChild"><name>right.newsletter</name></action>;

Unable to prevent Magento from Caching a Block

I'm working on a Magento 1.6 site, which has the following xml inside the home page's CMS "Layout Update XML" field:
<reference name="content">
<block type="catalog/navigation" name="catalog.category.home" as="homecategory" template="catalog/category/homecategory.phtml" />
</reference>
As the template shows randomized categories, I would like to disable caching for this block.
To do so, I attempted using getChildHtml('sub-block-template', false) with the following:
(homecategory has $this->getChildHtml('random_categories', false) in its template)
<reference name="content">
<block type="catalog/navigation" name="catalog.category.home" as="homecategory" useCache="false" template="catalog/category/homecategory.phtml">
<block type="catalog/navigation" name="catalog.category.home.randcats" as="random_categories" useCache="false" template="catalog/category/random.phtml" />
</block>
</reference>
So now I'm stuck, wondering why I can't prevent caching of that block, despite using the 'false' argument.
I had this same problem. I beleive it has to do something with the block type of type="catalog/navigation". I've seen this disabling of caching work on other types of blocks. Here is a fix for this block type and this problem:
phtml file change: make sure the second param is false
echo $this->getChildHtml('topCategoriesList',false);
xml file change:
Add these actions to the block
<block type="catalog/navigation" name="topCategoriesList" as="topCategoriesList" template="catalog/navigation/categorylist.phtml">
<action method="unsetData"><key>cache_lifetime</key></action>
<action method="unsetData"><key>cache_tags</key></action>
</block>
Have you tried forcing it by creating a new custom block type and overloading the caching functions? Extend the Mage_Catalog_Block_Product_List_Random class and create an empty pseudo-constructor:
protected function _construct() {}
This will prevent inheriting adding cache tags, lifetime, and other metadata to the block object. Then you can overload the cache key info as well so that it doesn't use any existing (or enabled) cache blocks. For example:
public function getCacheKeyInfo()
{
return array(
'MY_CACHE_TAG',
Mage::app()->getStore()->getId(),
(int)Mage::app()->getStore()->isCurrentlySecure(),
Mage::getDesign()->getPackageName(),
Mage::getDesign()->getTheme('template')
);
}

Add a link to Magento's My Account Page Conditionally

I would like to create a link on the My Account page that only get displays under certain conditions.
Right now I have the link display all the time by adding the following entry to my layout XML file:
<customer_account>
<reference name="customer_account_navigation">
<action method="addLink" translate="label" module="nie"><name>nie</name><path>nie</path><label>NIE Admin</label></action>
</reference>
</customer_account>
I am assuming there is a way to code this so that it only displays under certain circumstances.
The cart & checkout links already do something similar so their method can be copied.
Create a block. It won't be displaying directly so can be descended from the boring Mage_Core_Block_Abstract.
Give it a method where the conditional logic will go.
public function addNieLink()
{
if (($parentBlock = $this->getParentBlock()) && (CONDITION-GOES-HERE)) {
$parentBlock->addLink($this->_('NIE Admin'), 'nie', $this->_('NIE Admin'), true, array(), 50, null, 'class="top-link-cart"');
// see Mage_Page_Block_Template_Links::addLink()
}
}
protected function _prepareLayout()
{
// Add the special link automatically
$this->addNieLink();
return parent::_prepareLayout();
}
Put your check in place of CONDITION-GOES-HERE.
Add your block to the links block.
<customer_account>
<reference name="customer_account_navigation">
<block type="yourmodule/link" name="yourmodule.link" />
</reference>
</customer_account>
(Correct the block type here to your newly created link block)
The important bit is it calls getParentBlock() to find out where the link is to go.

Resources