温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - How to update product.quantity in PrestaShop with PrestaSharp?

其他 - 如何使用PrestaSharp更新PrestaShop中的product.quantity?

发布于 2020-03-27 16:19:24

我正在为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);

我应该如何进行?提前致谢

查看更多

查看更多

提问者
Biiz
被浏览
39
Biiz 2020-02-03 20:52

基于该github答复,我能够转换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);

发布
问题

分享
好友

手机
浏览

扫码手机浏览