简体   繁体   中英

Magento - CatalogSearch Module overriding not working

I want to override the "prepareProductCollection" function of layer in core CatalogSearch module. This is what I have written:

File Path: Company/Module/Model/CatalogSearch/Layer.php

class Company_Module_Model_CatalogSearch_Layer extends Mage_CatalogSearch_Model_Layer
{
public function prepareProductCollection($collection)
{
    parent::prepareProductCollection($collection);

    Mage::getModel('cataloginventory/stock_item')->addCatalogInventoryToProductCollection($collection);

    $collection->getSelect()->order('is_in_stock desc');
    return $this;
}
}

And in the config file:

<config>
    <modules>
        <Company_Module>
            <version>0.0.0.1</version>
        <Company_Module>
    </modules>
    <global>
        <models>
            <module>
               <class>Company_Module_Model</class>
            </module>
            <catalogsearch>
                <rewrite>
                   <layer>Company_Module_Model_CatalogSearch_Layer</layer>
                </rewrite>
            </catalogsearch>
        </models>
    </global>
</config>

I think I might be missing something? Can anyone help me with this?

您错过了在config.xml中结束catalogsearch标记

在顶部添加require_once(app / Mage / CatalogSearch / Model / Layer)

I've tried to do the exact same thing.
I followed multiple tutorials and tried several things. In the end I ended up having 503 errors all the time (without any maintanance.flag file - other shop sites worked well).
But I managed to fix that. I will show you what I have now and also what the reason for the 503 was, in case someone stumbles across that problem:

I've named the Module Company/CatalogSearch and it is made up of three files:

app/code/local/Company/CatalogSearch/Model/Layer.php :

<?php
class Company_CatalogSearch_Model_Layer extends Mage_CatalogSearch_Model_Layer {

    public function prepareProductCollection($collection) {

        parent::prepareProductCollection($collection);

        // YOUR CODE HERE

        return $this;
    }
}


app/code/local/Company/CatalogSearch/etc/config.xml :

<?xml version="1.0"?>
<config>
    <modules>
        <Company_CatalogSearch>
            <version>0.1</version>
        </Company_CatalogSearch>
    </modules>
    <global>
        <models>
            <catalogsearch>
                <rewrite>
                    <layer>Company_CatalogSearch_Model_Layer</layer>
                </rewrite>
            </catalogsearch>
        </models>
    </global>
</config>


app/etc/modules/Company_CatalogSearch.xml :

<?xml version="1.0"?>
<config>
    <modules>
        <Company_CatalogSearch>
            <active>true</active>
            <codePool>local</codePool>
        </Company_CatalogSearch>
    </modules>
</config>


And that's all.

To the 503 problem: In the file app/code/local/Company/CatalogSearch/etc/config.xml I had

<rewrite>
    <layer>
        Company_CatalogSearch_Model_Layer
    </layer>
</rewrite>

instead of

<rewrite>
    <layer>Company_CatalogSearch_Model_Layer</layer>
</rewrite>


Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM