How can I display CreatedOnUtc in Product Box? How?

3 weeks ago
4.60.6
I want to show product creation date and time in product box.
How can I do it?

thanks.
3 weeks ago
Hi
I think this should work
@inject Nop.Services.Catalog.IProductService productService;
@inject Nop.Services.Helpers.IDateTimeHelper dateTimeHelper;
@{
    var productCreationDate = (await productService.GetProductByIdAsync(Model.Id)).CreatedOnUtc;
    var createdOnUserTime = await dateTimeHelper.ConvertToUserTimeAsync(productCreationDate, DateTimeKind.Utc);
}

<div class="product-created-time">@createdOnUserTime</div>

3 weeks ago
sanju.dahal741 wrote:
Hi
I think this should work
@inject Nop.Services.Catalog.IProductService productService;
@inject Nop.Services.Helpers.IDateTimeHelper dateTimeHelper;
@{
    var productCreationDate = (await productService.GetProductByIdAsync(Model.Id)).CreatedOnUtc;
    var createdOnUserTime = await dateTimeHelper.ConvertToUserTimeAsync(productCreationDate, DateTimeKind.Utc);
}

<div class="product-created-time">@createdOnUserTime</div>



That worked very well. Thank you so much.
One more question... This time is always server time?
3 weeks ago
The date is saved in the database as UTC. The code below converts the UTC saved in the database to the user's local time based on the timezone used/set by the customer.

var createdOnUserTime = await dateTimeHelper.ConvertToUserTimeAsync(productCreationDate, DateTimeKind.Utc);
2 weeks ago
sanju.dahal741 wrote:
Hi
I think this should work
@inject Nop.Services.Catalog.IProductService productService;
@inject Nop.Services.Helpers.IDateTimeHelper dateTimeHelper;
@{
    var productCreationDate = (await productService.GetProductByIdAsync(Model.Id)).CreatedOnUtc;
    var createdOnUserTime = await dateTimeHelper.ConvertToUserTimeAsync(productCreationDate, DateTimeKind.Utc);
}

<div class="product-created-time">@createdOnUserTime</div>


I apologize, but your answer is not good. If your home page has 5,000 products, it will take longer for the page to load.

danielaguero DM me for same 😊
2 weeks ago
Pedross wrote:

I apologize, but your answer is not good. If your home page has 5,000 products, it will take longer for the page to load.

danielaguero DM me for same 😊


You are probably right, it is better to bind the created time to the model at the ProductModelFactory but this will require code customization at Nop.Web or you need to build a plugin to override the product overview preparation.

The other things I want to add are
1. The method used GetProductByIdAsync(Id) from the ProductService  caches the product entity (in fact all the GetById method caches the entity) in the memory so the product will be served from the cache and loaded only once from the db until the cache expires

2. If you have 5000 products on the homepage then you have a whole lot of problems that you may want to look at while operating an e-commerce app than the performance issue caused by the GetProductByIdAsync method


  
2 weeks ago
sanju.dahal741 wrote:

I apologize, but your answer is not good. If your home page has 5,000 products, it will take longer for the page to load.

danielaguero DM me for same 😊

You are probably right, it is better to bind the created time to the model at the ProductModelFactory but this will require code customization at Nop.Web or you need to build a plugin to override the product overview preparation.

The other things I want to add are
1. The method used GetProductByIdAsync(Id) from the ProductService  caches the product entity (in fact all the GetById method caches the entity) in the memory so the product will be served from the cache and loaded only once from the db until the cache expires

2. If you have 5000 products on the homepage then you have a whole lot of problems that you may want to look at while operating an e-commerce app than the performance issue caused by the GetProductByIdAsync method

  

I'm not sure whether you're new to this platform, however it's possible to present product information via a widget utilizing a component on a product detail page without customization.

nopcommerce is cached and saved for a subsequent request.
2 weeks ago
I completely agree that a widget plugin is best for adding additional information to the pages. It is easier to maintain and upgrade.

Pedross wrote:

If your home page has 5,000 products, it will take longer for the page to load.


I don't see how a widget plugin makes the loading of a page faster than directly injecting the services at views (I agree this is not a good approach to modify by injecting the services at views but still it does the job ).

The ProductOverviewModel available at the _ProductBox.cshtml doesn't have the CreatedDateTime property. If you build a widget plugin then the component still has to use the productService to fetch the product from the database to get the createdTimeUtc (additional SQL request if it has not been cached)  and format the dateTime to the user's local date time. This means both the widget plugin and the service injection at the View file are performing the same action which means there won't be any performance gain at all. In fact using a widget plugin might slow (negligible) the application when it starts  because  of the plugin

If you are that concerned with the SQL request made by GetProductById here and want to avoid that request then you could override the PrepareProductOverviewModelsAsync method via a plugin. It has IEnumerable<Product> products as its parameter which means that the createdonUtc is available. You could format the date to the user's local date and assign the value to the CustomProperties property (avoiding the getProductById's SQL request). You will also have to override the view and use the CustomProperties value.

To conclude, the performance impact will be negligible to any user with either of these 3 steps i.e. injecting the service into the views, using the widget plugin, or using the override method. The widget plugin is the best approach while adding the newer information on the views but this won't help with the performance or the speed of the page load as you have implied