add product.template from plugin

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
3 years ago
how to handle product template from plugin
3 years ago
up
3 years ago
There is a bit involved but very quickly

1. Copy ProductTemplate.Simple.cshtml to Plugin Views ProductTemplate.YourTemplate.cshtml
2. Create a template

var productTemplates = new List<ProductTemplate>
{
    new ProductTemplate
    {
        Name = "Your Template",
        ViewPath = "ProductTemplate.YourTemplate",
        DisplayOrder = 200,
        IgnoredProductTypes = ""
    }
};
_productTemplateRepository.Insert(productTemplates);

3. Create product that uses the template

4. Create a new route

routeBuilder.MapRoute("Nop.Group.Name.Front.YourTemplate", urlPattern,
    new { controller = "YourController", action = "YourTemplateFront" });

5. Add to action filter

var productTemplateViewPath = productModelFactory.PrepareProductTemplateViewPath(product);
if (productTemplateViewPath == "ProductTemplate.YourTemplate")
{
    filterContext.Result = new RedirectToRouteResult("Nop.Group.Name.Front.YourTemplate", new { SeName = seName });
}

6. Create Controller action in YourController

        public IActionResult YourTemplateFront(string seName)
        {
            if (string.IsNullOrEmpty(seName))
                throw new ArgumentNullException("No product found with the specified SeName");

            var urlRecord = _urlRecordService.GetBySlug(seName);
            if (urlRecord == null)
                return RedirectToRoute("HomePage");

            var product = _productService.GetProductById(urlRecord.EntityId);
            if (product == null)
                throw new ArgumentNullException("No product found with the specified id");
....
            var model = _productModelFactory.PrepareProductDetailsModel(product);

            var productTemplate = _productTemplateService.GetProductTemplateById(product.ProductTemplateId);

            if (productTemplate != null)
            {
                string url = string.Format("~/Plugins/Group.Name/Views/FrontView/{0}.cshtml", productTemplate.ViewPath);
                return View(url, model);
            }

            return View(null, model);
        }
3 years ago
thanks
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.