No overload for method 'AddProductToCart' takes 1 arguments Nop.Web.MVC.Tests\Public\Infrastructure\RoutesTests.cs

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
Il y a 11 ans
Right -- thank you will do....
Il y a 11 ans
Andy I am aware you busy.. But your advise would be great. I have wrote this method but I'm not sure its correct to what you advised me to wright. Basic breakdown -- knowing that I don't know if its correct.

Called the product service to get the Id's of each product
I have taken from the Update cart method. Form key's to EnterQuantity's(I think) that will hopefully addtocart directing all products to cart.

With the return String I have redirect the return Url -- I know this works as I have tested on the AddToCartMethod in the shopping cart controller and work's fine.


[HttpPost, ActionName("Product")]
public ActionResult AddMultipleProductsToCart(int productId, FormCollection form, int quantity = 1)
        {
            var product = _productService.GetProductById(productId);
            if (product == null)
                return RedirectToAction("Index", "Home");

            int productVariantId = 0;
            
            foreach (string formKey in form.AllKeys)

if (formKey.Equals(string.Format("price_{0}.EnteredQuantity", productVariantId), StringComparison.InvariantCultureIgnoreCase))
{
    var productVariant = _productService.GetProductVariantById(productVariantId);
    if (int.TryParse(form[formKey], out quantity))
    {
         var addtocart = _shoppingCartService.AddToCart(_workContext.CurrentCustomer,
         productVariant, ShoppingCartType.ShoppingCart, string.Empty, decimal.Zero, quantity, true);
    }
  }
if (_shoppingCartService.DirectAddToCartAllowed(productId, out productVariantId))
{
     var productVariant = _productService.GetProductVariantById(productVariantId);
     var addToCart = _shoppingCartService.AddToCart(_workContext.CurrentCustomer,
     productVariant, ShoppingCartType.ShoppingCart, string.Empty, decimal.Zero, quantity, true);
}
            

string returnUrl = _workContext.CurrentCustomer.GetAttribute<string>(SystemCustomerAttributeNames.LastContinueShoppingPage);
return Redirect(returnUrl);
}


As I said I'm not sure if this is correct for what I wanted I am on the right track also how do I define this view its created but I truely don't know??

All help highly apped

Richard...
Il y a 11 ans
I have to say I am stuck worse than a fly on a spider's web.

Would It possible you could break down further what you meant by the following

You would need a new method in your controller that takes a list of ProductId/Qty pairs and puts them into the cart.  Then your view would need to compile that list based on what items are checked and the quantity entered

I have the following method in the ShoppingCartController.


[HttpPost, ActionName("Product")]
public ActionResult AddMultipleProductsToCart(int productId, FormCollection form, int quantity = 1)
{
            var product = _productService.GetProductById(productId);
            
            if (product == null)
                return RedirectToAction("Index", "Home");
            
            string returnUrl = _workContext.CurrentCustomer.GetAttribute<string>(SystemCustomerAttributeNames.LastContinueShoppingPage);
            
            int productVariantId = 0;
            foreach (string formKey in form.AllKeys)
            
            if (formKey.Equals(string.Format("price_{0}.EnteredQuantity", productVariantId), StringComparison.InvariantCultureIgnoreCase))
            {
            
                    var productVariant = _productService.GetProductVariantById(productVariantId);
                    if (int.TryParse(form[formKey], out quantity))
                    {
                        var addtocart = _shoppingCartService.AddToCart(_workContext.CurrentCustomer,
                        productVariant, ShoppingCartType.ShoppingCart, string.Empty, decimal.Zero, quantity, true);
                    }
            }
            
            if (_shoppingCartService.DirectAddToCartAllowed(productId, out productVariantId))
            {
                var productVariant = _productService.GetProductVariantById(productVariantId);
                var addToCartWarings = _shoppingCartService.AddToCart(_workContext.CurrentCustomer,
                    productVariant, ShoppingCartType.ShoppingCart,
                    string.Empty, decimal.Zero, quantity, true);
                if (addToCartWarings.Count == 0)
                    return Redirect(returnUrl);

            }
      
            return Redirect(returnUrl);
        }


I have generated the following route.


//Richard Evans -- Description 17/8/2012 --
//Concord Localized Route for Inseting Multiple Qty's into Cart.
            routes.MapLocalizedRoute("AddMultipleProductsToCart",
                           "cart/addproduct/{productId}/quantity/{quantity}",
                            new { controller = "ShoppingCart", action = "AddMultipleProductsToCart" },
                            new { productId = @"\d+" },
                            new[] { "Nop.Web.Controllers" });


I have contined to use the same view for testing purpose in the Category.Template - I launched in the source code I found out that the button is tracking to Model.Id/2 Category based Id. and becomes undefined. I know this is not the correct course of action.

But I looked at the related product controller and the view and can see that it incorperartes a list as below,
The only thought that concerns me with this is that It will overide the ProductOverview Method as it generates a list itself or is plawsable to apply both..

