Changing the way the Quantity option is displayed based on the Maximum cart setting

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 anni tempo fa
In my Nopcommerce store some of my product variants have a maximum cart setting.
I would like to have the quantity option in my store change based on the maximum cart setting.  

For instance, if the maximum cart setting was the default setting of 10,000 I would like the quantity option to stay how it is, just a text box that allows the customer to enter the quantity.

However, if I set the maximum cart setting for a product variant was something under 10; I would like the quantity option to be displayed as a dropdown menu - 1 through the maximum cart setting.
In the following post it was discussed how you could change the quantity box to a dropdown menu.

https://www.nopcommerce.com/boards/t/16874/change-qty-to-drop-box-and-default-is-zero.aspx

However, I am getting an error when I try to reference the maximum cart setting in the _ProductVariantAddToCart.cshtml.

Here is how I tried to code it.

@if (!Model.DisableBuyButton || !Model.DisableWishlistButton)
    {
if (Model.OrderMaximumQuantity <= 10)
{
var maxQty = Model.OrderMaximumQuantity;
var selectList = new List<SelectListItem>(maxQty);
for (var i = 1; i <= maxQty; i++)
{
selectList.Add(new SelectListItem { Text = i.ToString(), Value = i.ToString() });
}
@Html.DropDownListFor(model => model.EnteredQuantity, selectList, new { @class = "qty-input" })
}
else
{
@Html.LabelFor(model => model.EnteredQuantity)<text>:</text>
@Html.TextBoxFor(model => model.EnteredQuantity, new { style = "Width: 40px;" })  
}
}

The _ProductVariantAddToCart.cshtml  does not recognize the Model.OrderMaximumQuantity in the code. I think I need to use @model ProductVariant  in order to get the Model.OrderMaximumQuantity to work.  However this makes the model.EnteredQuantity show up as an error. So I thought that I might be able to put both at the top like


@model Nop.Web.Models.Catalog.ProductModel.ProductVariantModel.AddToCartModel
@model ProductVariant  

but this was also an error.

Does anyone know how I can reference the maximum cart settting in _ProductVariantAddToCart.cshtml?

I am not a programmer so if someone could look at the code and show me how to fix it or how I should go about this I would greatly appreciate it.

I am using Nopcommerce Version 2.4

Thanks in advance!
12 anni tempo fa
This would require changes to the source code.

    Add a property to the class AddToCartModel (file: \Presentation\Nop.Web\Models\CatalogProductModel.cs)
    
        public int OrderMaximumQuantity { get; set; }
        
      
    Update the PrepareProductVariantModel() method (file: \Presentation\Nop.Web\Controllers\CatalogController.cs)        
    In the region labeled #region 'Add to cart' model, add the following:
    
        model.AddToCart.OrderMaximumQuantity = productVariant.OrderMaximumQuantity;

        
   You will need to recompile the Nop.Web project.

---        
        
    Update view: \Views\Catalog\_ProductVariantAddToCart.cshtml
    
    Remove or comment out line 16 and add the following code:
    var maxQty = Model.OrderMaximumQuantity;
    if (maxQty <= 10)
    {
        var selectList = new List<SelectListItem>(maxQty);
        for (var i = 1; i <= maxQty; i++)
        {
            selectList.Add(new SelectListItem { Text = i.ToString(), Value = i.ToString() });
        }
        @Html.DropDownListFor(model => model.EnteredQuantity, selectList, new { @class = "qty-input" })
    }
    else
    {
        @Html.TextBoxFor(model => model.EnteredQuantity, new { @class = "qty-input" })
    }
.
12 anni tempo fa
It works great!
  Thank you so much for taking the time to answer my question, provide the code, and explain the steps in detail.  I really appreciate you helping me figure out how to do this.  

It is amazing how helpful poeple are on the Nopcommerce forum.  Thanks!
11 anni tempo fa
Hi,

I’m trying to do something similar but I would like the Quantity dropdown to populate with the available stock. E.g.. if there are only 10 products available I would like the dropdown to only display 1 to 10?

Can this be done without too much trouble?

Cheers,

Mike.
11 anni tempo fa
Hi,

I’m trying to do something similar but I would like the Quantity dropdown to populate with the available stock. E.g.. if there are only 10 products available I would like the dropdown to only display 1 to 10?

Can this be done without too much trouble?

Cheers,

Mike.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.