How to get Curren customerid?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
7 years ago
Hello all,

From checkout page I redirected to Chargify subscription URL and after subscription I redirected to my site with Chargify ID and Chargify Subscription ID. Then I want to insert those value in a custom table (in NOP Database) with current userid. For that I used below code -

Customer_Id = _workContext.CurrentCustomer.Id;

I checked from the database that I am not getting proper custome id.
Example:

When I register in NOP site, customer id is 504.
Then after Login I redirected to chargify and when I came back from chargify, in our thanks controller I wrote that above line to get the customerid, but it's showing 506. In database it's a guest userid, all fields have null value.

Now my question is if I redirected to some other site then how can I get the current loggedin userid or user details when I come back?

If it's not possible then can I pass my current customerid to chargify and get back that customerid as parameter just like Chargify ID and Chargify Subscription ID? Can I use Chargify custom field value to store my NOP Customer id in Chargify?

Thanks
7 years ago
First,if you rediredted to others url you can get the customer id first and as a parameter,when return the nop website through the request["customerid"] to get the custoemrid.

If you do not like this,try something else. In you question description, i sure that when you return the nop website ,you must logout before,try to find where.
When you visit the nop website or logout the register ,nop will register a new customer and give a new customerid.
See below code:

public virtual Customer CurrentCustomer
        {
            get
            {
                if (_cachedCustomer != null)
                    return _cachedCustomer;

                Customer customer = null;
                if (_httpContext == null || _httpContext is FakeHttpContext)
                {
                    //check whether request is made by a background task
                    //in this case return built-in customer record for background task
                    customer = _customerService.GetCustomerBySystemName(SystemCustomerNames.BackgroundTask);
                }

                //check whether request is made by a search engine
                //in this case return built-in customer record for search engines
                //or comment the following two lines of code in order to disable this functionality
                if (customer == null || customer.Deleted || !customer.Active)
                {
                    if (_userAgentHelper.IsSearchEngine())
                        customer = _customerService.GetCustomerBySystemName(SystemCustomerNames.SearchEngine);
                }

                //registered user
                if (customer == null || customer.Deleted || !customer.Active)
                {
                    customer = _authenticationService.GetAuthenticatedCustomer();
                }

                //impersonate user if required (currently used for 'phone order' support)
                if (customer != null && !customer.Deleted && customer.Active)
                {
                    var impersonatedCustomerId = customer.GetAttribute<int?>(SystemCustomerAttributeNames.ImpersonatedCustomerId);
                    if (impersonatedCustomerId.HasValue && impersonatedCustomerId.Value > 0)
                    {
                        var impersonatedCustomer = _customerService.GetCustomerById(impersonatedCustomerId.Value);
                        if (impersonatedCustomer != null && !impersonatedCustomer.Deleted && impersonatedCustomer.Active)
                        {
                            //set impersonated customer
                            _originalCustomerIfImpersonated = customer;
                            customer = impersonatedCustomer;
                        }
                    }
                }

                //load guest customer
                if (customer == null || customer.Deleted || !customer.Active)
                {
                    var customerCookie = GetCustomerCookie();
                    if (customerCookie != null && !String.IsNullOrEmpty(customerCookie.Value))
                    {
                        Guid customerGuid;
                        if (Guid.TryParse(customerCookie.Value, out customerGuid))
                        {
                            var customerByCookie = _customerService.GetCustomerByGuid(customerGuid);
                            if (customerByCookie != null &&
                                //this customer (from cookie) should not be registered
                                !customerByCookie.IsRegistered())
                                customer = customerByCookie;
                        }
                    }
                }

                //create guest if not exists
                if (customer == null || customer.Deleted || !customer.Active)
                {
                    customer = _customerService.InsertGuestCustomer();
                }


                //validation
                if (!customer.Deleted && customer.Active)
                {
                    SetCustomerCookie(customer.CustomerGuid);
                    _cachedCustomer = customer;
                }

                return _cachedCustomer;
            }
            set
            {
                SetCustomerCookie(value.CustomerGuid);
                _cachedCustomer = value;
            }
        }


Sorry my english does not well.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.