1

At the moment I have a category of say 20 products and I have popularity on them all (they are all in a set order) If I then add a new product to this category, it sets the popularity of this new product to 0. I want to default the popularity of new products to 100.

How can I do this?

2
  • 1
    By popularity you mean position?
    – Marius
    Commented Jul 31, 2014 at 10:40
  • Yes I do mean position
    – Adi
    Commented Jul 31, 2014 at 13:08

1 Answer 1

1

The perfect solution for this is to develop an Event-Observer module using the controller_action_postdispatch_adminhtml_catalog_product_save event which is fired on product save. Your Observer.php will then update the product's popularity, like this:

app/code/local/Namespace/Module/etc/config.xml:

...
<adminhtml>
    <events>
        <controller_action_postdispatch_adminhtml_catalog_product_save>
            <observers>
                <namespace_module>
                    <type>singleton</type>
                    <class>Namespace_Module_Model_Observer</class>
                    <method>updatePopularity</method>
                </namespace_module>
            </observers>
        </controller_action_postdispatch_adminhtml_catalog_product_save>
    </events>
</adminhtml>

And then in app/code/local/Namespace/Module/Model/Observer.php:

public function updatePopularity($observer){
    $product = $observer->getEvent()->getProduct();
    $product->setPopularity(100);
    $product->save();
}

Then you should be all set.

12
  • Brilliant! I'll give that a go
    – Adi
    Commented Jul 31, 2014 at 12:24
  • Otherwise, you could go the non-programmatic route and just set the attribute popularity to have a default value of 100. You can do this via CatalogAttributesManage Attributes
    – Moose
    Commented Jul 31, 2014 at 12:28
  • I can't access $product
    – Adi
    Commented Jul 31, 2014 at 13:10
  • If I print_r $product its null. So can't setPopularity() Any ideas?
    – Adi
    Commented Jul 31, 2014 at 13:11
  • Code is working 100% for me. If you say that Magento is reading your Observer.php code, that means that the event listener is set up correctly. Re check the code above & make sure there are no typos on your side.
    – Moose
    Commented Jul 31, 2014 at 13:22

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.