var relatedProductsModel = relatedProducts
                .Select(x =>
                {
                    return new ProductModel.RelatedProductModel()
                    {
                        Id = x.Id,
                        ProductId1 = x.ProductId1,
                        ProductId2 = x.ProductId2,
                        Product2Name = _productService.GetProductById(x.ProductId2).Name,
                        DisplayOrder = x.DisplayOrder
                    };
                })
                .ToList();


Do you think it would be an idea to incorperate this....

All help highly regarded

Richard
Il y a 11 ans
Your function looks fine from what I can tell.  I don't really want to get into fully breaking this down because it's becoming a huge thing and it's not really well suited to a forum.  There are a few examples in the Admin project that show a list of things and let you check the boxes for the ones you want.  You basically need to recreate that on the Web side.
Il y a 11 ans
Hi Andy

I know you said that this is getting to big for this thread I will be writing on a seperate forum page. I'm aware you are busy,

I have wrote the new method as follow's in the Nop/Web/Controllers/ShoppingCart.cs

[HttpPost]
        public ActionResult AddMultipleProductToCart(int productId)
        {
            //current we support only ShoppingCartType.ShoppingCart
            const ShoppingCartType shoppingCartType = ShoppingCartType.ShoppingCart;
            
            string returnUrl = _workContext.CurrentCustomer.GetAttribute<string>(SystemCustomerAttributeNames.LastContinueShoppingPage);
            
            var product = _productService.GetProductById(productId);
            if (product == null)
                //no product found
                return RedirectToAction("Index", "Home");

            var productVariants = _productService.GetProductVariantsByProductId(productId);
            if (productVariants.Count != 1)
            {
                {
                    return Redirect(returnUrl);
                }
            }

            //get default product variant
            var defaultProductVariant = productVariants[0];
  
            //quantity to add
            var qtyToAdd = defaultProductVariant.OrderMinimumQuantity > 0 ?
                defaultProductVariant.OrderMinimumQuantity : 1;

            //get standard warnings without attribute validations
            //first, try to find existing shopping cart item
            var cart = _workContext
                .CurrentCustomer
                .ShoppingCartItems
                .Where(sci => sci.ShoppingCartType == shoppingCartType)
                .ToList();
            var shoppingCartItem = _shoppingCartService
                .FindShoppingCartItemInTheCart(cart, shoppingCartType, defaultProductVariant);
            //if we already have the same product variant in the cart, then use the total quantity to validate
            
            var quantityToValidate = shoppingCartItem != null ?
                shoppingCartItem.Quantity + qtyToAdd : qtyToAdd;

            var addToCartWarnings = _shoppingCartService
                .GetShoppingCartItemWarnings(_workContext.CurrentCustomer, ShoppingCartType.ShoppingCart,
                 defaultProductVariant, string.Empty, decimal.Zero, quantityToValidate, false);
            
            if (addToCartWarnings.Count > 0)
            {
                return Redirect(returnUrl);
            }

            //now let's try adding product to the cart (now including product attribute validation, etc)
            addToCartWarnings = _shoppingCartService.AddToCart(_workContext.CurrentCustomer,
                defaultProductVariant, ShoppingCartType.ShoppingCart,
                string.Empty, decimal.Zero, qtyToAdd, true);
            
            if (addToCartWarnings.Count > 0)
            {
              return Redirect(returnUrl);
            }

            return Redirect(returnUrl);
        }


There seems to be no error's when debugging.

I'm slightly confused about the view.

I have kept the ProductBox but done allot of alteration

Using the The Category.Template I Have the following code


@foreach (var product in Model.Products)
     {
         <tr>
         @Html.Partial("_ConcordProductBox", product)
         </tr>
     }
     <td class = "addtocart">
     <input type="button" value="@T("Order")" class="productlistaddtocartbutton" onclick="setLocation('@(@Url.RouteUrl("ConcordAddMultipleProductsToCart", new { productId = Model.Id }))/' + $('#quantity_').val())" />
     </td>

I have added the following route.

//Richard Evans -- Description Add Multiple Products to Cart 17/8/2012
            //Start (Test - Revist)
            routes.MapLocalizedRoute("ConcordAddMultipleProductsToCart",
                            "cart/addproduct/{productId}/quantity/{quantity}",
                            new { controller = "Catalog", action = "ConcordAddMultipleProductsToCart" },
                            new { productId = @"\d+" },
                            new[] { "Nop.Web.Controllers" });
            //Finish

On excuting it becomes undefined In the source  with the following error

Server Error in '/' Application.
--------------------------------------------------------------------------------

The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.

Requested URL: /undefined


and in the Source View I have the following

 <input type="button" value="Order" class="productlistaddtocartbutton" onclick="setLocation('/' + $('#quantity_').val())" />


Is there away I can define the Multiple ProductId with paired Quantity's with this input button.

I have looked at various List view's in the Admin area but they tend to use MVC.Telerik UI. But I thought it would be more productive to use basic HTML/CSS/Jquery.

I know you said you where busy but If you help it would be highly regarded.

Kind Regards

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