Quick way to associate existing products with newly created Magento websites/stores.

Submitted by deeperdata - 1 year ago

This is the fastest way (IMHO) way to iterate through all products and associate them with newly created websites. The workbench script is ready to go.

<?php
    /**
     * Working with Magento outside of the normal routing dispatch
     * flow of Mage::run()? Mage::app(); is the way to go. This
     * workbench script can be a quick way to check on various
     * parts of the app, including configuration, which can be
     * handy when verifying custom modules.
     */
    //set some PHP params for ease of debugging
    ini_set('display_errors',true);
    error_reporting(E_ALL | E_STRICT);
    //process bootstrap & application hub class
    require 'app/Mage.php';
    //set developer mode
    Mage::setIsDeveloperMode(true);
    //the standard filemask for server-written files
    umask(0);
    /**
     * Initialize the app and config with the default store scope
     * (based on configuration data). Note that not all parts of
     * the configuration are loaded, meaning that frontend- and
     * adminhtml-configured event observers are not added to the
     * observer lists. This can be remedied by calling
     * Mage::app()->loadArea() / ->loadAreaPart() with the
     * appropriate params. Note also that sessions must be
     * invoked manually as well.
     */
    Mage::app();
    /**
     * Now it's possible to play around / test things. For example:
     *
     *     $product = Mage::getModel('catalog/product');
     *
     * will load an instance of Mage_Catalog_Model_Product, just as
     * would take place in a normally-dispatched application flow.
     *
     * Have fun!
     * Ben Marks, Blue Acorn
     * http://benmarks.github.com/
     * @benmarks
     */

     // STEP 1: BUILD AN ARRAY WITH THE NEW WEBSITE IDS
     $websiteIds = array(5,6,7);

     // STEP 2: GET ALL PRODUCT IDS
     $productIds= Mage::getResourceModel('catalog/product_collection')->getAllIds();

     // STEP 3: ASSIGN ALL PRODUCTS TO THE NEW WEBSITES
     Mage::getModel('catalog/product_website')->addProducts($websiteIds, $productIds);

     // AND THAT'S IT!!!
affiliate_link
Learn PHP
comments powered by Disqus
Proudly hosted on Digital Ocean