300 individual product in cart

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
4 years ago
I am using the bellow code to migrating the shopping cart. Because I have 300 products in the cart when going to log in it is taking about 5-6 min. Is it ok to sqlcommand rather an iteration?

var toCustomerId = _dataProvider.GetInt32Parameter("ToCustomerId", toCustomer.Id);
var fromCustomerId = _dataProvider.GetInt32Parameter("FromCustomerId", fromCustomer?.Id);
            var shoppingCartTypeId = _dataProvider.GetInt32Parameter("ShoppingCartType", (int)ShoppingCartType.ShoppingCart);

             string sqlForToCustomer = $"UPDATE ShoppingCartItem SET CustomerId=@ToCustomerId" +
                    $" WHERE CustomerId=@FromCustomerId " +
            $"AND ShoppingCartTypeId=@ShoppingCartType";


            var updateResultaftermigration = _dbContext.ExecuteSqlCommand(sqlForToCustomer, false, null, toCustomerId, fromCustomerId, shoppingCartTypeId);
4 years ago
"iteration" to do what?  There is no built in service/method for migrating items from one customer cart to another.

Is this a one time migration?  (Why use C# code rather than just execute SQL commands in SQL tool?)
4 years ago
Did you already try the "MigrateShoppingCart" method in the ShoppingCartService?  

Looks like it runs 2 for loops, the first to add each item to a new cart and then the second to delete each item from the old cart.  I can see how that might take a while for 300 products but have not used it for more than ~10 items (which was very quick).  

Is this a one time occurrence, or will every customer have hundreds of items in their cart and you need to migrate to a different cart more than once?
4 years ago
OK, I stand corrected; it looks like there is a "built in service/method for migrating items from one customer cart to another".   But from where it's used in the Login, I suspect it is just to migrate any items in the Guest cart (before login) to the registered user (after login).  So, I don't see how that should impact your login time if the registered user has the 300 items in cart.

\Presentation\Nop.Web\Controllers\CustomerController.cs
public virtual IActionResult Login(LoginModel model, string returnUrl, bool captchaValid)
... _shoppingCartService.MigrateShoppingCart(_workContext.CurrentCustomer, customer, true);
4 years ago
af1racing wrote:
Did you already try the "MigrateShoppingCart" method in the ShoppingCartService?  

Looks like it runs 2 for loops, the first to add each item to a new cart and then the second to delete each item from the old cart.  I can see how that might take a while for 300 products but have not used it for more than ~10 items (which was very quick).  

Is this a one time occurrence, or will every customer have hundreds of items in their cart and you need to migrate to a different cart more than once?


That is very true for 10 it is not a problem. But Just try it with 300 individual products for 4.2, please. You can say it is insane to add 300 products at cart. But for some type of business it very obvious.
4 years ago
New York wrote:
OK, I stand corrected; it looks like there is a "built in service/method for migrating items from one customer cart to another".   But from where it's used in the Login, I suspect it is just to migrate any items in the Guest cart (before login) to the registered user (after login).  So, I don't see how that should impact your login time if the registered user has the 300 items in cart.

\Presentation\Nop.Web\Controllers\CustomerController.cs
public virtual IActionResult Login(LoginModel model, string returnUrl, bool captchaValid)
... _shoppingCartService.MigrateShoppingCart(_workContext.CurrentCustomer, customer, true);


Let me clear a little more. Suppose I add 300 individual products as a guest customer and at my site the anonymous checkout is disabled. So when I go to checkout then the application is redirected to the login page. Now when I am going to log in, this method is called and the migration is started from guest customer to registered customer. This is the scenario and by design, it will take that much time depending on the server configuration and added individual product amount(not quantity). So if I use this code it will take some milliseconds. But there is a problem, if the toCustomer(Register Customer) and fromCustomers(Guest One) have the same item(s) it will show both item(s) individually rather  sumup the quantity. So how I can solve that, actually there a lot of way. But the client requirment is to add uncommon product of register and guest customer. Suppose the guest customer have 5 in which 2 products already at the registered customer cart then the uncommon 3 items will be added to guest customer. If the registered customer(toCustomer) does not have any product so far I do not get any issue. Please correct me if I miss something.

Thanks a lot.
4 years ago
sina.islam wrote:
OK, I stand corrected; it looks like there is a "built in service/method for migrating items from one customer cart to another".   But from where it's used in the Login, I suspect it is just to migrate any items in the Guest cart (before login) to the registered user (after login).  So, I don't see how that should impact your login time if the registered user has the 300 items in cart.

\Presentation\Nop.Web\Controllers\CustomerController.cs
public virtual IActionResult Login(LoginModel model, string returnUrl, bool captchaValid)
... _shoppingCartService.MigrateShoppingCart(_workContext.CurrentCustomer, customer, true);


Let me clear a little more. Suppose I add 300 individual products as a guest customer and at my site the anonymous checkout is disabled. So when I go to checkout then the application is redirected to the login page. Now when I am going to log in, this method is called and the migration is started from guest customer to registered customer. This is the scenario and by design, it will take that much time depending on the server configuration and added individual product amount(not quantity). So if I use this code it will take some milliseconds. But there is a problem, if the toCustomer(Register Customer) and fromCustomers(Guest One) have the same item(s) it will show both item(s) individually rather  sumup the quantity. So how I can solve that, actually there a lot of way. But the client requirment is to add uncommon product of register and guest customer. Suppose the guest customer have 5 in which 2 products already at the registered customer cart then the uncommon 3 items will be added to guest customer. If the registered customer(toCustomer) does not have any product so far I do not get any issue. Please correct me if I miss something.

Thanks a lot.


Typing mistake

"Suppose the guest customer has 5 in which 2 products already at the registered customer cart then the uncommon 3 items will be added to the guest customer."

It will be the registered customer.


"Suppose the guest customer has 5 in which 2 products already at the registered customer cart then the uncommon 3 items will be added to the registered customer."
4 years ago
Ok, I think I understand a little better.  It sounds like this will occur often and you are wanting to override the MigrateShoppingCart method during login to use direct SQL queries instead.  

That is not something I have tried or tested, but you could first select any shopping cart items for the registered customer and prevent duplicates with something like "where guestCart.ProductId <> registeredCart.ProductId".  

Or if there are duplicate ProductIds you could use "merge into" to insert new products and update existing quantities.  

I would look through the AddToCart and DeleteShoppingCartItem methods in the ShoppingCartService to make sure I wasn't orphaning generic / checkout attributes, or anything else they handle.

Is it a possibility for your client to prevent guest shopping carts with the Access Control List?  If only registered customers can add to cart then this problem is avoided entirely.
4 years ago
af1racing wrote:
Ok, I think I understand a little better.  It sounds like this will occur often and you are wanting to override the MigrateShoppingCart method during login to use direct SQL queries instead.  

That is not something I have tried or tested, but you could first select any shopping cart items for the registered customer and prevent duplicates with something like "where guestCart.ProductId <> registeredCart.ProductId".  

Or if there are duplicate ProductIds you could use "merge into" to insert new products and update existing quantities.  

I would look through the AddToCart and DeleteShoppingCartItem methods in the ShoppingCartService to make sure I wasn't orphaning generic / checkout attributes, or anything else they handle.

Is it a possibility for your client to prevent guest shopping carts with the Access Control List?  If only registered customers can add to cart then this problem is avoided entirely.


+1 for reading the long post and control the add to cart by ACL. That is exactly I have done Not in. Thanks again.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.