0

I have setup a product attribute for showing the product in a space on the left side of the store. I only want to display one product in that space and therefore would like to only have one product that had the attribute set to 'true'.

For example, if one product already had the leftbanner attribute set to true saving another product with the leftbanner attribute as true would change the first products attribute to 'false'.

I've tried setting the attribute to have the 'unique' flag, but as expected I get an error when saving another product saying the value must be unique.

1 Answer 1

2

You can achieve this using an observer on the product before save or after save. I recommend doing it on the before save event.
Create a module, or you can use one that you already have. Let's call it Easylife_Leftbanner. You will need these files: app/etc/modules/Easylife_Leftbanner.xml - the declaration file

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Leftbanner>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Catalog />
            </depends>
        </Easylife_Leftbanner>
    </modules>
</config>

app/code/local/Easylife/Leftbanner/etc/config.xml - the configuration file

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Leftbanner>
            <version>0.0.1</version>
        </Easylife_Leftbanner>
    </modules>
    <global>
        <models>
            <leftbanner><!-- declare the models alias -->
                <class>Easylife_Leftbanner_Model</class>
            </leftbanner>
        </models>
        <events>
            <catalog_product_save_before><!-- observe the product before save event -->
                <observers>
                    <leftbanner>
                        <class>leftbanner/observer</class>
                        <method>changeLeftbanner</method>
                    </leftbanner>
                </observers>
            </catalog_product_save_before>
        </events>
    </global>
</config>

app/code/local/Easylfe/Leftbanner/Model/Observer.php - the observer

<?php
class Easylife_Leftbanner_Model_Observer{
    public function changeLeftbanner($observer){
        $attribute = 'leftbanner';
        $product = $observer->getEvent()->getProduct();
        //if the product attribute value has changed
        if ($product->getData($attribute) != $product->getOrigData($attribute)){
            //if the value is set to 1
            if ($product->getData($attribute) == 1){
                //get all the products that already have the value 1 for the attribute.
                //normally there should be only 1, but it's safer this way
                $collection = Mage::getModel('catalog/product')->setStoreId($product->getStoreId())->getCollection()
                    ->addAttributeToFilter($attribute, 1);
                $ids = $collection->getAllIds();
                //if there are such products
                if (count($ids)){
                    //change the value of the attribute for them
                    Mage::getModel('catalog/product_action')->updateAttributes($ids, array($attribute=>'0'), $product->getStoreId());
                }
            }
        }
        return $this;
    }
}

Clear the cache and give it a go.

0

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.