How to display product Image in customer order place email notification in Nopcommerce 3.7 and above.

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

I want to know

Once if customer placed an order for product. How to display Ordered Product image in Order Placed Customer Notification Email Template in Nop 3.7 . I am very new to nopcommerce please do help as soon as possible.
7 years ago
Try to append the following changes in  Nop.Services.Messages->MessageTokenProvider.cs file

#region Fields
.....
private readonly IPictureService _pictureService;
private readonly MediaSettings _mediaSettings;

#endregion

#region Ctor

        public MessageTokenProvider(
....
            PictureService pictureService,
            MediaSettings mediaSettings)
        {
           ....
            this._pictureService = pictureService;
            this._mediaSettings = mediaSettings;
        }

#endregion

protected virtual string ProductListToHtmlTable(Order order, int languageId, int vendorId)
{    ...
        //picture col
         sb.AppendLine(string.Format("<th>{0}</th>", ""));
            sb.AppendLine(string.Format("<th>{0}</th>", _localizationService.GetResource("Messages.Order.Product(s).Name", languageId)));
...
  for (int i = 0; i <= table.Count - 1; i++)
  {
    var orderItem = table[i];
    var product = orderItem.Product;
    if (product == null)
      continue;

    if (vendorId > 0 && product.VendorId != vendorId)
      continue;

    sb.AppendLine(string.Format("<tr style=\"background-color: {0};text-align: center;\">", _templatesSettings.Color2));
    //product picture
    var picture = _pictureService.GetPicturesByProductId(product.Id, 1).FirstOrDefault();

    //let's check whether this product has some parent "grouped" product
    if (picture == null && !product.VisibleIndividually && product.ParentGroupedProductId > 0)
    {
      picture = _pictureService.GetPicturesByProductId(product.ParentGroupedProductId, 1).FirstOrDefault();
    }
    string imageUrl = _pictureService.GetPictureUrl(picture, _mediaSettings.CartThumbPictureSize);
    sb.AppendFormat("<td><img src=\"{0}\" alt=\"\" /></td>", imageUrl);

    //product name
    ....

  }
        
}
7 years ago
Hi

var picture = _pictureService.GetPicturesByProductId(product.Id, 1).FirstOrDefault();

in above line i got an excepion "Object reference not set to an instance of an object.";
how to fix this plz reply me as soon as possible
7 years ago
Thankyou shahdat45ict

Code worked fine...
5 years ago
Hi shahdat45ict,
Thanks for the implementation. I tried it on 3.7 Nop and its giving error.
Does the code require to change the catalog controller as well.
Thank you in Advance
5 years ago
Not sure if image is added.

