Developer forum

Forum » Ecommerce - Standard features » Auto addMulti to cart not working for prices

Auto addMulti to cart not working for prices

D W
Reply

Hello,

I have been working on creating an auto-add-to-cart page that will add items from a quote in Salesforce.

All the data has come correctly from Salesforce, and based off everything that I can find when searching, I have created the form correctly.

It adds items correctly to the DW cart, however, I am unable to get DW to accept the prices that I am passing in the form. These prices are for custom configured products so it is imperative that I can get the prices to be applied to DW's cart.

Thank you,

Scott


Replies

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Scott,

 

For a number of security reasons you cannot submit the (unit) price for a product in DW when adding it to the cart through a request. You can do ths through the API through, but you need to set the OrderlineType to 2 (Fixed). That will tell DW to not the unit price untouched moving forward.

 

Best Regards,

Nuno Aguiar

 
D W
Reply

That makes sense. Do you happen to know the API call (AddToCart() maybe)? It could save some time hunting through the API.

Thank you,

Scott

 
D W
Reply

Also, it looks like everything that sets the OrderLine type is now obsolete according to the API.

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

There are a few topics on the Forum you can get inspiration from. I find this one interesting enough to get you started

https://doc.dynamicweb.com/forum/development/development/add-orderline-programatically-exception-at-getrewardpoints

 
D W
Reply

This is the code I am using from the above 'inspiration'. However, SetUnitPrice() does not appear to do anything. The price isn't being set when that function call is made so I get the products added to the cart, but they all have price of $0.00.

I'm at a loss here.  

 

      var Cart = Dynamicweb.eCommerce.Common.Context.Cart ?? new Order();
      Cart.Save();

      
      foreach (var quoteLineItem in quoteLines)
      {
        if (string.IsNullOrWhiteSpace(quoteLineItem.ConfigID__c))
        {
          continue;
        }
        GuruConfiguration gcon = new GuruConfiguration();
        GuruConfigurationRepository gcrepo = new GuruConfigurationRepository();
        gcon = gcrepo.GetById(quoteLineItem.ConfigID__c);

        var guru = GuruHelpers.ConvertCudToFlatObjects(quoteLineItem.CUD_XML__c);
        var data = Boxx.Web.Shared.Guru.GuruExtensions.GetOrderDataFromCud(quoteLineItem.CUD_XML__c);

        
        var line = new OrderLine();
        line.Product = null;
        line.ProductID = gcon.DynamicwebProductId;
        Product thisProduct = Product.GetProductByID(line.ProductID);
        line.ProductNumber = thisProduct.Number;
        line.ProductName = thisProduct.Name;
        line.Quantity = quoteLineItem.Quantity > 0 ? (double)quoteLineItem.Quantity : 0;
        line.AllowOverridePrices = true;
       

        if (data.SystemDiscountPrice == 0)
        {
          line.SetUnitPrice(data.SystemTotalPrice);
        }
        else
        {
          line.SetUnitPrice(data.SystemDiscountPrice);
        }


        line.SetOrderLineType(OrderLine.OrderLineType.Fixed);

        line.Order = Cart;

        Cart.OrderLines.Add(line);
        Cart.ClearCachedPrices();
        Cart.Save();

        
      }
      Dynamicweb.eCommerce.Common.Context.SetCart(Cart);

 
D W
Reply

Also, we have event subscribers for Cart.Created, Cart.Line.Added, Cart.Line.Increased, and Cart.Line.Decreased and none of the above code triggers any of those events (Cart.Created and Cart.Line.Added should be firing at the least, which is why there are two Cart.Save()'s in the code, trying to trigger the events).

 
D W
Reply

Update: Looks like I've gotten something working. The Subscribers still don't fire for some reason, but the cart is created finally.

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Scott,

 

Glad you made some progress. I don't know what might have cause that.

 
D W
Reply

It's the Total Price on an OrderLine. The one that should be the total of {quantity} times {unitprice}. Calculate is set to true, but the total price isn't being calculated, and I haven't found anything as yet in the API that will allow me to set the total price. Is there something I'm missing that should trigger the cart totals to be calculated?

 

Thank you,

S

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Scott,

 

Looks like you forgot to set the price to be recalculated when you set the UnitPrice

https://doc.dynamicweb.com/api/html/03730260-ebe9-7930-cff5-7c8af1a75e1b.htm

 

Alternativaly you can try to do this on the cart (instaed of the orderline), but I am not sure if you need to do it before you do the Cart.Save(), or after you set it to the context Dynamicweb.eCommerce.Common.Context.SetCart(Cart); you'll need to try that out.

https://doc.dynamicweb.com/api/html/822c7f9b-75d9-fb1e-fa4c-b50dbfa61f55.htm

 

Best Regards,

Nuno Aguiar

 
D W
Reply

These don't work. The OrderLineService object that both of the above links use does not exist. If it is an DW9 thing, we don't have DW9.

 

Thank you

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply
This post has been marked as an answer

Hi Scott,

 

You have the method ForceCartRecalculation in DW8 as well http://doc.dynamicweb.com/api8/#Dynamicweb~Dynamicweb.eCommerce.Orders.Order~ForcePriceRecalculation.html

 

Best Regards,

Nuno Aguiar

Votes for this answer: 1

 

You must be logged in to post in the forum