Check if it is Grouped Product in _ProductBox.cshtml?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
1 year ago
NC 3.90

How can I check in code if it is Grouped Product? I want to use it in _ProductBox.cshtml

I need the code example like this:
@if (Model.GroupedProduct) {
<div class="note">This is Grouped Product.</div>
}


Thanks.
1 year ago
Note that adding a property to the Model - e.g. Model.GroupedProduct, is going to require a change to the Model and Controller.    Are you able to modify core code?  (Alternately, if you only want to touch the .cshtml file,  it may be possible to use EngineContext.Current.Resolve to get the services you need to determine the product type)

However, Grouped products are not orderable directly.  Are you trying to determine if the ordered (simple) product is associated to a grouped product?
1 year ago
New York wrote:
Note that adding a property to the Model - e.g. Model.GroupedProduct, is going to require a change to the Model and Controller.    Are you able to modify core code?  (Alternately, if you only want to touch the .cshtml file,  it may be possible to use EngineContext.Current.Resolve to get the services you need to determine the product type)

However, Grouped products are not orderable directly.  Are you trying to determine if the ordered (simple) product is associated to a grouped product?


I try to explain what I want. I have simple products and I have grouped products.
For all products I check "Display stock quantity", so all products show exact amount of stock like this "154 IN STOCK".

I just want to hide numbers in stock availability for grouped product. So if it is grouped product, then just show "IN STOCK", not "154 IN STOCK". For Simple Product continue to show the Quantity like "154 IN STOCK".

Now it works perfect. Just want to hide number quanity for Grouped Product (in _ProductBox.cshtml)

My current code is this:
<!-- Availability -->
    @if (stockMessage.Equals(localizationService.GetResource("Products.Availability.OutOfStock")))
    {
      <div class="out-of-stock">@stockMessage</div>
    }
    else
    {
      <div class="in-stock">@stockMessage</div>
    }
1 year ago
zaf wrote:
However, Grouped products are not orderable directly.  Are you trying to determine if the ordered (simple) product is associated to a grouped product?


Can you please help me?
1 year ago
We need to know if you can modify core code and recompile (in Visual Studio), or are you only able to use text editor to modify .cshtml files.
1 year ago
New York wrote:
We need to know if you can modify core code and recompile (in Visual Studio), or are you only able to use text editor to modify .cshtml files.


I can only edit _ProductBox.cshtml code...
1 year ago
New York wrote:
We need to know if you can modify core code and recompile (in Visual Studio), or are you only able to use text editor to modify .cshtml files.


And above in _ProductBox.cshtml, I have this code:
var productService = EngineContext.Current.Resolve<IProductService>();
  var localizationService = EngineContext.Current.Resolve<ILocalizationService>();
  var productAttributeParser = EngineContext.Current.Resolve<IProductAttributeParser>();
  var dateRangeService = EngineContext.Current.Resolve<IDateRangeService>();

  var stockMessage = string.Empty;
  var currentProduct = productService.GetProductById(Model.Id);
  if(currentProduct.ProductType == ProductType.SimpleProduct)
  {
    stockMessage = currentProduct.FormatStockMessage("", localizationService,
      productAttributeParser, dateRangeService);
  }
  else
  {
    var associatedProducts = productService.GetAssociatedProducts(currentProduct.Id);
    foreach (var ap in associatedProducts)
    {
      stockMessage = ap.FormatStockMessage("", localizationService,
        productAttributeParser, dateRangeService);

      if (stockMessage.Equals(localizationService.GetResource("Products.Availability.InStock")))
      {
        break;
      }
    }
  }
}
1 year ago
In your code up the top put
bool groupedProduct = false
if (currentProduct.ProductType == ProductType.GroupedProduct)
    groupedProduct = true;

in the markup you can use

@if (groupedProduct ) {
    <div class="note">This is Grouped Product.</div>
}
1 year ago
Yidna wrote:
In your code up the top put
bool groupedProduct = false
if (currentProduct.ProductType == ProductType.GroupedProduct)
    groupedProduct = true;

in the markup you can use

@if (groupedProduct ) {
    <div class="note">This is Grouped Product.</div>
}


This code breaks my site...
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.