Custom Code for 'Book of the Day' from v2.4 to v2.6

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 years ago
Note: I am not a hardcore programmer so please excuse my ignorance...
I had written an ActionResult and a corrsponding view to display a book of the day in the right column in v2.4
What it simply does is that I just pass the product Id to bookOfTheDay in the All Setting in Admin panel and it would get that product and display it.

        [ChildActionOnly]
        public ActionResult BookOfTheDayBlock()
        {
            var model = new List<ProductModel>();
            var product = _productService.GetProductById(_catalogSettings.bookOfTheDay);
                model.Add(PrepareProductOverviewModel(product, true, true));

            return PartialView(model);
        }

@model IList<Nop.Web.Models.Catalog.ProductModel>
@using Nop.Core;
@using Nop.Core.Infrastructure;
@using Nop.Web.Framework.UI;
@using Nop.Web.Models.Catalog;
@if (Model.Count > 0)
{
    <div class="block block-recently-viewed-products">
        <div class="title">
            Book of the day
        </div>
        <div class="clear">
        </div>
        <div class="listbox">
                @for (int i = 0; i < Model.Count; i++)
                {
                    var product = Model[i];
                        <a href="@Url.RouteUrl("Product", new { productId = product.Id, SeName = product.SeName })">@product.Name</a>
                        <div class="poll-take-poll">
                        <div class="picture" style="text-align:center;">
                        <a href="@Url.RouteUrl("Product", new { productId = product.Id, SeName = product.SeName })" title="@product.DefaultPictureModel.Title">
                            <img style="border-width: 0px; float:none" alt="@product.DefaultPictureModel.AlternateText" src="@product.DefaultPictureModel.ImageUrl" title="@product.DefaultPictureModel.Title" />
                        </a>
                        </div>
                            @Html.Raw(product.ShortDescription)
                            <br />
                            <div class="title" style="background:none; text-align:center;">                            
                            Only for @Html.Raw(product.ProductPrice.Price)
                            </div>                            
                        </div>
                                                    
                }
        </div>
    </div>
}



When I ported the same code to v2.6 it did not find the method "PrepareProductOverviewModel" in the line

model.Add(PrepareProductOverviewModel(product, true, true));


Instead there is a function "PrepareProductOverviewModels" which I tried to use but it requires the "product" to be of the type Generic.IEnumerable which I dont know how to get. Any help would be appreciated.
Thank you.
P.S. Also if someone can also quickly go through the view and point out any programming or performance issues as I have copied and pasted code from multiple places. It works fine though there are no issues in it.
11 years ago
If you call it like this:

PrepareProductOverviewModels(new List<Product> { product }, true, true).Single();


you should be fine. It would waste some clocks compared to the 2.4 version, but nothing to be worrying about.


Regarding the overall performance/style, you are passing around lists when you clearly need only one item, so you should probably change the model on the view from

@model IList<Nop.Web.Models.Catalog.ProductModel>


to

@model Nop.Web.Models.Catalog.ProductModel


and the Action Method like so:


        [ChildActionOnly]
        public ActionResult BookOfTheDayBlock()
        {
            var model = ProductModel();
            var product = _productService.GetProductById(_catalogSettings.bookOfTheDay);
            model = PrepareProductOverviewModels(new List<Product> { product }, true, true).Single();
            return PartialView(model);
        }


And in the View, get rid of everything that treats the Model as a list (Count() checking, iterating over it...) and simply access the product model from Model, but that's totally optional and won't affect performance more than by a negligible factor.
11 years ago
Thank you very much M.Ang.

The snapshot is given below :-)

http://imageshack.us/photo/my-images/3/bookoftheday.jpg/

Although the product there is not a book, I just randomly selected a product ID...
11 years ago
I am also pasting here the code from the modified view as per your suggestions in case anyone else needs it. Thanks again.


@model Nop.Web.Models.Catalog.ProductOverviewModel
@using Nop.Core;
@using Nop.Core.Infrastructure;
@using Nop.Web.Framework.UI;
@using Nop.Web.Models.Catalog;
@if (!Model.Equals(null))
{
    <div class="block block-recently-viewed-products">
        <div class="title">
            Book of the day
        </div>
            <div class="clear">
            </div>
            <div class="listbox">
                <a href="@Url.RouteUrl("Product", new { productId = Model.Id, SeName = Model.SeName })">@Model.Name</a>
                <div class="poll-take-poll">
                    <div class="picture" style="text-align:center;">
                        <a href="@Url.RouteUrl("Product", new { productId = Model.Id, SeName = Model.SeName })" title="@Model.DefaultPictureModel.Title">
                        <img style="border-width: 0px; float:none" alt="@Model.DefaultPictureModel.AlternateText" src="@Model.DefaultPictureModel.ImageUrl" title="@Model.DefaultPictureModel.Title" />
                        </a>
                    </div>
                    @Html.Raw(Model.ShortDescription)
                    <br />
                    <div class="title" style="background:none; text-align:center;">                            
                        Only for @Html.Raw(Model.ProductPrice.Price)
                    </div>                            
                </div>                                                  
            </div>
    </div>
}
11 years ago
you're welcome, it was a pleasure :)
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.