Julien Lachal
Julien Lachal

Reputation: 1141

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']);
    }
}

Upvotes: 0

Views: 724

Answers (2)

deWebLooper
deWebLooper

Reputation: 21

For complete keeping record in Advanced Stock Management you should add also assign certain warehouse to product.

$warehouse_location = new WarehouseProductLocation();
$warehouse_location -> id_product = $prod['id_product'];
$warehouse_location -> id_product_attribute = 0; // need to be set if necessary
$warehouse_location -> id_warehouse = **$id_warehouse**;
$warehouse_location -> location = '';
$warehouse_location -> save();

This is missing in @sadlyblue answer code.

So full working code can be something like this:

// set variables
$msg = [];
$err = [];

$out_of_stock = 2; // 0 = deny, 1 = allow, 2 = by default
$id_warehouse = 2; // 1, 2, ...
$prods = [
    [
        'id_product' => 14,
    ],
    
];

$warehouse = new Warehouse($id_warehouse);
$stock_manager = StockManagerFactory::getManager();

foreach ($prods as $prod) {
    $product = new Product($prod['id_product']);

    if (!$product->advanced_stock_management) {
        $product->setAdvancedStockManagement(1);
    }
        
    StockAvailable::setProductDependsOnStock($prod['id_product'], 1);
    StockAvailable::setProductOutOfStock($prod['id_product'], $out_of_stock); //idProduct, outOfStock, idShop, idProductAttribute

    if ($stock_manager->addProduct(
        $prod['id_product'], // idProduct (int)
        0,                   // idProductAttribute (int)
        $warehouse,          // warehouse data by warehouse id (array)
        123,                 // quantity (int)
        1,                   // idStockMvtReason (int)
        0.0,                 // priceTe (float)
        1                    // isUsable (bool)
    )) {
        StockAvailable::synchronize($prod['id_product']);

        $warehouse_location = new WarehouseProductLocation();
        $warehouse_location -> id_product = $prod['id_product'];
        $warehouse_location -> id_product_attribute = 0; // need to be set if necessary
        $warehouse_location -> id_warehouse = $id_warehouse;
        $warehouse_location -> location = '';
        $warehouse_location -> save();

        $msg[] = 'Product ID:' . $prod['id_product'] . ' set to Advanced Stock Mmanagement';
    } else {
        $err[] = 'Unable to set product ID:' . $prod['id_product'] . ' to Advanced Stock Mmanagement';
    }
}

I used it in my project, it works same in Thirtybees.

Upvotes: 0

sadlyblue
sadlyblue

Reputation: 2987

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']);
    }
}

Upvotes: 1

Related Questions