Magento as a commerce platform, allow us create and maintain various kind of products with its attributes. Magento DB follows a EAV structure to maintain the products with its appropriate attributes.

Sometime we need to update the attributes of the existing products for which we have defined functions in Magento .
For Example: Default Magento way to update the website:
$website_ids = array(1, 2, 3);

$products = Mage::getModel('catalog/product')

foreach( $products as $_product) {
	$_product = Mage::getModel('catalog/product')->load($_product->getEntityId());
    	try {

   	 } catch(Exception $e) {
        echo $e->getMessage();


In the above example Magento would fetch each product object and update its the website. This process is correct but its time taking. So we can use this method for updating it few products, but when you have large inventory it would be a problem.

For large inventory, it would be better to update directly in DB. For DB update the we need know the table responsible to store all the website details “mage_catalog_product_website”.

This table contains 2 columns: product_id and the website id.

Now we can write a simple Magento query or direct MySQL query to update the website details.

Following is a exmaple which would directly add/update the website option for all products.

You can fetch products ids from the table ‘Mage_Catalog_Product_Entity’. Or you can use default Magento function to fetch the entity.

$website_ids = array(1,2,3,5,6);

$read = Mage::getSingleton('core/resource')->getConnection('core_read'); //initialize a read method
$write = Mage::getSingleton('core/resource')->getConnection('core_write'); //initialize a write method

//execute a read method

$resultquery= $read->query(" SELECT * FROM `mage_catalog_product_entity` ");

 $row = $resultquery->fetchAll(); //fetch all the records

foreach($website_ids as $web_id){

     $insertProductQuery = "INSERT INTO `mage_catalog_product_website`
     ( `product_id` , `website_id` ) VALUES ( '$productId', '$web_id');";
      $write->query($insertProductQuery);// Execute write the query


I find this method more useful and faster for updating 1000+ records in less time. Hope this would be help to you also.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s