0

I am running Magento version 1.9.3.4 with PHP 5.6.40 for my store and it has around 5000 products. So I need to increase the product price by Rs.1000 for all products. I just added below custom code in updateprice.php file in my Root and executed it by mysite.com/updateprice.php but unfortunately its not working.

Here is my code :

<?php

require_once('../app/Mage.php');
Mage::app('default');

$priceToAdd = 1000;

$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$write->query("
UPDATE catalog_product_entity_decimal val
SET  val.value = (val.value + $priceToAdd)
WHERE  val.attribute_id = (
SELECT attribute_id FROM eav_attribute eav
WHERE eav.entity_type_id = 4
AND eav.attribute_code = 'price'
)
");

Can anyone know how to fix this?

Note : Then Price change should applicable for both Price & Special price. For example If the products have both price & special price then the price 1000 should increased to both. If the products only have the price then it should updated that price alone.

  • 1
    use data profile to for this – Shoaib Munir Jan 24 at 10:37
3

Try below code:

<?php
ini_set("display_errors", 1);
require_once ( "app/Mage.php" );
Mage::app();
umask(0);
$priceToAdd = 1000;
$_products = Mage::getModel('catalog/product')->getCollection();
if ($_products->count()) {
    foreach ($_products as $_product) {
        $product = Mage::getModel('catalog/product')->load($_product->getId());
        $updater = Mage::getSingleton('catalog/product_action');
        $updater->updateAttributes(array($product->getId()), array('price' => ($product->getPrice() + $priceToAdd)), 0);

        if ($product->getSpecialPrice() != '') {
            $updater->updateAttributes(array($product->getId()), array('special_price' => ($product->getSpecialPrice() + $priceToAdd)), 0);
        }
    }
}

Update: To update prices using percentage amount.

Change:

$updater->updateAttributes(array($product->getId()), array('price' => ($product->getPrice() + $priceToAdd)), 0);

To:

$updater->updateAttributes(array($product->getId()), array('price' => ($product->getPrice() + (($product->getPrice() * $percentage)/100))), 0);

and

Change:

$updater->updateAttributes(array($product->getId()), array('special_price' => ($product->getSpecialPrice() + $priceToAdd)), 0);

To:

$updater->updateAttributes(array($product->getId()), array('special_price' => ($product->getSpecialPrice() + (($product->getSpecialPrice() * $percentage)/100))), 0);
  • thanks I will try this. Can you please let me know how to increase & decrease products by Percentage & Specific amount with above code? – Mesk Jan 24 at 10:52
  • For a specific amount, I have already answered. For the percentage change, please check my update. – Mohit Kumar Arora Jan 24 at 11:16
  • Thanks. You have mentioned Specific Amount & Percentage Increase. Also please let me know the same for Decrease – Mesk Jan 24 at 11:22
  • For Decrease, you can use minus sign (-) in place of the plus sign (+). – Mohit Kumar Arora Jan 24 at 11:40
  • For Percentage Decrease? – Mesk Jan 24 at 12:02

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.