Search code examples
prestashopprestashop-1.6

Adding a product to advanced stock management programmatically on Prestashop 1.6


I am trying to assign certain products for be used with advanced stock management, it seems OK in the DB, but some things aren't ticked on the BO, I'm wondering if this might have any impact.

For example, when selecting shop groups and going on a product in the BO, I don't see "I want to use advanced stock management for this product" ticked, while the "depends on stock" radio button is correctly selected (and thus disabled since the checkbox if left untouched). I don't know if this is problematic or not for the stock of the product. If anyone had any input on this, it would be appreciated.

Here is what I have so far:

$warehouse = new Warehouse(1);
$stock_manager = StockManagerFactory::getManager();
foreach($prods as $prod)
{
    StockAvailable::setProductDependsOnStock($prod['id_product'], 1);
    if ($stock_manager->addProduct($prod['id_product'], 0, $warehouse, 1, 5, 0.1, 1))
    {
        StockAvailable::synchronize($prod['id_product']);
    }
}

Solution

  • The StockAvailable::setProductDependsOnStock doesn't enable the AdvancedStockManagement for that product. You also need to set the product AdvancedStockManagement. You can use:

    $warehouse = new Warehouse(1);
    $stock_manager = StockManagerFactory::getManager();
    foreach($prods as $prod)
    {
        $product = new Product($prod['id_product']);
        if(!$product->advanced_stock_management)
            $product->setAdvancedStockManagement(true);
        StockAvailable::setProductDependsOnStock($prod['id_product'], 1);
        if ($stock_manager->addProduct($prod['id_product'], 0, $warehouse, 1, 5, 0.1, 1))
        {
            StockAvailable::synchronize($prod['id_product']);
        }
    }