0

I need to update(increase/decrease) the price & special price by certain amount or percentage for all products in all stores.

Here is the my code but its not increase/decrease the price. I have also refered few links but not match with the needs.

<?php

error_reporting(1);
ini_set('max_execution_time', 0);
use \Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$state = $objectManager->get('\Magento\Framework\App\State');
$state->setAreaCode('frontend');

$updateprice = "100";

$productFactory = $objectManager->get('\Magento\Catalog\Model\ProductFactory');
$product = $productFactory->create()->load();
$product->setPrice($updateprice);
$product->save();
?>

My version is Magento 2.2.4

Can anyone let me know how to increase/decrease the price for all products?

1 Answer 1

0

try my below code

<?php
ob_start();
use Magento\Framework\App\Bootstrap;
include('app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
ini_set('memory_limit', '1024M');

$updateprice = "100";
$storeId = '1'; //Store ID
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$products = $productCollection->addAttributeToSelect('*')
            ->load();
foreach ($products as $_product) {
    $price = $_product->getPrice();   
    $getSpecialPrice = $_product->getSpecialPrice(); 
    $_product->setPrice($price + $updateprice);
    $_product->setSpecialPrice($price + $updateprice);
    $_product->save();
}

Before

enter image description here

After

enter image description here

7
  • Thanks. I need to increase/decrease the product price by certain amount for all products & all stores. For example if already product price is 1000 then it should increase by 100 so the final amount after update is 1100
    – mysti
    Mar 29, 2019 at 5:44
  • ok let me check and i will update you Mar 29, 2019 at 5:46
  • Thanks,Will check it. It will applies for all products in all Stores? For percentage?
    – mysti
    Mar 29, 2019 at 6:38
  • I have tried above code, added as php file and executed the file but loading for very long and nothing happened.
    – mysti
    Apr 2, 2019 at 15:27
  • Also in backend price updated in wrong logic. For example the same amount is added to special price if the special price don't have value
    – mysti
    Apr 2, 2019 at 15:34

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.