Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save narodnaia/f8ad17613ccebcfed21c389c7c7806bc to your computer and use it in GitHub Desktop.
Save narodnaia/f8ad17613ccebcfed21c389c7c7806bc to your computer and use it in GitHub Desktop.
Prestashop - Out of stock notification per product
<?php
/**
* Intended to run on command line. Outputs csv(;)
* Redirect output to file and attach to email, or save
* in folder.
* Minimum stock is saved (per product) as a product feature.
* You must specify feature id
*/
define('DB_HOST', 'your-host');
define('DB_USERNAME', 'your-username');
define('DB_PASSWORD', 'your-password');
define('PS_DATABASE', 'your-prestashop-database');
define('PS_PREFIX', 'your-database-prefix');
define('MIN_STOCK_FEATURE_ID', your-min-stock-feature-id);
try {
$db = new PDO(
sprintf(
'mysql:host=%s;dbname=%s', DB_HOST, PS_DATABASE
),
DB_USERNAME,
DB_PASSWORD
);
} catch (PDOException $e) {
echo 'Database connection failed';
}
$stmt = $db->query('SELECT distinct l.value,s.quantity,p.reference,la.name
from ' . PS_PREFIX . 'product as p
inner join ' . PS_PREFIX . 'stock_available as s on p.id_product = s.id_product
inner join ' . PS_PREFIX . 'feature_product as f on p.id_product = f.id_product
inner join ' . PS_PREFIX . 'feature_value_lang as l on f.id_feature_value = l.id_feature_value
inner join ' . PS_PREFIX . 'product_lang as la on p.id_product = la.id_product
where f.id_feature = ' . MIN_STOCK_FEATURE_ID . '
group by p.reference');
echo 'Reference' . ";" . 'Name' . ";" . 'Stock' . ";" . 'Min. Stock' . "\n";
while ($product = $stmt->fetch(PDO::FETCH_ASSOC)) {
$referencia = $product['reference'];
$min_stock = $product['value'];
$stock = $product['quantity'];
$name = utf8_encode($product['name']);
if ( $stock <= $min_stock ) {
echo $reference . ";" . $name . ";" . $stock . ";" . $min_stock . "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment