Developer forum

Forum » Integration » Best approach to manipulate data from LiveIntegration product prices

Best approach to manipulate data from LiveIntegration product prices

Jan Sangill
Reply

Hi,

I have a very special case, where on the webpage the webprice is 110. If a cusatomer is logged in - then his price could be: 120. If the price - as it is in this case, is above the web price, then the customer should be displayed the webprice, and not the 120 comming from BC LiveIntegration.

What is the best and easiest way to achieve this logic? Can I manipulate the data returned from BC before DW processes it? Any ideas?

Regards

Jan


Replies

 
Shiwanka Chathuranga Dynamicweb Employee
Shiwanka Chathuranga
Reply

Which approach your working on?

if its Template tags, in live integration having ProductTemplateExtender that you can do nesessory customization and achive it

i have not tried with viewmodel, but someone will help if your using viewmodel with liveintegration

 
Jan Sangill
Reply

Hi, it is template tags.

If I use the templateExtender to do this, then I will also have to do the changes elsewhere. As in Ecommerce.Cart.Line.Added and other places too?

Is there a place where this can be achieved just after communicating with ERP and then manipulate data? So I can only do the logic one place and it carries through the whole system?

WOuld a priceprovider make sense? The easiest solution is what I am after:>

Appreciate any help

 

 
Dmitriy Benyuk Dynamicweb Employee
Dmitriy Benyuk
Reply

Hi Jan,
you can try to implement the CustomProductProvider that can override the prices from the ERP. The code should look like that:

using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Products;
using Dynamicweb.Ecommerce.Prices;
using System.Linq;

namespace Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Examples
{
    /// <summary>
    /// Class CustomProductProvider.
    /// </summary>
    /// <seealso cref="Products.ProductProviderBase" />
    public class CustomProductProvider : ProductProviderBase
    {
        /// <summary>
        /// Gets the price.
        /// </summary>
        /// <param name="product">The product.</param>
        /// <param name="quantity">The quantity.</param>
        /// <returns>PriceInfo.</returns>
        public override PriceInfo GetPriceInfo(LiveContext context, ProductInfo product, double quantity)
        {
            // Example: if we have a price per kilogram - we need to multiply it by quantity
            if (double.TryParse(product["TotalPrice"].ToString(), out double unitPriceWithoutVat))
            {
                double? unitPriceWithVat = (double?)product["TotalPriceWithVat"];

                var currency = Common.Context.Currency;
                if (currency is null)
                    currency = Services.Currencies.GetAllCurrencies().FirstOrDefault();
                var price = new PriceInfo(currency);
                price.PriceWithoutVAT = unitPriceWithoutVat * quantity;
                price.PriceWithVAT = unitPriceWithVat != null ? unitPriceWithVat.Value * quantity : 0;
                return price;
            }
            else
            {
                return base.GetPriceInfo(context, product, quantity);
            }
        }
    }
}

So you need to implement your own custom logic in the GetPriceInfo method to return the price for the specific product instead.
BR, Dmitrij

 
Shiwanka Chathuranga Dynamicweb Employee
Shiwanka Chathuranga
Reply

@Dimithri

as priority CustomProductProvider   or ProductTemplateExtender execute Last?    as my knowledge its ProductTemplate extender right?

 

in that case @jan need to implement both, ProductTemplateExtender to display price and CustomroductProvider to calculation?

is that correct?

 
Dmitriy Benyuk Dynamicweb Employee
Dmitriy Benyuk
Reply

CustomProductProvider is executed first. Just try to implement the CustomProductProvider  and override the old/obsoleted GetPriceInfo method:
public virtual PriceInfo GetPriceInfo(Integration.ProductInfo product, double quantity, LiveContext context)
or both
public override PriceInfo GetPriceInfo(Integration.ProductInfo product, double quantity, LiveContext context)
public override PriceInfo GetPriceInfo(LiveContext context, ProductInfo product, double quantity)

 
Shiwanka Chathuranga Dynamicweb Employee
Shiwanka Chathuranga
Reply

but that price template tag will override by Liveintegration ProductTemplateExtender right?

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

You cannot change prices using template tags and viewmodels.

 
Shiwanka Chathuranga Dynamicweb Employee
Shiwanka Chathuranga
Reply

@nicolai    your right,

but i think you can manipulate,