System.InvalidOperationException
  HResult=0x80131509
  Message=An error occurred when trying to create a controller of type 'Nop.Web.Controllers.CatalogController'. Make sure that the controller has a parameterless public constructor.
  Source=System.Web.Mvc
  StackTrace:
   at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType)
   at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
   at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
   at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
   at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
   at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
   at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
   at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerAsyncWrapper.<>c__DisplayClass7.<BeginProcessRequest>b__6()
   at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerWrapper.Wrap[TResult](Func`1 func)
   at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerAsyncWrapper.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
   at System.Web.Mvc.Html.ChildActionExtensions.ActionHelper(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues, TextWriter textWriter)
   at System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues)
   at System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper, String actionName, String controllerName)
   at ASP._Page_Views_Home_Index_cshtml.Execute() in c:\DevProjects\Nop370\Presentation\Nop.Web\Views\Home\Index.cshtml:line 9
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.StartPage.RunPage()
   at System.Web.WebPages.StartPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
   at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)

Inner Exception 1:
DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Nop.Services.Messages.MessageTokenProvider' can be invoked with the available services and parameters:
Cannot resolve parameter 'Nop.Services.Media.PictureService pictureService' of constructor 'Void .ctor(Nop.Services.Localization.ILanguageService, Nop.Services.Localization.ILocalizationService, Nop.Services.Helpers.IDateTimeHelper, Nop.Services.Catalog.IPriceFormatter, Nop.Services.Directory.ICurrencyService, Nop.Core.IWorkContext, Nop.Services.Media.IDownloadService, Nop.Services.Orders.IOrderService, Nop.Services.Payments.IPaymentService, Nop.Services.Stores.IStoreService, Nop.Core.IStoreContext, Nop.Services.Catalog.IProductAttributeParser, Nop.Services.Common.IAddressAttributeFormatter, Nop.Core.Domain.Messages.MessageTemplatesSettings, Nop.Core.Domain.Catalog.CatalogSettings, Nop.Core.Domain.Tax.TaxSettings, Nop.Core.Domain.Directory.CurrencySettings, Nop.Core.Domain.Shipping.ShippingSettings, Nop.Services.Events.IEventPublisher, Nop.Services.Media.PictureService, Nop.Core.Domain.Media.MediaSettings)'.
3 years ago
Bliangta wrote:
Not sure if image is added.

System.InvalidOperationException
  HResult=0x80131509
  Message=An error occurred when trying to create a controller of type 'Nop.Web.Controllers.CatalogController'. Make sure that the controller has a parameterless public constructor.
  Source=System.Web.Mvc
  StackTrace:
   at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType)
   at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
   at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
   at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
   at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
   at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
   at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
   at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerAsyncWrapper.<>c__DisplayClass7.<BeginProcessRequest>b__6()
   at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerWrapper.Wrap[TResult](Func`1 func)
   at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerAsyncWrapper.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
   at System.Web.Mvc.Html.ChildActionExtensions.ActionHelper(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues, TextWriter textWriter)
   at System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues)
   at System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper, String actionName, String controllerName)
   at ASP._Page_Views_Home_Index_cshtml.Execute() in c:\DevProjects\Nop370\Presentation\Nop.Web\Views\Home\Index.cshtml:line 9
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.StartPage.RunPage()
   at System.Web.WebPages.StartPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
   at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)

Inner Exception 1:
DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Nop.Services.Messages.MessageTokenProvider' can be invoked with the available services and parameters:
Cannot resolve parameter 'Nop.Services.Media.PictureService pictureService' of constructor 'Void .ctor(Nop.Services.Localization.ILanguageService, Nop.Services.Localization.ILocalizationService, Nop.Services.Helpers.IDateTimeHelper, Nop.Services.Catalog.IPriceFormatter, Nop.Services.Directory.ICurrencyService, Nop.Core.IWorkContext, Nop.Services.Media.IDownloadService, Nop.Services.Orders.IOrderService, Nop.Services.Payments.IPaymentService, Nop.Services.Stores.IStoreService, Nop.Core.IStoreContext, Nop.Services.Catalog.IProductAttributeParser, Nop.Services.Common.IAddressAttributeFormatter, Nop.Core.Domain.Messages.MessageTemplatesSettings, Nop.Core.Domain.Catalog.CatalogSettings, Nop.Core.Domain.Tax.TaxSettings, Nop.Core.Domain.Directory.CurrencySettings, Nop.Core.Domain.Shipping.ShippingSettings, Nop.Services.Events.IEventPublisher, Nop.Services.Media.PictureService, Nop.Core.Domain.Media.MediaSettings)'.







It should be IPictureService instead of PictureService. You should use the interface instead of the actual class. ;)
2 years ago
update in 4.40

var picture = (await _pictureService.GetPicturesByProductIdAsync(product.Id, 1)).FirstOrDefault();
                if (picture == null && !product.VisibleIndividually && product.ParentGroupedProductId > 0)
                {
                    picture = (await _pictureService.GetPicturesByProductIdAsync(product.ParentGroupedProductId)).FirstOrDefault();
                }
                string fullSizeImageUrl, imageUrl;
                (imageUrl, picture) = await _pictureService.GetPictureUrlAsync(picture, _mediaSettings.CartThumbPictureSize);
                (fullSizeImageUrl, picture) = await _pictureService.GetPictureUrlAsync(picture);
                sb.AppendFormat("<td><img src=\"{0}\" alt=\"\" /></td>", imageUrl);
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.