Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/cartforge.co/scripts/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/cartforge.co/scripts/update_prices.php
<?php

use Magento\Framework\App\Bootstrap;

require __DIR__ . '/../app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get(\Magento\Framework\App\State::class);
$state->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);

// File path to the CSV
$csvFilePath = 'Pricelist 15 - Sheet1.csv';

// Check if file exists
if (!file_exists($csvFilePath)) {
    die("File not found: $csvFilePath");
}

// Open the CSV file
$csvData = array_map('str_getcsv', file($csvFilePath));
$headers = array_shift($csvData); // Get the headers

// Get Product Repository
$productRepository = $objectManager->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);

// Iterate through the CSV rows
foreach ($csvData as $row) {
    $data = array_combine($headers, $row);
    $sku = $data['Item number']; // Assuming the column is named 'ItemCode'
    $price = $data['Sales price'];  // Assuming the column is named 'Price'


    if (empty($sku) || empty($price)) {
        continue; // Skip if SKU or Price is missing
    }

    try {
        // Load product by SKU
        $product = $productRepository->get($sku);
        // Calculate the new price
        $newPrice = $price * 1.20;

        // Update product price
        $product->setPrice($newPrice);
        $productRepository->save($product);

        echo "Updated price for SKU: $sku to $newPrice\n";
    } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
        echo "Product with SKU: $sku not found.\n";
    } catch (\Exception $e) {
        echo "Error updating SKU: $sku. Error: " . $e->getMessage() . "\n";
    }
}

Spamworldpro Mini