1

I am using the below code to update the price but the price is not changing.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('\Magento\Catalog\Model\Product');
$product->load($productId);
$product->setPrice($productData['price']);
$product->save();

  • Try to get catalogFactory object and then do the same thing. – Ronak Chauhan Oct 14 '16 at 12:43
  • Make sure $productData['price'] has value? – Khoa TruongDinh Oct 14 '16 at 13:20
  • let me know what scenario u want to update the product price ? ex: after add to cart ...like that – Nagaraju Kasa Jul 7 '17 at 4:33
1

I've been trying to find a script that would let me update the prices via their skus. I would generate a csv file with "sku";"new price" and then run the script against it. I haven't seen anything like it.

  • No better methods than this. :) – PY Yick Jul 7 '17 at 3:37
1

If you want to use SQL approach, You can use the following standalone script which can be run either via Web or CLI

<?php
/**
 *
 * @author Raj KB<magepsycho@gmail.com>
 * @website http://www.magepsycho.com
 * @extension MassImporterPro: Pricing - http://www.magepsycho.com/mass-importer-pro-price-importer-regular-special-tier-group.html
 */
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Capture warning / notice as exception
set_error_handler('ctv_exceptions_error_handler');
function ctv_exceptions_error_handler($severity, $message, $filename, $lineno) {
    if (error_reporting() == 0) {
        return;
    }
    if (error_reporting() & $severity) {
        throw new ErrorException($message, 0, $severity, $filename, $lineno);
    }
}

require __DIR__ . '/../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);

$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('adminhtml');


/**************************************************************************************************/
// UTILITY FUNCTIONS - START
/**************************************************************************************************/
function _mpLog($data, $includeSep = false)
{
    $fileName = BP . '/var/log/m2-magepsycho-import-prices.log';
    if ($includeSep) {
        $separator = str_repeat('=', 70);
        file_put_contents($fileName, $separator . '<br />' . PHP_EOL,  FILE_APPEND | LOCK_EX);
    }
    file_put_contents($fileName, $data . '<br />' .PHP_EOL,  FILE_APPEND | LOCK_EX);
}

function mpLogAndPrint($message, $separator = false)
{
    _mpLog($message, $separator);
    if (is_array($message) || is_object($message)) {
        print_r($message);
    } else {
        echo $message . '<br />' . PHP_EOL;
    }

    if ($separator) {
        echo str_repeat('=', 70) . '<br />' . PHP_EOL;
    }
}

function getIndex($field)
{
    global $headers;
    $index = array_search($field, $headers);
    if ( !strlen($index)) {
        $index = -1;
    }
    return $index;
}

function readCsvRows($csvFile)
{
    $rows = [];
    $fileHandle = fopen($csvFile, 'r');
    while(($row = fgetcsv($fileHandle, 0, ',', '"', '"')) !== false) {
        $rows[] = $row;
    }
    fclose($fileHandle);
    return $rows;
}

function _getResourceConnection()
{
    global $obj;
    return $obj->get('Magento\Framework\App\ResourceConnection');
}

function _getReadConnection()
{
    return _getConnection('core_read');
}

function _getWriteConnection()
{
    return _getConnection('core_write');
}

function _getConnection($type = 'core_read')
{
    return _getResourceConnection()->getConnection($type);
}

function _getTableName($tableName)
{
    return _getResourceConnection()->getTableName($tableName);
}

function _getAttributeId($attributeCode)
{
    $connection = _getReadConnection();
    $sql = "SELECT attribute_id FROM " . _getTableName('eav_attribute') . " WHERE entity_type_id = ? AND attribute_code = ?";
    return $connection->fetchOne(
        $sql,
        [
            _getEntityTypeId('catalog_product'),
            $attributeCode
        ]
    );
}

function _getEntityTypeId($entityTypeCode)
{
    $connection = _getConnection('core_read');
    $sql        = "SELECT entity_type_id FROM " . _getTableName('eav_entity_type') . " WHERE entity_type_code = ?";
    return $connection->fetchOne(
        $sql,
        [
            $entityTypeCode
        ]
    );
}

function _getIdFromSku($sku)
{
    $connection = _getConnection('core_read');
    $sql        = "SELECT entity_id FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?";
    return $connection->fetchOne(
        $sql,
        [
            $sku
        ]
    );

}

function checkIfSkuExists($sku)
{
    $connection = _getConnection('core_read');
    $sql        = "SELECT COUNT(*) AS count_no FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?";
    return $connection->fetchOne($sql, [$sku]);
}

function updatePrices($sku, $price, $storeId = 0)
{
    $connection     = _getWriteConnection();
    $entityId       = _getIdFromSku($sku);
    $attributeId    = _getAttributeId('price');

    $sql = "INSERT INTO " . _getTableName('catalog_product_entity_decimal') . " (attribute_id, store_id, entity_id, value) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE value=VALUES(value)";
    $connection->query(
        $sql,
        [
            $attributeId,
            $storeId,
            $entityId,
            $price
        ]
    );
}

/**************************************************************************************************/
// UTILITY FUNCTIONS - END
/**************************************************************************************************/

try {
    $csvFile        = 'var/import/prices.csv'; #EDIT - The path to import CSV file (Relative to Magento2 Root)
    $csvData        = readCsvRows(BP . '/' . $csvFile);
    $headers        = array_shift($csvData);

    $count   = 0;
    foreach($csvData as $_data) {
        $count++;
        $sku   = $_data[getIndex('sku')];
        $price = $_data[getIndex('price')];

        if ( ! checkIfSkuExists($sku)) {
            $message =  $count .'. FAILURE:: Product with SKU (' . $sku . ') doesn\'t exist.';
            mpLogAndPrint($message);
            continue;
        }

        try {
            updatePrices($sku, $price);
            $message = $count . '. SUCCESS:: Updated SKU (' . $sku . ') with price (' . $price . ')';
            mpLogAndPrint($message);
        } catch(Exception $e) {
            $message =  $count . '. ERROR:: While updating  SKU (' . $sku . ') with Price (' . $price . ') => ' . $e->getMessage();
            mpLogAndPrint($message);
        }
    }
} catch (Exception $e) {
    mpLogAndPrint(
        'EXCEPTION::' . $e->getTraceAsString()
    );
}

And the sample CSV can be used as:

"sku","price"
"24-MB01","31"
"24-MX02","32"
"24-UG04","94"
"24-UG05","99"

Source: http://bit.ly/M2-Bulk-Price-Update

  • I'm trying to implement your script in my Magento project, as described here blog.magepsycho.com/… However, I'm getting the following error: "EXCEPTION::#0 C:\Server\htdocs\pub\update-prices.php(182): ctv_exceptions_error_handler(8, 'Undefined offse...', 'C:\\Server\\htdoc...', 182, Array) #1 {main}" At line 182 in update-prices.php: "$sku = $_data[getIndex('sku')];" What exactly is the issue here? Is it malformatting of the csv file? Can you help me fix this? I appreciate it. – Iveta Allogenes Apr 11 at 15:41
0

May be you can try the below code.

<?php
error_reporting(1);
ini_set('max_execution_time', 0);

use \Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$state = $objectManager->get('\Magento\Framework\App\State');
$state->setAreaCode('frontend');
$product_id = 1;
$price = "20.12";
$productFactory = $objectManager->get('\Magento\Catalog\Model\ProductFactory');
$product = $productFactory->create()->load($product_id);
$product->setPrice($price);
$product->save();

It is working for me.

don't forget clear page cache.

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.