Get product id in widget

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
8 years ago
Hello nopcommerce community,

I'm currently writing my first widget, and i'm having a little trouble with something.
I want to render a new button next to the buy button, in the productdetails_add_info zone.
However, I can't figure out how to get the product id in my controller class, PublicInfo.

[ChildActionOnly]
        public ActionResult PublicInfo(string widgetZone, object additionalData = null)
        {
            var product = _productService.GetProductById();
            var computerConfigureSettings = _settingService.LoadSetting<ComputerConfigureSettings>(_storeContext.CurrentStore.Id);

            var model = new PublicInfoModel();
            model.ProductId = product.Id;

            return View("~/Plugins/Widgets.ComputerConfigure/Views/WidgetsComputerConfigure/PublicInfo.cshtml", model);
        }


Any help is appreciated :)
8 years ago
Here is a option

 var routeData = ((System.Web.UI.Page)this.HttpContext.CurrentHandler).RouteData;
string productId = routeData.Values["productId"].ToString();
8 years ago
Did you check additionalData param?
8 years ago
You are using productdetails_add_info zone . They already pass productid with the zone.
@Html.Widget("productdetails_add_info", Model.ProductId)

You need to get prouctid from additionaldata just like that
 ProductId = (int)additionalData
7 years ago
how to get VendorId from route just like http://localhost:15536/Store/Your%20Store. I need yourstore vendor Id.
6 years ago
anik1991 wrote:
You are using productdetails_add_info zone . They already pass productid with the zone.
@Html.Widget("productdetails_add_info", Model.ProductId)

You need to get prouctid from additionaldata just like that
 ProductId = (int)additionalData


thanks, and how to get the product by Id? (I'm very new)
5 years ago
Hi there,

I'm interested in this as well.  I used this suggestion in my plugin to add a widget to the product details page but I am getting an error about not being able to convert an object to an int. I can't get past this point. Any suggestions?

public IViewComponentResult Invoke(string widgetZone, object additionalData)
        {
            var model = new PublicInfoModel { };

            if (additionalData != null)
            {
              
                model.ProductId = (int)additionalData;
                    
                   // do something here...
             }
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.