在 Prestashop 1.6 上以编程方式将产品添加到高级库存管理

问题描述 投票:0回答:2

我正在尝试分配某些产品用于高级库存管理,在数据库中似乎没问题,但有些东西在 BO 上没有勾选,我想知道这是否会产生任何影响。

例如,当选择商店组并在 BO 中查看产品时,我没有看到“我想对此产品使用高级库存管理”,而“取决于库存”单选按钮已正确选择(并且因此,如果未触及该复选框,则该复选框将被禁用)。我不知道这是否对产品库存有问题。如果有人对此有任何意见,我们将不胜感激。

这是我到目前为止所拥有的:

$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']);
    }
}
prestashop prestashop-1.6
2个回答
1
投票

StockAvailable::setProductDependsOnStock
不会启用该产品的 AdvancedStockManagement。您还需要设置产品AdvancedStockManagement。您可以使用:

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

0
投票

为了在高级库存管理中完整保留记录,您应该添加并为产品分配特定仓库。

$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();

@sadlyblue 答案代码中缺少此内容。

完整的工作代码可以是这样的:

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

我在我的项目中使用了它,它在 Thirtybees 中的效果相同。

© www.soinside.com 2019 - 2024. All rights reserved.