Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Kyle Eck 130 posts 280 karma points
    Dec 16, 2021 @ 16:21
    Kyle Eck
    0

    CMS Import to convert a CSV value to Vendr Price

    Richard,

    I am tryign to use a custom IFieldProvider class to convert a csv value to a Vendr Price.

    Speaking with Matt it looks like the object structure should be like this:

    {"A GUID HERE", "A PRICE HERE"}
    

    However, when I run my import it does not fire my custom controller Parse method at all, am i missing something here?

    If I get this working, my plan is to take a stab at the complex variants for Vendr as well, but I need to take baby steps here.

    Here is my current class I am working with:

    public class VendrCurrencyFieldProvider : IFieldProvider
    {
        private readonly IContentService _contentService;
        public VendrCurrencyFieldProvider(IContentService contentService)
        {
            _contentService = contentService;
        }
    
        public object Parse(object value, ImportPropertyInfo property, FieldProviderOptions fieldProviderOptions)
        {
            var price = Decimal.Parse(value.AsString());
            var data = new Dictionary<string, decimal> {
                { new Guid().ToString(), price }
        };
            return JsonConvert.SerializeObject(data);
        }
    }
    

    It just takes in the current value and grabs the price and I create the data structure and serialize it before sending it back.

    Thanks

  • Kyle Eck 130 posts 280 karma points
    Dec 16, 2021 @ 17:17
    Kyle Eck
    100

    Richard,

    I solved this and will be moving onto the Vendr Complex Variant import etc.

    Here is my solution from a CSV file data source that originated from Wordpress/WooCommerce. This may be of use to you in the future, feel free to use as you see fit.

    //need to have the editor alias from vendr to tell CMS import to only run this when we are importing into the Vendr Price field
    [FieldProvider(PropertyEditorAlias = "Vendr.Price")]
    public class VendrCurrencyFieldProvider : IFieldProvider
    {
        private readonly IContentService _contentService;
        public VendrCurrencyFieldProvider(IContentService contentService)
        {
            _contentService = contentService;
        }
    
        public object Parse(object value, ImportPropertyInfo property, FieldProviderOptions fieldProviderOptions)
        {
    
            //create new decimal to hold value
            var price = new Decimal();
    
            //if value is blank in your source file (I used CSV) then compensate for that and place a value you
            //would know into the price field so you can either manually change it or account for it in some way
            if(value != null && value.AsString() != "")
            {
                price = Decimal.Parse(value.AsString());
            }
            else
            {
                price = Decimal.Parse("1234.56");
            }
    
            //create the data structure you need to send the data in
            //in the case of Vendr.Price you need { currencyId guid (found in back office) , price value from source file }
            var data = new Dictionary<string, decimal> {
                { "05c1c00a-fd2d-41f9-b5af-017d7c739f60", price }
            };
    
            //serialize the data and return the value - done
            return JsonConvert.SerializeObject(data);
        }
    }
    
  • Richard Soeteman 4045 posts 12898 karma points MVP 2x
    Dec 17, 2021 @ 09:00
    Richard Soeteman
    0

    Great it's solved Kyle and thanks that we can use this... I was about to reply on your original question that you were missing the Fieldprovider attribute

    [FieldProvider(PropertyEditorAlias = "Vendr.Price")]
    

    Best,

    Richard

Please Sign in or register to post replies

Write your reply to:

Draft