Close

Adding a categoryId to PrepareProductOverviewModel

[email protected]
9 years ago
#8073 Quote
Avatar
  • 12
So I made some functionality to assign an image to the Product-Category mapping so when you view a category, you can get specific pictures to show as the default. (I sort of cloned "PrepareProductOverviewModels" to take a categoryId so that it can choose which image it should show).  My problem is the ajax filters plugin obviously ignores the categoryId.  Without having the source, is there anything that I can do to get the plugin to include this functionality?  When I added the categoryId to "PrepareProductOverviewModels", I noticed it broke the plugin(because of signature I'm guessing), so is there any way to get your plugin to call my other version of "PrepareProductOverviewModels" with the categoryId?


This started as a PM, so here's Boyko's response...
Please refer to this post of how to properly modify the CatalogController without making any breaking changes, so that the Filters can work.
http://www.nop-templates.com/boards/topic/798/nop-ajax-filters-not-working-show-a-pop-up-box-with-message-loading-the-page-failed/page/3#4061


Thanks for the response Boyko, but I was able to make the change without breaking the plugin (I have two functions that both funnel to a third one). My question was more if there is any way to get the plugin to utilize my version with the categoryId.  Since I can't modify the source to call my version with the extra categoryId parameter, can something be done in the views? Maybe an ajax call that passes the productId and categoryId to swap out the images? My question with that is where can I even access the categoryId from?  Any ideas here? Thanks.
Boyko
9 years ago
#8074 Quote
Avatar
  • Moderator
  • 1570
It would be best to change/swap the picture when you prepare the model but you can also do this in the _ProductBox.cshtml. Simply call you function in the view and change the picture there.
Regards,
Nop-Templates.com Team
Boyko
9 years ago
#8075 Quote
Avatar
  • Moderator
  • 1570
Forgot to answer your question.

You can get the category id from the route data in the view like this:

Url.RequestContext.RouteData.Values["categoryId"]


But I am not sure if the category id will be available in the route data when the ajax requests are made.
You can give it a try and let me know.
Regards,
Nop-Templates.com Team
[email protected]
9 years ago
#8076 Quote
Avatar
  • 12
I agree that it would be best to do it when I prepare the model, is there anyway to do that without changing the plugin source? Just to clarify, everything works correctly when I go to a category, the correct picture for that category is chosen. It is only when I apply a filter, then it just returns the first product picture, because obviously the plugin doesn't know about my new functionality.

I was looking at _ProductBox to do an ajax call or something, but I won't have the categoryId in ProductOverviewModel if the model is prepared through the plugin. One thing I thought was before the call to  @Html.Partial("_ProductBox", product, maybe set product.CategoryId equal to the CategoryModel's id. Does this plugin use the normal CategoryTemplate.ProductsInGridOrLines view, or is there a separate view that I would have to change this in? Thanks for your help.

[email protected]
9 years ago
#8080 Quote
Avatar
  • 12
Boyko wrote:
Forgot to answer your question.

You can get the category id from the route data in the view like this:

Url.RequestContext.RouteData.Values["categoryId"]


But I am not sure if the category id will be available in the route data when the ajax requests are made.
You can give it a try and let me know.


Unfortunately, the categoryId is not available when the ajax requests are made.
Is there a category id in this model? SevenSpikes.Nop.Plugins.AjaxFilters.Models.ProductsModel
[email protected]
9 years ago
#8081 Quote
Avatar
  • 12
For other people, to get the categoryId after the ajax is called, I used this...

@{
var filterData = System.Web.Helpers.Json.Decode(Model.SpecificationFilterModel7SpikesJson);
product.CategoryId = filterData.CategoryId;
}

I added this block to the plugin's CategoryTemplate view, right before the call to the _ProductBox partial.  This was the only way I could find to get the categoryId after the ajax call is made. Then the _ProductBox partial calls a child action that takes the productId and categoryId and just returns the PictureModel.
Boyko
9 years ago
#8082 Quote
Avatar
  • Moderator
  • 1570
rossiter10 wrote:
For other people, to get the categoryId after the ajax is called, I used this...

@{
var filterData = System.Web.Helpers.Json.Decode(Model.SpecificationFilterModel7SpikesJson);
product.CategoryId = filterData.CategoryId;
}

I added this block to the plugin's CategoryTemplate view, right before the call to the _ProductBox partial.  This was the only way I could find to get the categoryId after the ajax call is made. Then the _ProductBox partial calls a child action that takes the productId and categoryId and just returns the PictureModel.


Hi rossiter10,

Very nice solution as the categoryId is not present in the ProductsModel.
Regards,
Nop-Templates.com Team