1

I currently created an extension which does a series of works to the category and product url.

Then I need to tell Magento not to include some of the categories and products if they have certain attributes.

But I'm a bit new so I need to know where do I setup my function. Is it in Observer.php or do I need to create another observer?

How to hook the event to my function. I think that would be in the config.xml like below:

<frontend>
    <events>
        <core_block_abstract_prepare_layout_before>
            <observers>
                <updater>
                    <type>model</type>
                    <class>My_Updater_Model_Observer</class>
                    <method>beforeLoadLayout</method>
                </updater>
            </observers>
        </core_block_abstract_prepare_layout_before>
    </events>
</frontend>

Thanks.

2
  • I think this is answered here: magento.stackexchange.com/a/151684/46249
    – sv3n
    Commented May 31, 2017 at 7:24
  • Thanks @sv3n, that gives some ideas but how to I trigger the function? In wording of which event should I use? Are there anything like <core_sitemap_generating_before> or maybe something else?
    – noob
    Commented May 31, 2017 at 23:14

2 Answers 2

2

Hope this helps a bit .... in this expample we exclude all products with color attribute red:

app\etc\modules\My_Module.xml

<?xml version="1.0"?>
<config>
    <modules>
        <My_Module>
            <active>true</active>
            <codePool>community</codePool>
        </My_Module>
    </modules>
</config>

app\code\community\My\Module\etc\config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <My_Module>
            <version>0.0.1</version>
        </My_Module>
    </modules>

    <global>
        <models>
            <My_Module>
                <class>My_Module_Model</class>
            </My_Module>
        </models>
        <events>
            <!-- product sitemap -->
            <sitemap_products_generating_before>
                <observers>
                    <sitemap_products>
                        <class>My_Module/observer</class>
                        <method>excludeProductsFromSitemap</method>
                    </sitemap_products>
                </observers>
            </sitemap_products_generating_before>
            <!-- category sitemap -->
            <sitemap_categories_generating_before>
                <observers>
                    <sitemap_products>
                        <class>My_Module/observer</class>
                        <method>excludeCategoriesFromSitemap</method>
                    </sitemap_products>
                </observers>
            </sitemap_categories_generating_before>
        </events>
    </global>
</config>

app\code\community\My\Module\Model\Observer.php

<?php

class My_Module_Model_Observer extends Mage_Core_Model_Observer
{
    public function excludeProductsFromSitemap(Varien_Event_Observer $observer)
    {
        $event = $observer->getEvent();
        $collection = $event->getCollection();
        $items = $collection->getItems();

        $excludeIds = Mage::getModel('catalog/product')
            ->getCollection()
            ->setStoreId($event->getStoreId())
            // example filter
            ->addAttributeToFilter('color', 'red')
            ->getAllIds();

        foreach ($excludeIds as $id) {
            unset($items[$id]);
        }

        $collection->setItems($items);
    }
    public function excludeCategoriesFromSitemap(Varien_Event_Observer $observer)
    {
        $event = $observer->getEvent();
        $collection = $event->getCollection();
        $items = $collection->getItems();

        // your logic to filter categories here

        $collection->setItems($items);
    }
}
2
  • Greate thanks, @sv3n. This is exactly what I'm looking for. Will try it tomorrow.
    – noob
    Commented Jun 1, 2017 at 13:23
  • Thanks for the answer but my rep is not 15 yet. But I will keep that in mind one day my rep is over 15 I will come back and upvote you.
    – noob
    Commented Jun 3, 2017 at 1:32
0

You can create new function in same observer and mention that function name in config.xml file as you did.

So you can write your new function in Model/Observer.php as below

...
public function beforeLoadLayout($observer)
{
// your function body here
}
...
1
  • Thanks @kishan-patadia, actually I have the beforeLoadLayout function and it works fine. The question is about how to overwrite Magento's core function of generating sitemap.xml.
    – noob
    Commented Jun 1, 2017 at 2:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.