2

I am trying to pull the image from a specific product variant on my e-commerce component. There are two image components on the e-commerce, on the product itself and the within the product variants. I could only pull the image on the product itself and not on the product variant.

I tried configuring the model and view of the product variant built-in code of Sitecore commerce, but I couldn't see the definition since it only takes me onto the metadata.

 @if (Model.Variants != null && Model.Variants.Count() > 0)
    {
        foreach (var variantDefinition in Model.VariantDefinitions)
        {
            var valueList = 
     Model.GetDistinctVariantPropertyValues(variantDefinition.PropertyName);
            if (valueList.Any())
            {
                var labelName = "variant" + variantDefinition.PropertyName;
                var selectName = "Variant_" + variantDefinition.PropertyName;
                var tryname = "propertyValue_" + variantDefinition.DisplayName;

                hasVariants = true;

                <div class="product-variant-name @selectName @tryname chooseContainer">
                    @foreach (var propertyValue in valueList)
                    {<div class="columnContainer">
                            <div class="phoneColumn" tabindex=0>
                                <div class="innerContainer">
                                    <img src="@Model.Images[0].GetImageUrl(70, 70)">
                                </div>
                                <h2>
                                    @propertyValue
                                </h2>
                            </div>
                        </div>
                    }

                </div>
           }
     }

The @Model.Images[0].GetImageUrl(70, 70) line only pulls data from the product itself but not from the product variant.

2
  • can you add more context? you can also look into Sitecore.Commerce.XA.Feature.Catalog.Models.CatalogItemRenderingModel class, it has the SetImages method that reads image content from product, you can customize this class for your purpose, in the initialize method this class receives ProductEntity instance which contains all variants with reference to Sitecore items. Oct 29 '19 at 7:26
  • Hi, I tried editing the CatalogItemRenderingModel class, but it says that the file is taken from metadata and cannot be edited.
    – D.Trump123
    Oct 30 '19 at 7:54
1

You have to create you custom CatalogItemRenderingModel class based on Sitecore.Commerce.XA.Feature.Catalog.Models.CatalogItemRenderingModel, Sitecore.Commerce.XA.Feature.Catalog and override SetImages() method with your custom logic:

protected virtual void SetImages()
    {
      if (this.Images != null)
        return;
      this.Images = (ICollection<MediaItem>) new List<MediaItem>();
      MultilistField field = (MultilistField) this.CatalogItem.Fields["Images"];
      if (field != null)
      {
        foreach (ID targetId in field.TargetIDs)
          this.Images.Add((MediaItem) this.CatalogItem.Database.GetItem(targetId));
      }
      else
        this.Images.Add((MediaItem) this.CatalogItem.Database.GetItem(ID.Parse("{8B33A7DC-8680-46AC-A199-1419AF50C330}")));
    }

After that you need to patch Sitecore config App_Config\Include\Feature\Commerce\Sitecore.Commerce.XA.Feature.Catalog.config

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <commerce.XA>
      <models>

        <CatalogItemRenderingModel type="YourCustomClass, YourDllName"
           patch:instead="*[@type='Sitecore.Commerce.XA.Feature.Catalog.Models.CatalogItemRenderingModel, Sitecore.Commerce.XA.Feature.Catalog']"/>

      </models>
    </commerce.XA>
  </sitecore>
</configuration>
6
  • Hi, how are you able to configure the Sitecore.Commerce.XA.Feature.Catalog.Models.CatalogItemRenderingModel. Because it shows a "from metadata" whenever I look into its definition, and I'm unable to configure it.
    – D.Trump123
    Oct 30 '19 at 11:19
  • You need to decompile Sitecore.Commerce.XA.Feature.Catalog.dll (for example, dotPeek jetbrains.com/decompiler/download) and find decompiled Sitecore.Commerce.XA.Feature.Catalog.Models.CatalogItemRenderingModel class there. Create your own class and as a base class for your custom one use CatalogItemRenderingModel and override virtual class from the base class. Oct 30 '19 at 11:33
  • Hi Andrei, can you provide the actual location of the Sitecore.Commerce.XA.Feature.Catalog.dll you used. Because I saw more than 5 copies of that certain dll in different folders within my instance. Also, is the class SetImages being used in other components or I'm freely able to edit it without affecting other commerce components. Thank you.
    – D.Trump123
    Nov 6 '19 at 8:24
  • C:\inetpub\yourwebsiterootfolder\bin. It should also been used in all other components Nov 6 '19 at 8:27
  • Thank you for that, as per following your instructions. I am at the stage where I already decompiled the file. However, I'm still unable to edit CatalogItemRenderingModel file. I think I'm missing a certain step in order to configure it?
    – D.Trump123
    Nov 6 '19 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.