How to Bulk Update Products to Include New Websites in Magento

multistoreproductproducts-management

A client has 20000+ products with 7 websites. They used to have 4 websites and most products are associated to 4 websites. What is the best & quickest way to iterate through the products and update the products to include the new websites.

I have the following code however it is far too slow:

$productCollection = Mage::getModel('catalog/product')->getCollection()
    ->addFieldToFilter('sku', array('like' => '02%'));
foreach ($productCollection as $product) {
    echo $product->getSku();
    $product->setWebsiteIds(array(1,2,3,4,5,6,7));
    try {
        $product->save();
        echo " - saved.";
    } catch (Exception $e) {
        echo ' - '.$e->getMessage();
    }
    echo "\n";
}

I were thinking of using the iterator walk method however I understand that the store/website isn't exactly an attribute so it can't be easily updated on its own.

Best Answer

Step 1
Build an array with your new website ids.

$websiteIds = array(5,6,7);

Step 2
Now get all the product ids.

$productIds= Mage::getResourceModel('catalog/product_collection')->getAllIds();

Step 3
Assign all the products to the new websites:

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

Step 4
Feel good about yourself.

Related Topic