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();
    try {
        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.


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.

| improve this answer | |
  • 1
    Thank you Marius. Perfect answer. (I would vote up but I can't yet) – Raj Mar 6 '14 at 12:00
  • 3
    step 5 create a gist: gist.github.com/mauricioprado00/2b730532689d28dcdb2b. you can do "php -f shell/product-website.php -- --products all --websites all" or specify websites/products ids. – useless Jul 16 '15 at 1:21
  • wow... that is an amazingly perfect efficient solution. Even I was going to loop around all those many products to update them. I want to know how to learn such concepts about Magento. – Deepanshu Goyal Dec 18 '15 at 6:35
  • In typical Marius fashion, worked like a charm! – sparecycle Mar 14 '16 at 15:27
  • 1
    The products do not seem to appear in the flat catalog and are not visible on the frontend. When using the - save() method the products do appear. Does anyone know why Marius' method gives this problem? M1.9, 4 websites, 15k products, flat catalog – Tom Jul 5 '16 at 23:05

If you only want to add the products that are assigned to all 4 previously existing websites, use this:

$oldWebsiteIds = [2, 3, 4, 5];
$newWebsiteIds = [6, 7, 8]

/** @var Mage_Catalog_Model_Resource_Product_Collection $productCollection */
$productCollection = Mage::getResourceModel('catalog/product_collection');
$productCollection->addWebsiteFilter($oldWebsiteIds );
// only filter products present in ALL of the websites
    ->having('COUNT(website_id) = ?', count($oldWebsiteIds))
$productIds = $productCollection->getAllIds();

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

See also: Filter products by website using AND

This can be a Magento setup script or a throwaway PHP script (in this case add include 'app/Mage.php'; at the top and remove it from the server after usage)

| improve this answer | |

Step 1: Get all website Ids/make array of website Ids

$websiteIds = Mage::getResourceModel('core/website_collection')->getAllIds();

Step 2: Get all product Ids

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

Step 3: Add website ids to all product

$actionModel = Mage::getSingleton('catalog/product_action');
$actionModel->updateWebsites($productIds, $websiteIds, 'add');

Note: If you want to remove the website from product. use remove instead add word

$actionModel->updateWebsites($productIds, $websiteIds, 'remove');
| improve this answer | |
  • is this faster? Or how is it different from Marius solution? – snh_nl Jul 31 at 11:14

Another solution if your laziness goes to the point of not wanting to create any PHP files:

INSERT IGNORE INTO catalog_product_website
SELECT entity_id, {website_id} FROM catalog_product_entity

Worked for me.

| improve this answer | |

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.