How to get parent products from a Variation.

Vote:
 

Hello.

In my Catalog I have a Product with two Variations, and in my code I would like to get the Product for those Variations, so my question is:

Which is the best way to get the Parent Product for a Variation programmatically?.

I'm a little bit stuck with this and I don't know hot to achieve this.

Best reagrds.

#190149
Apr 03, 2018 17:14
Vote:
 

You can use the extension variationContent.GetParentProducts, which returns a list of Products for that variation 

#190150
Apr 03, 2018 17:31
Vote:
 

Hi everyone.

thank you Quan Mai for your help:

Finally, using your suggestion and researching a little bit more I have solved this by this way:

    [CatalogContentType(GUID = "a1b78c39-0549-48bf-adfe-e5a4968c2816", MetaClassName = "Base_Variation")]
    public class BaseVariation : VariationContent
    {
        private IContentLoader contentLoader = null;

        public BaseVariation()
        {
            this.contentLoader = ServiceLocator.Current.GetInstance<EPiServer.IContentLoader>();
        }

        [CultureSpecific]
        [Display(
            Name = "Title",
            Description = "This is the Default Variation Title.",
            GroupName = SystemTabNames.Content,
            Order = 1)]
        public virtual string Title { get; set; }


        #region Publich Methods

        public BaseProduct GetBaseParentProduct()
        {
            BaseProduct returnValue = null;

            try
            {
                ContentReference productContentReference = this.GetParentProducts().FirstOrDefault();
                returnValue = contentLoader.Get<BaseProduct>(productContentReference);
            }
            catch
            {
                returnValue = null;
            }

            return returnValue;
        }

        public List<BaseProduct> GetBaseParentProducts()
        {
            List<BaseProduct> returnValue = new List<BaseProduct>();

            try
            {
                IEnumerable<ContentReference> productsContentReference = this.GetParentProducts();

                foreach (ContentReference prodcr in productsContentReference)
                {
                    returnValue.Add(contentLoader.Get<BaseProduct>(prodcr));
                }
            }
            catch
            {
                returnValue = new List<BaseProduct>();
            }

            return returnValue;
        }

        #endregion Publich Methods
    }

With those two methods I can get what I was looking for:

        public BaseProduct GetBaseParentProduct()
        {
            BaseProduct returnValue = null;

            try
            {
                ContentReference productContentReference = this.GetParentProducts().FirstOrDefault();
                returnValue = contentLoader.Get<BaseProduct>(productContentReference);
            }
            catch
            {
                returnValue = null;
            }

            return returnValue;
        }

        public List<BaseProduct> GetBaseParentProducts()
        {
            List<BaseProduct> returnValue = new List<BaseProduct>();

            try
            {
                IEnumerable<ContentReference> productsContentReference = this.GetParentProducts();

                foreach (ContentReference prodcr in productsContentReference)
                {
                    returnValue.Add(contentLoader.Get<BaseProduct>(prodcr));
                }
            }
            catch
            {
                returnValue = new List<BaseProduct>();
            }

            return returnValue;
        }

I hope more than one is useful.

Thank you and best regards.

#190180
Apr 04, 2018 11:27
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.