如何使用 PrestaSharp 在 PrestaShop 中更新 product.quantity?

How to update product.quantity in PrestaShop with PrestaSharp?

我正在为 C# 使用 prestaSharp。我需要更新产品数量,但 product.quantity 属性 是 readonly.

var ProductFactory = new Bukimedia.PrestaSharp.Factories.ProductFactory(BaseUrl, Account, Password);
List<Bukimedia.PrestaSharp.Entities.product> products = ProductFactory.GetAll();
foreach (var item in myItems)
{
    var productToUpdate = products.First(x => x.reference == item.Ref);
    // update the quantities
    if (productToUpdate != null)
    {
        productToUpdate.quantity = item.Quantity;
    }
}
await ProductFactory.UpdateListAsync(products);

我该如何进行?提前致谢

Prestashop 还有另一个 table 数量:"Stock"

您必须使用此 table 和相关 类。像这样:

StockAvailable::setQuantity($id_product, $id_product_attribute, $quantity);

基于此 github reply 我能够转换 VB 代码,并在一次调用中更新所有股票

var ProductFactory = new Bukimedia.PrestaSharp.Factories.ProductFactory(BaseUrl, Account, Password);
var StockAvailableFactory = new Bukimedia.PrestaSharp.Factories.StockAvailableFactory(BaseUrl, Account, Password);

// call prestasharp/api/Products to get the products
List<Bukimedia.PrestaSharp.Entities.product> products = ProductFactory.GetAll();

var stocksToUpdate = new List<Bukimedia.PrestaSharp.Entities.stock_available>();
foreach (var itemQ in itemsQuantity)
{
    var productToUpdate = products.First(x => x.reference == itemQ.Ref);
    // update the quantities
    if (productToUpdate != null)
    {
        var dtnSearch = new Dictionary<string, string>();
        dtnSearch.Add("id_product", productToUpdate.id.ToString());
        var currentStock = await StockAvailableFactory.GetByFilterAsync(dtnSearch, null, null);
        if(0 < currentStock.Count())
        {
            currentStock[0].quantity = Decimal.ToInt32(itemQ.Quantity);
            stocksToUpdate.Add(currentStock[0]);
        }
    }
}
await StockAvailableFactory.UpdateListAsync(stocksToUpdate);