November Happy Hour will be moved to Thursday December 5th.

Cart Meta fields problem in commerce 12.4

Vote:
 

Hi Team,

Currently, we are using commerce 10.2.1.To set cart meta fields like below

public void SetShipmentAddressId(int value)
{
var cart = CartHelper.Cart;

cart.SetCustomField(MetaFieldNames.xx, value);
cart.SetCustomField(MetaFieldNames.yyy, false);
SetShipmentProcessed(false);
cart.AcceptChanges();
}

For set custom field function

  public static class OrderStorageBaseExtention
    {
        public static void SetCustomField(this OrderStorageBase obj, string fieldName, T value)
        {
            try
            {
                obj.SetMetaField(fieldName, value);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static T GetCustomField(this OrderStorageBase obj, string fieldName)
        {
            try
            {
                var value = obj[fieldName];
                return (value is T) ? (T)value : default(T);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}

But commerce 12.4 how can I achieve this.

Advance thanks,

Regards,

Karthik

 

#195107
Jul 13, 2018 12:05
Vote:
 

It is still working the same. If you are using ICart, you can access the metafields via Properties property

#195108
Jul 13, 2018 12:09
Vote:
 

Thank you Quan,

We already using some common extension method like below.How could replace this with cart.properties[name]=value;

using System;
using Mediachase.Commerce.Orders;

namespace xxxx.Common.Infrastructure.Extensions
{
    public static class OrderStorageBaseExtention
    {
        public static void SetCustomField<T>(this OrderStorageBase obj, string fieldName, T value)
        {
            try
            {
                obj.SetMetaField(fieldName, value);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static T GetCustomField<T>(this OrderStorageBase obj, string fieldName)
        {
            try
            {
                var value = obj[fieldName];
                return (value is T) ? (T)value : default(T);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}

Regards,

Karthik

#195244
Edited, Jul 18, 2018 15:33
Vote:
 
public static class IExtendedPropertiesExtensions
    {
        public static string GetString(this IExtendedProperties extendedProperties, string fieldName)
        {
            return DefaultIfNull(extendedProperties.Properties[fieldName], string.Empty);
        }

        public static bool GetBool(this IExtendedProperties extendedProperties, string fieldName)
        {
            return DefaultIfNull(extendedProperties.Properties[fieldName], false);
        }

        public static Guid GetGuid(this IExtendedProperties extendedProperties, string fieldName)
        {
            return DefaultIfNull(extendedProperties.Properties[fieldName], Guid.Empty);
        }

        public static int GetInt32(this IExtendedProperties extendedProperties, string fieldName)
        {
            return DefaultIfNull(extendedProperties.Properties[fieldName], default(int));
        }

        public static DateTime GetDateTime(this IExtendedProperties extendedProperties, string fieldName)
        {
            return DefaultIfNull(extendedProperties.Properties[fieldName], DateTime.MaxValue);
        }

        public static decimal GetDecimal(this IExtendedProperties extendedProperties, string fieldName)
        {
            return DefaultIfNull(extendedProperties.Properties[fieldName], default(decimal));
        }
        
        private static T DefaultIfNull<T>(object val, T defaultValue)
        {
            return val == null || val == DBNull.Value ? defaultValue : (T)val;
        }
    }

For set I would just use

cart.Properties["Name"] = value;
#195250
Edited, Jul 18, 2018 16:39
Vote:
 

Thank You Mark.

#195254
Jul 18, 2018 17:25
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.