SUGGESTION : Improvement on generating Guest Customer

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
5 лет назад
Caerels wrote:
Hello,

I'm using Web API's to perform some customized stuff in nopCommerce (asset mgmt. of sold goods). The Web API methods receive information from an Android/IoS app. The app has the possibility to create a new user (customer in nop) and therefore I need to do some upfront checking: is the email already in use, does the customer exist,...:

            if (!String.IsNullOrWhiteSpace(email))
            {
                var cust = _customerService.GetCustomerByEmail(email);
                if (cust != null)
                    return EEPC_BadRequest("Email is already registered");
            }

When executing this call, the 'Guest account' is created. I've tried to set the _workContext.CurrentCustomer to the build-in SearchEngine account like so:

            _workContext.CurrentCustomer = _customerService.GetCustomerBySystemName(SystemCustomerNames.SearchEngine);

However I guess that even the GetCustomerBySystemName() call will generate the Guest account.
So I try to set the _workContext.CurrentCustomer variables individually like so:

            _workContext.CurrentCustomer.Active = true;
            _workContext.CurrentCustomer.CustomerGuid = new Guid("0f806806-7a1d-4780-b5a6-320ca123c842");
            _workContext.CurrentCustomer.Id = 2;
            _workContext.CurrentCustomer.Email = "builtin@search_engine_record.com";
            _workContext.CurrentCustomer.IsSystemAccount = true;
            _workContext.CurrentCustomer.PasswordFormatId = 0;
            _workContext.CurrentCustomer.SystemName = SystemCustomerNames.SearchEngine;
            _workContext.CurrentCustomer.Username = null;
            _workContext.CurrentCustomer.VendorId = 0;

and make the call to GetCustomerByEmail(email), still the guest account gets created.

So simple question: How can I avoid the 'Guest' account gets created in my WebAPI method?

tx for the help!




Caerels take a look at this post https://www.nopcommerce.com/boards/t/62032/prevent-nopcommerce-from-creating-guest-customer-on-every-api-request.aspx
5 лет назад
vipul.dumaniya wrote:
Hello Guys,

I Know that b'coz of supporting Guest checkout we have to create new guest customer on every visit so guest customer can place order without registering.

I have one suggestion is instead of creating new guest customer on every new customer visit we can create one System customer like other system customer called like "Built In-GuestCustomer" etc and assign this user as _workContext.currentCustomer on every guest visit.

And when that customer add items in cart for shopping we just create new Guest Customer and set it as current customer and add cart item to current customer so they can checkout as guest as well.

so it will reduce the overhead of guest customer on customer table and may be we don't need schedule task for delete the guest customer as well.


Please suggest your opinion


Would you please send me a link where it is explained and how to implement it in code?  I am using nopC 4.1.
3 года назад
Wouldn't it make most sense, instead of creating permanent records in the database for every unregistered person who shops, to instead use cookies to persist a cart's contents on the client side? Then when the cookie expires, the cart expires, and their session leaves no remnant garbage in the database. The database methodology seems to be a relic of a time from before there were cookies. It creates a giant mess of clutter in the Customers table of the database. I'd assume most websites are using the cookie method. Isn't that why cookies were invented?
3 года назад
mindofsound wrote:
...It creates a giant mess of clutter in the Customers table of the database....

There is a Scheduled Task to "Delete guests".
3 года назад
This really is a mess. I haven't even launched the site yet and have 38496 customers on my test site which is firewalled from the outside world.
A bit of research has shown that it creates a new guest customer every 5 mins and this appears to be due to a keep alive ping from the load balancer.
The delete guests scheduled task runs but doesn't delete them but does manage to max out the SQL instance.
I'm at a loss as to why it's necessary to store a customer in the database until they need to checkout.
3 года назад
Maybe using an IP Blocker or browsecap.xml file could help?
3 года назад
This is my solution, I was able to limit the new guest account, each IP accessing the website will be created as 1 new guest account.
In this case, you can Disable the DeleteGuests task as it will no longer be needed.

Open the file at the path: Nopcommerce\Libraries\Nop.Services\Customers\CustomerService.cs
and add the following line of code:

public virtual Customer InsertGuestCustomer()
        {
            //KEVIN NGUYEN - get current IP address
            var currentIpAddress = EngineContext.Current.Resolve<IWebHelper>().GetCurrentIpAddress();
            if (CommonHelper.IsValidIpAddress(currentIpAddress))
            {
                var guest = _customerRepository.Table.Where(x => x.LastIpAddress == currentIpAddress
                                                    && (x.Username == null || x.Username == string.Empty)
                                                    && (x.Email == null || x.Email == string.Empty)
                                                    && !x.IsSystemAccount)?.FirstOrDefault();
                if (guest != null)
                    return guest;
            }

            var customer = new Customer
            {
                CustomerGuid = Guid.NewGuid(),
                Active = true,
                CreatedOnUtc = DateTime.UtcNow,
                LastActivityDateUtc = DateTime.UtcNow,
                LastIpAddress = currentIpAddress
            };

            //add to 'Guests' role
            var guestRole = GetCustomerRoleBySystemName(NopCustomerDefaults.GuestsRoleName);
            if (guestRole == null)
                throw new NopException("'Guests' role could not be loaded");

            _customerRepository.Insert(customer);

            AddCustomerRoleMapping(new CustomerCustomerRoleMapping { CustomerId = customer.Id, CustomerRoleId = guestRole.Id });

            return customer;
        }


3 года назад
New York wrote:
Maybe using an IP Blocker or browsecap.xml file could help?

I'll look into this.

I think I'd rather see guests and genuine customers split in the database though. I really don't like the huge gaps in the ID numbering.
3 года назад
nguyendinhchinh wrote:
This is my solution, I was able to limit the new guest account, each IP accessing the website will be created as 1 new guest account.
In this case, you can Disable the DeleteGuests task as it will no longer be needed.

Open the file at the path: Nopcommerce\Libraries\Nop.Services\Customers\CustomerService.cs
and add the following line of code:

public virtual Customer InsertGuestCustomer()
        {
            //KEVIN NGUYEN - get current IP address
            var currentIpAddress = EngineContext.Current.Resolve<IWebHelper>().GetCurrentIpAddress();
            if (CommonHelper.IsValidIpAddress(currentIpAddress))
            {
                var guest = _customerRepository.Table.Where(x => x.LastIpAddress == currentIpAddress
                                                    && (x.Username == null || x.Username == string.Empty)
                                                    && (x.Email == null || x.Email == string.Empty)
                                                    && !x.IsSystemAccount)?.FirstOrDefault();
                if (guest != null)
                    return guest;
            }

            var customer = new Customer
            {
                CustomerGuid = Guid.NewGuid(),
                Active = true,
                CreatedOnUtc = DateTime.UtcNow,
                LastActivityDateUtc = DateTime.UtcNow,
                LastIpAddress = currentIpAddress
            };

            //add to 'Guests' role
            var guestRole = GetCustomerRoleBySystemName(NopCustomerDefaults.GuestsRoleName);
            if (guestRole == null)
                throw new NopException("'Guests' role could not be loaded");

            _customerRepository.Insert(customer);

            AddCustomerRoleMapping(new CustomerCustomerRoleMapping { CustomerId = customer.Id, CustomerRoleId = guestRole.Id });

            return customer;
        }




Thanks for the suggestion. It does introduce a problem though... The customer's basket will be shared with anyone on the same network.
2 года назад
This ip solution and built-in guest solution works for me, because guest user cannot put products in cart, only for registered users. Thank you all.

Note: The numbers of guest users created per day are strange, but this solution helps and I don't have time to investigate.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.