Magento 1.8 – Set Product Visibility on Observer at Store Level Before Product Save

event-observermagento-1.8product-attribute

I have 2 websites, each of which has 2 stores.

When I create a product from a store(say, Store 1) of a website(say, Website 1), and if I set the websites for this product as Website 1 and Website 2, the product's visibility is set as catalog, Search only for the store from which I created it(Store 1). As for the other stores, the visibility is set to Not Visible Individually.

I have an observer for catalog_product_save_before, in which I want to set this visibility at store level.

Tried this code:

Mage::getModel('catalog/product_action')->updateAttributes(
                    array($productId),
                    array('visibility'=>4),
                    1
                );

and it works. But for this, I need to get the productId, which I wont get since the product is not yet saved in my observer function.

How do I set this product visibility for a particular store(say Store 3 of Website 2) to catalog, Search in the catalog_product_save_before observer?

Best Answer

Finally got it.

The observer function:

public function before_product_save($observer)
{
    $product = $observer->getProduct();
    $product->setStoreId(1)->setData('visibility', 4);
}

where, the store id is 1 and the visibility attribute value 4 refers to Catalog, Search.