public ActionResult RemoveOne(int id)
        {
            if (!_orchardServices.Authorizer.Authorize(CommercePermissions.ManageProducts, null, T("Not authorized to manage products")))
            {
                return(new HttpUnauthorizedResult());
            }

            var product = _contentManager.Get <ProductPart>(id);

            _productInventoryService.UpdateInventory(product, -1);
            Dictionary <string, int> newInventory;
            IBundleService           bundleService;

            if (_wca.GetContext().TryResolve(out bundleService))
            {
                var affectedBundles = _contentManager.Query <BundlePart, BundlePartRecord>()
                                      .Where(b => b.Products.Any(p => p.ContentItemRecord.Id == product.Id))
                                      .WithQueryHints(new QueryHints().ExpandParts <ProductPart>())
                                      .List();
                newInventory = affectedBundles.ToDictionary(
                    b => b.As <ProductPart>().Sku,
                    b => bundleService.GetProductQuantitiesFor(b).Min(p => _productInventoryService.GetInventory(p.Product) / p.Quantity));
            }
            else
            {
                newInventory = new Dictionary <string, int>(1);
            }
            newInventory.Add(product.Sku, _productInventoryService.GetInventory(product));
            return(new JsonResult {
                Data = newInventory
            });
        }
Example #2
0
        public ActionResult RemoveOne(int id)
        {
            var bundle   = _contentManager.Get <BundlePart>(id);
            var products = _bundleService.GetProductQuantitiesFor(bundle).ToList();

            foreach (var productPartQuantity in products)
            {
                //These calls will also update the inventory for the bundle
                _productInventoryService.UpdateInventory(productPartQuantity.Product, -productPartQuantity.Quantity);
            }
            var newInventory = products.ToDictionary(p => p.Product.Sku, p => _productInventoryService.GetInventory(p.Product));

            newInventory.Add(bundle.As <ProductPart>().Sku, products.Min(p => _productInventoryService.GetInventory(p.Product) / p.Quantity));
            return(new JsonResult {
                Data = newInventory
            });
        }
        protected override DriverResult Display(
            ProductPart part, string displayType, dynamic shapeHelper)
        {
            var inventory = _productInventoryService.GetInventory(part);
            var discountedPriceQuantity = _priceService.GetDiscountedPrice(new ShoppingCartQuantityProduct(1, part));
            var priceTiers = _tieredPriceProvider != null?_tieredPriceProvider.GetPriceTiers(part) : null;

            var discountedPriceTiers = _priceService.GetDiscountedPriceTiers(part);
            var shapes = new List <DriverResult>();

            shapes.Add(ContentShape(
                           "Parts_Product",
                           () => shapeHelper.Parts_Product(
                               Sku: part.Sku,
                               Price: part.Price,
                               DiscountedPrice: discountedPriceQuantity.Price,
                               DiscountComment: discountedPriceQuantity.Comment,
                               Inventory: inventory,
                               OutOfStockMessage: part.OutOfStockMessage,
                               AllowBackOrder: part.AllowBackOrder,
                               Weight: part.Weight,
                               Size: part.Size,
                               ShippingCost: part.ShippingCost,
                               IsDigital: part.IsDigital,
                               ConsiderInventory: part.ConsiderInventory,
                               MinimumOrderQuantity: part.MinimumOrderQuantity,
                               ContentPart: part,
                               CurrencyProvider: _currencyProvider
                               )
                           ));
            if (part.Inventory > 0 || part.AllowBackOrder || (part.IsDigital && !part.ConsiderInventory))
            {
                shapes.Add(ContentShape(
                               "Parts_Product_AddButton",
                               () => {
                    // Get attributes and add them to the add to cart shape
                    var attributeShapes = _attributeProviders
                                          .Select(p => p.GetAttributeDisplayShape(part.ContentItem, shapeHelper))
                                          .ToList();
                    return(shapeHelper.Parts_Product_AddButton(
                               ProductId: part.Id,
                               MinimumOrderQuantity: part.MinimumOrderQuantity,
                               ProductAttributes: attributeShapes));
                })
                           );
            }
            if (priceTiers != null)
            {
                shapes.Add(ContentShape(
                               "Parts_Product_PriceTiers",
                               () => {
                    return(shapeHelper.Parts_Product_PriceTiers(
                               PriceTiers: priceTiers,
                               DiscountedPriceTiers: discountedPriceTiers,
                               CurrencyProvider: _currencyProvider
                               ));
                })
                           );
            }
            return(Combined(shapes.ToArray()));
        }