Product Specification Attributes on ProductTemplate.Grouped.cshtml

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
8 years ago
Hello,

I am trying to display the product specification attributes for the simple products (associated products) on the grouped template (v 3.6). I know it is very simple to adjust the source in ProductController.cs to load the specifications all the time, but I do not want to edit the nop source and am opting to do it in the view. I am new to nop and razor (from a forms background) so I am struggling on tis. I have used several forum posts to expose the specificationAttributeService on the page. I am able to load the attributes with this:
var ProductSpecifications = specificationAttributeService.GetProductSpecificationAttributes(variant.Id);

And I can display the attribute names for each simple product with this:
@foreach (var spec in ProductSpecifications)
{
    @spec.SpecificationAttributeOption.SpecificationAttribute.Name
    <br />
}

What is eluding me is how to get the value of the attribute for the product. I cannot seem to figure out where it is. note that some of the attributes are custom values.

Thank you in advance for any help on this.

David
8 years ago
It seems I answered my own question. Perhaps this Razor in view solution will come in handy to some...

Here is how I did it. in ProductTemplate.Grouped.cshtml add:
@using Nop.Services.Catalog;


just before <!--product breadcrumb--> I added:
    public IList<ProductSpecificationModel> GetProductSpecifications(int productId)
    {
        var specificationAttributeService = EngineContext.Current.Resolve<ISpecificationAttributeService>();
        return specificationAttributeService.GetProductSpecificationAttributes(productId, 0, null, true)
                        .Select(psa =>
                        {
                            var m = new ProductSpecificationModel
                            {
                                SpecificationAttributeId = psa.SpecificationAttributeOption.SpecificationAttributeId,
                                SpecificationAttributeName = psa.SpecificationAttributeOption.SpecificationAttribute.GetLocalized(x => x.Name),
                            };

                            switch (psa.AttributeType)
                            {
                                case Nop.Core.Domain.Catalog.SpecificationAttributeType.Option:
                                    m.ValueRaw = HttpUtility.HtmlEncode(psa.SpecificationAttributeOption.GetLocalized(x => x.Name));
                                    break;
                                case Nop.Core.Domain.Catalog.SpecificationAttributeType.CustomText:
                                    m.ValueRaw = HttpUtility.HtmlEncode(psa.CustomValue);
                                    break;
                                case Nop.Core.Domain.Catalog.SpecificationAttributeType.CustomHtmlText:
                                    m.ValueRaw = psa.CustomValue;
                                    break;
                                case Nop.Core.Domain.Catalog.SpecificationAttributeType.Hyperlink:
                                    m.ValueRaw = string.Format("<a href='{0}' target='_blank'>{0}</a>", psa.CustomValue);
                                    break;
                                default:
                                    break;
                            }
                            return m;
                        }).ToList();
    }
}

In the associated product loop I added:
var specs = GetProductSpecifications(variant.Id);


Then I loop through specs to display the attributes and values. Yu can use this to do a general display and adjust the code as needed for the display you like:
@foreach (var spec in specs)
{
   @Html.Raw(String.Format("{0} : {1}<br />", @spec.SpecificationAttributeName,@spec.ValueRaw));
}
5 years ago
Here's what I did with asp.net core version. I didn't see an method for GetLocalized() anymore, maybe it's handled automatically?
nopCommerce version 4.1


path: \Presentation\Nop.Web\Themes\Burkhart\Views\Product\ProductTemplate.Grouped.cshtml

<!-- product specifications -->
@functions {

    /// <summary>
    /// Prepare the product specification models
    /// see: https://www.nopcommerce.com/boards/t/37719/product-specification-attributes-on-producttemplategroupedcshtml.aspx
    /// </summary>
    /// <param name="productId">Product ID</param>
    /// <returns>List of product specification model</returns>
    public virtual IList<ProductSpecificationModel> GetProductSpecificationModel(int productId)
    {
        if (productId <= 0)
            throw new ArgumentNullException(nameof(productId));

        var _specificationAttributeService = EngineContext.Current.Resolve<ISpecificationAttributeService>();

        return _specificationAttributeService.GetProductSpecificationAttributes(productId, 0, null, true)
                .Select(psa =>
                {
                    var m = new ProductSpecificationModel
                    {
                        SpecificationAttributeId = psa.SpecificationAttributeOption.SpecificationAttributeId,
                        SpecificationAttributeName = psa.SpecificationAttributeOption.SpecificationAttribute.Name,
                        ColorSquaresRgb = psa.SpecificationAttributeOption.ColorSquaresRgb,
                        AttributeTypeId = psa.AttributeTypeId
                    };

                    switch (psa.AttributeType)
                    {
                        case SpecificationAttributeType.Option:
                            m.ValueRaw = WebUtility.HtmlEncode(psa.SpecificationAttributeOption.SpecificationAttribute.Name);
                            break;
                        case SpecificationAttributeType.CustomText:
                            m.ValueRaw = WebUtility.HtmlEncode(psa.CustomValue);
                            break;
                        case SpecificationAttributeType.CustomHtmlText:
                            m.ValueRaw = psa.CustomValue;
                            break;
                        case SpecificationAttributeType.Hyperlink:
                            m.ValueRaw = $"<a href='{psa.CustomValue}' target='_blank'>{psa.CustomValue}</a>";
                            break;
                        default:
                            break;
                    }
                    return m;
                }).ToList();

    }

}
<!--product breadcrumb-->



// add variant specification just above gift card

<!-- specifications -->
                                    @{
                                        variant.ProductSpecifications = GetProductSpecificationModel(variant.Id);
                                        @await Html.PartialAsync("_ProductSpecifications", variant.ProductSpecifications)
                                    }
                                    <!--gift card-->
                                    
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.