Reference a custom field in a plugin?

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

I need to send a specific custom field I created with "Configuration > Settings > Customer settings". This field is a simple text field, and I need my Payment Plugin to send this to the Payment Gateway.

Custom Field Name:   Prepaid Meter Number


This is what I have in my Plugin to send field data to the Payment Gateway:

------------------------------------------------------------------------------------------>>>

public void PostProcessPayment(PostProcessPaymentRequest postProcessPaymentRequest)
        {
            var storeLocation = _webHelper.GetStoreLocation();

            var post = new RemotePost
            {
                FormName = "SagePay",
                Method = "POST",
                Url = string.Format("{0}/eng/process?", _sagePayPaymentSettings.UseSandbox ? "https://paynow.sagepay.co.za/site/paynow.aspx" : "https://paynow.sagepay.co.za/site/paynow.aspx")
            };
            post.Add("m1", _sagePayPaymentSettings.SagePayServiceKey);
            post.Add("m2", _sagePayPaymentSettings.SagePaySoftwareVendorKey);
            post.Add("p2", postProcessPaymentRequest.Order.Id.ToString());
            post.Add("p3", string.Format("Order: #{0}", postProcessPaymentRequest.Order.Id));
            post.Add("p4", postProcessPaymentRequest.Order.OrderTotal.ToString("0.00", CultureInfo.InvariantCulture));
            post.Add("Budget", _sagePayPaymentSettings.SagePayShowBudget);

            if (postProcessPaymentRequest.Order.BillingAddress != null)
            {
                post.Add("m4", postProcessPaymentRequest.Order.BillingAddress.LastName + ", " + postProcessPaymentRequest.Order.BillingAddress.FirstName);
                post.Add("m5", postProcessPaymentRequest.Order.BillingAddress.PhoneNumber;
                post.Add("m6", postProcessPaymentRequest.Order.Customer.;
                post.Add("m9", postProcessPaymentRequest.Order.BillingAddress.Email);
            }

            post.Post();
        }

------------------------------------------------------------------------------------------>>>

The "post.Add("m6", postProcessPaymentRequest.Order.Customer.;" line is where I need to reference the custom field I created in the Customers Form.

Is this possible? I can't seems to pick up the CustomerAttribute property. Am going about this the wrong way?

Any help or guidance will be appreciated.

Thanks.
5 years ago
https://www.nopcommerce.com/boards/t/45236/customer-custom-attribute.aspx#179508
5 years ago
Also have a look at the routine in Admin - CustomerController
PrepareCustomerAttributeModel(CustomerModel model, Customer customer)

It reads the attributes to display in the admin page for a customer - you can use some of this code to find and get the value for the attribute you want.

You may or may not have a few attributes defined for a customer, if more than one you will need to search all the attributes returned for either the Id or the Name of the attribute you want, i.e. you have the name as you have defined it.
5 years ago
Yidna wrote:
Also have a look at the routine in Admin - CustomerController
PrepareCustomerAttributeModel(CustomerModel model, Customer customer)

It reads the attributes to display in the admin page for a customer - you can use some of this code to find and get the value for the attribute you want.

You may or may not have a few attributes defined for a customer, if more than one you will need to search all the attributes returned for either the Id or the Name of the attribute you want, i.e. you have the name as you have defined it.



Hi Yidna,

Thanks for the reply. Apologies for only now responding

I had a look at the code you mentioned, and I unsure which part of the code I should use. Do I have to use all of the code below, or just a portion of it. So basically there is only 1 custom attribute, which is a simple text box that will store a string value. I need to send this value to my Payment Gateway. Can you perhaps just clarify which code specifically I need to use to retrieve this value.

----------------------------------------------------------------
Admin > Customer Controller
----------------------------------------------------------------

        [NonAction]
        protected virtual void PrepareCustomerAttributeModel(CustomerModel model, Customer customer)
        {
            var customerAttributes = _customerAttributeService.GetAllCustomerAttributes();
            foreach (var attribute in customerAttributes)
            {
                var attributeModel = new CustomerModel.CustomerAttributeModel
                {
                    Id = attribute.Id,
                    Name = attribute.Name,
                    IsRequired = attribute.IsRequired,
                    AttributeControlType = attribute.AttributeControlType,
                };

                if (attribute.ShouldHaveValues())
                {
                    //values
                    var attributeValues = _customerAttributeService.GetCustomerAttributeValues(attribute.Id);
                    foreach (var attributeValue in attributeValues)
                    {
                        var attributeValueModel = new CustomerModel.CustomerAttributeValueModel
                        {
                            Id = attributeValue.Id,
                            Name = attributeValue.Name,
                            IsPreSelected = attributeValue.IsPreSelected
                        };
                        attributeModel.Values.Add(attributeValueModel);
                    }
                }


                //set already selected attributes
                if (customer != null)
                {
                    var selectedCustomerAttributes = customer.GetAttribute<string>(SystemCustomerAttributeNames.CustomCustomerAttributes, _genericAttributeService);
                    switch (attribute.AttributeControlType)
                    {
                        case AttributeControlType.DropdownList:
                        case AttributeControlType.RadioList:
                        case AttributeControlType.Checkboxes:
                        {
                            if (!String.IsNullOrEmpty(selectedCustomerAttributes))
                            {
                                //clear default selection
                                foreach (var item in attributeModel.Values)
                                    item.IsPreSelected = false;

                                //select new values
                                var selectedValues = _customerAttributeParser.ParseCustomerAttributeValues(selectedCustomerAttributes);
                                foreach (var attributeValue in selectedValues)
                                    foreach (var item in attributeModel.Values)
                                        if (attributeValue.Id == item.Id)
                                            item.IsPreSelected = true;
                            }
                        }
                            break;
                        case AttributeControlType.ReadonlyCheckboxes:
                        {
                            //do nothing
                            //values are already pre-set
                        }
                            break;
                        case AttributeControlType.TextBox:
                        case AttributeControlType.MultilineTextbox:
                        {
                            if (!String.IsNullOrEmpty(selectedCustomerAttributes))
                            {
                                var enteredText = _customerAttributeParser.ParseValues(selectedCustomerAttributes, attribute.Id);
                                if (enteredText.Any())
                                    attributeModel.DefaultValue = enteredText[0];
                            }
                        }
                            break;
                        case AttributeControlType.Datepicker:
                        case AttributeControlType.ColorSquares:
                        case AttributeControlType.ImageSquares:
                        case AttributeControlType.FileUpload:
                        default:
                            //not supported attribute control types
                            break;
                    }
                }

                model.CustomerAttributes.Add(attributeModel);
            }
        }

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