in priceprovider

        public override PriceRaw FindPrice(Product product, double quantity, string variantId, Currency currency, string unitId, User user)
        {
            PriceRaw rawPrice = new PriceRaw();
            rawPrice.Currency = currency;
            rawPrice.Price = 100;
            return rawPrice;

 

 

in producttemplatextender (if needed)

pricesTemplate.SetTag("Ecom:Product.Prices.Price", 100);

 

just simple example, what ever price you have, it sells for 100

did i wrong?

 

 

 
Jan Sangill
Reply

Hi Dmitriy,

I tried implementing your suggested code, and it does display the price correctly after I alter it on my product page. 

In my index productlist the code is fired also and it returns the new price I want it to be, however it keeps the live integration price when I get to the view. So the price sent back is not used.

In my cart, some weird stuff is also happening. Like Ecom:Order..PriceWithoutVATFormatted is a tag set - that holds the old value from live integration.

Any ideas?

 

 
Dmitriy Benyuk Dynamicweb Employee
Dmitriy Benyuk
Reply

Hi Jan,
for the orders handling it is needed to implement something different, not even sure what way is better. The problem is that you are trying to mix two systems being a "Master" but it was not designed to support this behaviour, usually one system can be a Master - Dynamicweb or ERP.

It looks like you will need to change the order calculation in this case, so some order lines will remain as they are from the BC, and some needs to be recalculated based on your "custom" product price logic so that should also have effect on the total order price being calculated in BC already.

So you can subscribe to the Live integration: Order.OnAfterSendingOrderToErp and then you will need to parse the response xml ("ResponseDocument" property in the OnAfterSendingOrderToErpArgs class) and whole order lines xmls and recalculate the lines and order amounts and update the "ResponseDocument" xml object with a new values.

Otherwise it seems the BC part should be customized/adjusted to suit this custom price cart calculation logic, so you can have a custom BC plugin and subscribe to the events:
OrdersPublisher.CalculateOrderOnBeforeAddSalesLine
OrdersPublisher.CreateOrderOnAfterCreateSalesLine
Both subscribers have the "salesLine" passed as a reference object so you can change the salesLine.UnitPrice to some custom value for this salesLine.CustomerNo for this salesLine.ItemNo by reading the request xml orderline unit price from Dynamicweb request xml object.

Maybe you can consider to set special prices for this customer/customer groups in the BC side so they can get the same price that is in Dynamicweb? (then no custom code required)
Or import the prices from BC into Dynamicweb

BR, Dmitrij

 
Jan Sangill
Reply

Hi Dmitriy,

Thanks for your feed. Much appreciated.

Yah I know this is not meant to be supported. It is not optimal.

I will consider if the optimal approach would maybe being doing this in BC instead with extending it there.

But before I do that, I would like to know if you have any idea why the price itself with the code you suggested would work on the productpage, but not in the productlist via an index?
Would it make sense to use a custom priceprovider instead of the ProductProviderBase? Or am I missing something here?

 
Dmitriy Benyuk Dynamicweb Employee
Dmitriy Benyuk
Reply

Hi Jan,
the example on how to make it work for the BC side for the orders sales line prices customization is now given here.
The Live integration already implements the PriceProvider and then the Live integration price provider should also call your CustomProductProvider so maybe the problem is that it can not find the Live integration instance or live integration can skip it because of some Live integration checkboxes are on/off.
Can you explain where it doesn't work as I haven't catch what do you mean product list via an index?
Also do you use one of the latest Live integration version? Can you try to set the Shop setting to Any, Lazy load product info Off, Live product info for anonymous users On?
BR, Dmitrij

 
Jan Sangill
Reply

Hi Dimitri,

"product list via an index" - I mean the productlist that displays X amount of products. VIa Product Catalog -> Index ->Query

Here it is fetching the prices from LiveIntegration as it should. The ProductProviderBase custom logic I applied is working and it seems to send it back. But the price actually shown when I print it out in template it the price received from LiveIntegration.

So the issue is not that LiveIntegration is not working. It is that the ProductProviderBase is not applying my prices everywhere:>

Hope this makes sense

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

You need to use a price provider.

 
Jan Sangill
Reply

Hi Nicolai or Dmitriy ,

If I use a priceprovider while using live integration, then the returned prices in my priceprovider will not be used, but the price from the live integration provider.

And in this case, I would need to override the Liveintegration priceprovider.

I am contemplating doing this logic in BC itself with extending BC plugin Unit instead. However if any ways to achieve it in DW - I am all ears. 

At the moment Dmitriy's ideas have resultet me to get the furthest. Still have the issue with the productlistpage with an index not working properly.

Let me know.

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Hi Jan

Strike my comment. Go with Dmitriys approach. 

 

You must be logged in to post in the forum