Developer forum

Forum » Ecommerce - Standard features » Parts List with Variants

Parts List with Variants

Cátia Torego
Reply

Hi,

I have a product that is a parts list and one of the products in the list has variants. Is there a way, when adding the parts list product to the cart, to select a specific variant instead of adding the master product?

I tried adding the following code, to the add to cart button, to select the variant but it still adds the master product:

var bomProduct = Dynamicweb.Ecommerce.Services.Products.GetProductById(product?.Id, product?.VariantId, Dynamicweb.Ecommerce.Common.Context.LanguageID);
if (bomProduct != null && bomProduct.Items.Any())
{
 foreach (var bomItem in bomProduct.Items)
 {
  if (string.IsNullOrEmpty(bomItem.BomGroupId))
  {
   var prod = Dynamicweb.Ecommerce.Services.Products.GetProductById(bomItem.BomProductId, bomItem.BomVariantId, Dynamicweb.Ecommerce.Common.Context.LanguageID);
   if (prod != null)
   {
    disableAddToCart = prod.Discontinued || !prod.Active ? "disabled" : disableAddToCart;

    if (prod.IsVariantMaster && prod.Active)
    {
     <select class="mt-2 mb-2 pt-2 pb-2" name="@bomItem.Id">
      @foreach (var variant in prod.VariantCombinations)
      {
       <option value="@variant.ProductId|@variant.VariantId">@prod.Name @variant.GetVariantName(Dynamicweb.Ecommerce.Common.Context.LanguageID).ToString()</option>
      }
     </select>
    }
   }
  }

DW version 9.15.14

Screenshot_2023-11-24_170410.png Screenshot_2023-11-24_170430.png

Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Should be doable.

Have a look at these threads: https://doc.dynamicweb.com/forum/development/development/form-for-adding-bom-configurable-products and https://doc.dynamicweb.com/forum/swift/swift/swift-and-bom-products

Also be aware that if you run the very latest 9.16.3 you have support for BOM in viewmodels using extension methods for the productviewmodel - you can use @Model.GetBomConfiguration to get viewmodels for BOM related data.

BR Nicolai

 
Cátia Torego
Reply

Hi Nicolai,

We updated to 9.16.3 but I can't find the GetBomConfiguration method in the ProductViewModelExtensions.
I'm including
Dynamicweb.Ecommerce.ProductCatalog. Am I missing something? 

 
Steffen Kruse Hansen Dynamicweb Employee
Steffen Kruse Hansen
Reply
This post has been marked as an answer

Hi Cátia,

The GetBomConfiguration should be parts of the ProductViewModelExtentionMethods class, and should be available if you just include Dynamicweb.Ecommerce.ProductCatalog.

I have just checked, and it seems like the changes didn't make it to the 9.16.3 version, but it's part of the 9.16.4 release, which is also currently available on NuGet. Alternatively, if you don't want to do a full upgrade to test this out, you can just upgrade your Dynamicweb.Ecommerce package to 9.16.5, then you will also have the fix available.

I hope it help, and sorry for the inconvenience

Best regards,

Steffen

Votes for this answer: 1
 
Cátia Torego
Reply

Thank you, I was able to use the GetBomConfiguration after adding the right Dynamicweb.Ecommerce package. But I'm still having an issue with adding the variants to the cart.

If I have a parts list created with a group (one product in that group has variants) I can now select the variant when adding to the cart and the variant is added.

  1. redirect:
    false
  2. ProductId:
    PROD157
  3. ProductName:
    Hose Kit (Group)
  4. ProductVariantName:
     
  5. ProductCurrency:
    USD
  6. ProductPrice:
    100.00
  7. ProductReferer:
    component_ProductAddToCart
  8. cartcmd:
    add
  9. PRODITEM59:
    PROD145|VO5
  10. Stock:
    30
  11. UnitID:
     
  12.  
    Quantity:
    1

But if I have a parts list created with individual products (one of the products has variants) I can select the variant but it's not added to the cart, the main products is still added instead. Am I missing something fro this case? I'm using he same logic in both cases.

  1. redirect:
    false
  2. ProductId:
    PROD148
  3. ProductName:
    Hose Kit (Variants)
  4. ProductVariantName:
     
  5. ProductCurrency:
    USD
  6.  
    ProductPrice:
    190.00
  7. ProductReferer:
    component_ProductAddToCart
  8. cartcmd:
    add
  9. PRODITEM51:
    PROD145|VO1
  10. Stock:
    19
  11. UnitID:
     
  12. Quantity:
    1

This is the code I'm using to select the variants for both cases:

@{
 var bomConfiguration = product.GetBomConfiguration();

 if (bomConfiguration != null && bomConfiguration.Items.Any())
 {
  /*Products*/
  foreach (var bomItem in bomConfiguration.Items)
  {
   var prod = Dynamicweb.Ecommerce.Services.Products.GetProductById(bomItem.Product.ProductId, bomItem.Product.VariantId, Dynamicweb.Ecommerce.Common.Context.LanguageID);
   if (prod != null)
   {
    disableAddToCart = prod.Discontinued || !prod.Active ? "disabled" : disableAddToCart;

    if (prod.IsVariantMaster && prod.Active)
    {
     <select class="mt-2 mb-2 pt-2 pb-2" name="@bomItem.Id">
      @foreach (var variant in prod.VariantCombinations)
      {
       <option value="@variant.ProductId|@variant.VariantId">@prod.Name @variant.GetVariantName(Dynamicweb.Ecommerce.Common.Context.LanguageID).ToString()</option>
      }
     </select>
    }
   }
  }
 }
 else if (bomConfiguration != null && bomConfiguration.Groups.Any())
 {
  /*Groups*/
  foreach (var group in bomConfiguration.Groups)
  {
   foreach (var groupProduct in group.Products)
   {
    var prod = Dynamicweb.Ecommerce.Services.Products.GetProductById(groupProduct.ProductId, groupProduct.VariantId, Dynamicweb.Ecommerce.Common.Context.LanguageID);
    if (prod != null)
    {
     disableAddToCart = prod.Discontinued || !prod.Active ? "disabled" : disableAddToCart;

     if (prod.IsVariantMaster && prod.Active)
     {
      <select class="mt-2 mb-2 pt-2 pb-2" name="@group.Id">
       @foreach (var variant in prod.VariantCombinations)
       {
        <option value="@variant.ProductId|@variant.VariantId">@prod.Name @variant.GetVariantName(Dynamicweb.Ecommerce.Common.Context.LanguageID).ToString()</option>
       }
      </select>
     }
    }
   }
  }
 }
}

 

AddToCartGroup.png AddToCartProduct.png GroupOrderLines.png PartsListGroup.png PartsListProducts.png ProductOrderLines.png
 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply
This post has been marked as an answer

Hi Catia

Difficult one - had to disassemble the code! Thank you for the detailed question and problem description which helped me understand (I hope :-))

Seems like when the bom item is a single product and you wanty to add a variant of that, you have to post it like this:

bomProduct.Id + "VariantId" = 'vo1'.

So that would be in your case like this:

Prod148VariantId=vo1
<input name="Prod148VariantId" value="vo1">
<input name="Prod148Quantity" value="2">

Yes, weird - and code with a very long history...

Just made a change to the add to cart so adding single items will support the same notation as adding multiple items (That is using your example from abnove). That will be out with next release of 9.16 

Votes for this answer: 1
 
Cátia Torego
Reply

Thanks Nicolai, I applyed that code for single products and I got it working.

 

You must be logged in to post in the forum