Retrieve checkoutattribute value from Order

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
5 years ago
From my plugin on the admin side, I need to get a specific checkout attribute starting with an Order object. Thanks!
5 years ago
Order has 2 properties CheckoutAttributeDescription and CheckoutAttributesXml :)


5 years ago
Thanks. I saw those, but I don't know their structure, so I'm kind of at a loss about how to go about using those. I would LOVE a small snippet of code that starts with an Order object and returns the value of an attribute's name. Thanks in advance!
5 years ago
Inject ICheckoutAttributeParser in your controller. The implementation has a ParseCheckoutAttributes method that accepts attribute xml. You just need to pass order's attribute xml and you should be able to get relevant objects.

Hope that helps :)
Thanks,
Anshul
5 years ago
And if you want to extract values from description directly, something like this should work :)

public static string GetCheckoutAttributeValue(string name, Order order)
        {
            var attributes = order.CheckoutAttributeDescription;
            var attributeParts = Regex.Split(attributes, "<br />");
            var attribute = attributeParts.FirstOrDefault(x => x.StartsWith($"{name}: "));
            return attribute?.Substring($"{name}: ".Length) ?? string.Empty;
}


Regards,
Anshul
5 years ago
Thank you! I will try implementing one of those in a few days and will report back on how it went. Cheers.
5 years ago
Here's what it looks like to extract the value for a particular CheckoutAttribute from a given nopOrder object:

        


        private readonly Nop.Services.Orders.ICheckoutAttributeParser _checkoutAttributeService;

        public MyService(ICheckoutAttributeParser checkoutAttributeService)
        {
            _checkoutAttributeService = checkoutAttributeService;
        }

        ...

        var listOfCA = _checkoutAttributeService.ParseCheckoutAttributes(nopOrder.CheckoutAttributesXml);
            
        foreach (var ca in listOfCA)
        {
            if (ca.Name.Equals("MyAttributeName"))
            {
                var caVals = _checkoutAttributeService.ParseValues(nopOrder.CheckoutAttributesXml, ca.Id);

                // This is for a textbox attribute. Other attribute types could require more manipulation to extract the value(s)
                var caValue = caVals[0];
            }
        }
        
3 years ago
Hi
i have a problem
i created slot plugin for deliver
so i want to use the slot in my order
then for the delivery date(select box1) i wrote a function that get 7 days(from today to 7 after days) in the
and for delivery time (select box2) i wrote query check the date in my slot table
then  i need to save values(date,time) in the checkoutattributevalue table
but i dont know how  save in the data base cos i think i change the function in the checkoutattributevalue
3 years ago
@sara0612
Please don't duplicate posts.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.