You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Prestashop 1.6库存覆盖更新PHP脚本开发需求

PHP Script to Bulk Update PrestaShop 1.6 Inventory via CSV (SKU Match)

Hey, I’ve been in your shoes—dealing with PrestaShop 1.6 inventory updates can be a pain when modules don’t play nice with your supplier’s CSV. Let’s cut to the chase: here’s a custom PHP script that’ll do exactly what you need, no fancy modules required. It reads your CSV, matches products by SKU (PrestaShop’s "product reference"), and overwrites the quantity field directly in the database.

Prerequisites

  • Your CSV needs at least two columns: SKU (matches PrestaShop’s product reference) and 可用库存 (the new stock quantity)
  • Drop the CSV file in your PrestaShop root folder (or adjust the file path in the script)
  • Critical: Backup your PrestaShop database first—always safe to do before bulk updates!

Full Script

Save this as update_inventory.php in your PrestaShop root directory:

<?php
// Load PrestaShop's core config to reuse its database connection
require_once('config/config.inc.php');
require_once('init.php');

// Path to your supplier's CSV file - tweak this if your file is elsewhere
$csvFile = 'supplier_stock.csv';

// Check if CSV exists
if (!file_exists($csvFile)) {
    die("Oops: Can't find the CSV file at $csvFile");
}

// Open the CSV for reading
$handle = fopen($csvFile, 'r');
if (!$handle) {
    die("Error: Failed to open the CSV file");
}

// Skip the header row (delete this line if your CSV doesn't have a header)
fgetcsv($handle);

// Track progress
$updated = 0;
$skipped = 0;

// Process each row in the CSV
while (($row = fgetcsv($handle, 1000, ',')) !== FALSE) {
    // Adjust these indices if your CSV columns are in a different order
    $sku = trim($row[0]); // Column 0 = SKU (product reference)
    $newQty = (int)trim($row[1]); // Column 1 = 可用库存 (new quantity)

    // Skip rows with empty SKU or invalid quantity
    if (empty($sku) || $newQty < 0) {
        $skipped++;
        continue;
    }

    // Find the product ID using its SKU/reference
    $productId = Db::getInstance()->getValue(
        'SELECT id_product FROM '._DB_PREFIX_.'product WHERE reference = "'.pSQL($sku).'"'
    );

    if (!$productId) {
        echo "Warning: No product found with SKU: $sku\n";
        $skipped++;
        continue;
    }

    // Update the stock quantity (targets simple products - no combinations)
    $updateSuccess = Db::getInstance()->execute(
        'UPDATE '._DB_PREFIX_.'stock_available 
         SET quantity = '.(int)$newQty.' 
         WHERE id_product = '.(int)$productId.' AND id_product_attribute = 0'
    );

    if ($updateSuccess) {
        echo "Updated: SKU $sku | New Quantity: $newQty\n";
        $updated++;
    } else {
        echo "Error: Failed to update SKU $sku\n";
        $skipped++;
    }
}

fclose($handle);

// Show final summary
echo "\n--- Update Summary ---\n";
echo "Total products updated: $updated\n";
echo "Total products skipped: $skipped\n";
?>

How It Works

  • PrestaShop Integration: We load config/config.inc.php to use PrestaShop’s existing database connection—no need to hardcode your DB credentials.
  • CSV Handling: The script skips the header row (remove fgetcsv($handle); if your CSV doesn’t have one). Adjust $row[0] and $row[1] if your CSV columns are ordered differently.
  • SKU Matching: It queries the product table to find the product ID linked to your CSV’s SKU.
  • Stock Update: Updates the quantity in the stock_available table. The id_product_attribute = 0 line targets simple products—if you sell configurable products (size/color combinations), let me know and I can tweak this part!
  • Real-Time Feedback: The script prints updates as it runs, so you can see exactly which products were updated or skipped.

Usage Steps

  1. Save the script as update_inventory.php in your PrestaShop root folder.
  2. Rename your supplier’s CSV to supplier_stock.csv (or update the $csvFile path in the script).
  3. Run the script via CLI (best for large CSVs):
    php update_inventory.php
    
    • If you need to run it via browser, add IP restriction or password protection first (e.g., using .htaccess) to keep it secure.
  4. Check the final summary to confirm the updates.

Important Notes

  • Backup First: I can’t stress this enough—always back up your database before running bulk updates. Use phpMyAdmin or PrestaShop’s built-in backup tool.
  • CSV Encoding: Make sure your CSV is saved in UTF-8 format to avoid character issues with SKUs.
  • Configurable Products: If you sell products with combinations, the script above won’t update those. Reach out and I’ll help adjust it to match SKUs to product attributes.
  • Security: Delete the script from your server after use, or restrict access if you set up a daily cron job for automatic updates.

内容的提问来源于stack exchange,提问作者Hashtag CV

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:24:16