1

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?

2 Answers 2

1

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.

1

Actually you are still saving the object and therefore you do not have an ID yet.

You should get the object (alias product) been saved from the eventData parameter passed to observer function

$event = $observer->getEvent();
$product = $event->getProduct(); //or take a look var_dump the event data.

This will allow you to change the value for visibility before saveing the product.

But from the question is no clear to me if you want to save in the same function/event the product to be Catalog, Search on store1 AND Not visibile on store2

This cannot be done before the save but only after the object is created.

regards

2
  • I don't understand why this cant be done in the observer function. It's just setting an attribute value, right? I found the answer and have posted below. Thanks for the help :)
    – albin
    Commented Jun 20, 2014 at 22:08
  • I was thinking that setStoreId cannot be called at this stage of saving product, but obviously I'm wrong.
    – Giuseppe
    Commented Jun 21, 2014 at 13:06

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.