override view using widget

1 year ago
I created a new widget in noppcommerce(version 4.60).
the widget is created to make changes in the _ProductBox and _ProductsInGridOrLines.cshtml.

I want to override _ProductBox.cshtml ,_ProductsInGridOrLines.cshtml and put some extra code there.
so , How can I override a view through widget?
1 year ago
You can make a Custom View Engine and load the version of _ProductBox.cshtml from your pluign folder
See https://www.nopcommerce.com/en/boards/topic/96538/referencing-a-plugin-from-a-theme-in-nopcommerce-no-longer-working-in-460
1 year ago
Hi Yidna and thank you for your reply.
I tried ViewEngine.
But it not overriding the view that I added in the plugin.

viewLocations = new[] { $"~/Plugins/Widgets.widgetName/Views/FrontView/Shared/Themes/_ProductBox.cshtml" }.Concat(viewLocations);
1 year ago
febifathima9 wrote:
Hi Yidna and thank you for your reply.
I tried ViewEngine.
But it not overriding the view that I added in the plugin.

viewLocations = new[] { $"~/Plugins/Widgets.widgetName/Views/FrontView/Shared/Themes/_ProductBox.cshtml" }.Concat(viewLocations);


(I followed the same code that you share in the link)
1 year ago
As per @Yidna

This is correct way to overrider view files from plugin.

Please make sure your customer view engine registering into plugin startup file.

Please put debug into your custom view engine code.

You got better Idea while you debug.

If you share your code then might be we got better idea.
Thanks,
1 year ago
Its worked, Thank you
1 year ago
I would like to raise another question related to overriding:

I'm overriding the view _ProductAttributes.cshtml file to make changes in the click event of color_squares in ProductDetails page.

The JS code is below:

function @(attributeChangeFnName)() {

                $.ajax({
                    cache: false,                  
                    url : "@(Url.Action("action", "controllerName"))", //here the controller is in the related plugin folder
                    data: $('#product-details-form').serialize(),
                    type: "POST",
                    dataType: "html",
                    success: function (response) {
                                $(".related-products-grid").html(response);
                                alert(response);
                    },
                    failure: function (errMsg) {
                        alert("An Error Occured: " + errMsg);
                    }
                });
            };


Here the controller is located in the custom plugin folder,
But it didn't redirecting to the Controller.

Have any solution for this?
1 year ago
febifathima9 wrote:
url : "@(Url.Action("action", "controllerName"))",

Yes something lke
url: "@Html.Raw(Url.Action("CustomProductDetails_AttributeChange", "CustomShoppingCart",

Have you registered the controller  in Infrastructure\NopStartup.cs
            services.AddScoped<ShoppingCartController, CustomShoppingCartController>();

Also make sure you dont have Controller in the Name
i.e. should be "CustomShoppingCart" not "CustomShoppingCartController"
1 year ago
Hi Yidna,
I added to NopStartup.
But it shows the error:

The type 'Nop.Plugin.Widgets.WidgetName.Controllers.CustomShoppingCartController' cannot be used as type parameter 'TImplementation' in the generic type or method
'ServiceCollectionServiceExtensions.AddScoped<TService, TImplementation>(IServiceCollection)'.
There is no implicit reference conversion from Nop.Plugin.Widgets.WidgetName.Controllers.CustomShoppingCartController'
to 'Nop.Web.Controllers.ShoppingCartController'.
11 months ago
Got the solution

It is due by this line in the CustomShoppingCartController of my Widget
[AuthorizeAdmin]
    [Area(AreaNames.Admin)]
    [AutoValidateAntiforgeryToken]
So I removed it.

[AuthorizeAdmin]
    [Area(AreaNames.Admin)]
    [AutoValidateAntiforgeryToken]
    public class CustomShoppingCartController : BasePluginController
    {
      
    }

Because my widget is for the public site ,not for Admin area.