I am doing a custom function for magento Admin. The function is when author finished setup a product and save, the product will automatically assigned to certain websiteId when the conditions is met.

For that purposes I use catalog_product_save_after event. In that event, i cannot call ->save() otherwise its gonna be infinite loop.

I tried

$product->getResource()->saveAttribute($product, 'website_ids');

But it seems that saveAttribute doesnt work on array attribute.

How should i assign desired websiteId to the product? Thank you


I reckon you should use the Mage_Catalog_Model_Product_Website class.

This class is used when you update the websites for product action in the backend.

You can use it by doing the following to add products to websites:

Mage::getModel('catalog/product_website')->addProducts($websiteIds, $productIds);

To remove products from websites you can do:

Mage::getModel('catalog/product_website')->removeProducts($websiteIds, $productIds);

You won't face any issue with the save method as those methods are using a resource model to interact directly with the database, for example the addProducts method looks like this:

public function addProducts($websiteIds, $productIds)
    if (!is_array($websiteIds) || !is_array($productIds)
        || count($websiteIds) == 0 || count($productIds) == 0)
        return $this;


    // Before adding of products we should remove it old rows with same ids
    $this->removeProducts($websiteIds, $productIds);
    try {
        foreach ($websiteIds as $websiteId) {
            foreach ($productIds as $productId) {
                if (!$productId) {
                $this->_getWriteAdapter()->insert($this->getMainTable(), array(
                    'product_id' => (int) $productId,
                    'website_id' => (int) $websiteId

            // Refresh product enabled index
            $storeIds = Mage::app()->getWebsite($websiteId)->getStoreIds();
            foreach ($storeIds as $storeId) {
                $store = Mage::app()->getStore($storeId);
                $this->_getProductResource()->refreshEnabledIndex($store, $productIds);

    } catch (Exception $e) {
        throw $e;
    return $this;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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