DRAJI
DRAJI

Reputation: 1869

To get product collections by some range of ids in magento

I want to update all product price by specific percentage from current price.I got the following line of code update price by percentage wise

$products = Mage::getModel('catalog/product')->getCollection()
 ->addAttributeToSelect('price');
 foreach ($products as $product) {

 $product->setPrice($product->getPrice() * 1.03);
 $product->save();
 echo $product->getId()."updated Sucess";
 }

Its working well. But it will take so much time to complete the above process because we have a lot of products in our store. Some times it will return time out error too. So i want to perform this above operation by split those product collections like if we have 1000 products, do this operation by like below

Collect 1st 100 products from product collections
Update price of those products
Then collect next 100 products
Update price of those products

Anybody have an idea to get product collections by some range of ids?

Upvotes: 5

Views: 12522

Answers (2)

Amit Bera
Amit Bera

Reputation: 7611

$_productCOllection = Mage::getModel('catalog/product')->getCollection()
   ->addAttributeToSelect('*')
   ->addAttributeToFilter('entity_id', array(
    'from' => $startPrOID,
    'to' => $EndPrOID
    ))
    ->load();

Upvotes: 9

Loïc
Loïc

Reputation: 11943

Sometimes using magento (or any other framework, it's better to directly query the database instead of using the system in place.

Using this :

SELECT * FROM `eav_attribute` WHERE `attribute_code` = ‘price’

You'll get your price attribute id.

Then using this :

SELECT * FROM `catalog_product_entity_decimal` 
    WHERE `attribute_id` = [above id] and `store_id` = [id of your store]

You'll get all your prices for a given store (which you can update using a simple MySQL UPDATE query.

Upvotes: 0

Related Questions