16

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.

4 Answers 4

36

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.

10
  • 1
    Thank you Marius. Perfect answer. (I would vote up but I can't yet)
    – Raj
    Commented Mar 6, 2014 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
    Commented Jul 16, 2015 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. Commented Dec 18, 2015 at 6:35
  • In typical Marius fashion, worked like a charm!
    – sparecycle
    Commented Mar 14, 2016 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
    Commented Jul 5, 2016 at 23:05
3

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
$productCollection->getSelect()
    ->having('COUNT(website_id) = ?', count($oldWebsiteIds))
    ->distinct(false)
    ->group('e.entity_id');
$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)

2

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.

3
  • +1 from me. But its not generate the product URL's. Magento Version 2.3.5 so reindex will not work. So have you an idea about URL. Commented Oct 30, 2020 at 10:56
  • @H_Parekh why not making a big manual reindex in command line?
    – Shadoweb
    Commented Oct 30, 2020 at 16:46
  • ,Thanks for the reply. I have reindex but in Magento 2 there is no reindex for URL's. Commented Nov 2, 2020 at 11:54
1

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');
1
  • is this faster? Or how is it different from Marius solution?
    – snh_nl
    Commented Jul 31, 2020 at 11:14

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.