Developer forum

Forum » Development » Custom Subscriber removes Discounts after updating to 8.4.1.8

Custom Subscriber removes Discounts after updating to 8.4.1.8

Martin Nielsen
Reply

Hi DW,

We have this code running on a site to set VAT to 0% if a specific country in EU is selected, and a VAT/EAN number is supplied.

[Dynamicweb.Extensibility.Subscribe( Dynamicweb.Notifications.eCommerce.Cart.BeforeRenderingNewStep )]
  public class OCDBeforeRenderingNewStep : Dynamicweb.Extensibility.NotificationSubscriber
  {
    public override void OnNotify( string notification, Dynamicweb.Extensibility.NotificationArgs args )
    {
      Dynamicweb.Notifications.eCommerce.Cart.BeforeRenderingNewStepArgs afterCustomerCountryIsSetArgs = args as Dynamicweb.Notifications.eCommerce.Cart.BeforeRenderingNewStepArgs;

      
      // Country must be set for this notification to do anything
      if ( afterCustomerCountryIsSetArgs.Order.CustomerCountryCode == null ) {
        return;
      }

      // If we're not on the correct cart page, don't run the code
      string gsCartPageID = Dynamicweb.Base.GetGs( "/Globalsettings/Co3/EventCartPageId" );
      string pageID = Dynamicweb.Base.Request("id", true);
      if ( pageID.Equals( gsCartPageID ) == false )
      {
        return;
      }

      string orderCountryCode = afterCustomerCountryIsSetArgs.Order.CustomerCountryCode.ToUpper();
      if ( string.IsNullOrEmpty( orderCountryCode ) == false )
      {
        Dynamicweb.eCommerce.International.CountryCollection countries = Dynamicweb.eCommerce.International.Country.getCountries( orderCountryCode );
        double countryVat = countries[0].Vat;

        Dynamicweb.eCommerce.Orders.Order o = afterCustomerCountryIsSetArgs.Order;
        // Test if VAT number is set and country is in the EU
        if ( string.IsNullOrEmpty( afterCustomerCountryIsSetArgs.Order.CustomerVatRegNumber ) == false )
        {
          // Set VAT to 0
          if ( Helper.IsEuMemberCountry( orderCountryCode ) == true && orderCountryCode.Equals( "DK" ) == false )
          {
            UpdateVATOnOrder( ref o, 0 );
          }
          // Set VAT to countries standard VAT
          else
          {
            UpdateVATOnOrder( ref o, countryVat );
          }
        }
        // If VAT number is not present, calculate VAT again,since it could have been reset earlier.
        else
        {
          UpdateVATOnOrder( ref o, countryVat );
        }
      }
    }

    private void UpdateVATOnOrder( ref Dynamicweb.eCommerce.Orders.Order order, double newVAT )
    {
      // Do nothing if cart is null
      if ( order == null )
        return;

      // Loop orderlines and change unitprice.
      foreach ( Dynamicweb.eCommerce.Orders.OrderLine ol in order.ProductOrderLines )
      {
        var olPrice = ol.UnitPrice;
        SetNewVat( olPrice, newVAT );

        ol.SetUnitPrice( olPrice, true );

        ol.Type = ((int)Dynamicweb.eCommerce.Orders.OrderLine.OrderLineType.Fixed).ToString();
      }

      order.ClearCachedPrices();

      var payFeeWithoutVat = order.PaymentFee;
      SetNewVat( payFeeWithoutVat, newVAT );
      var shipFeeWithoutVat = order.ShippingFee;
      SetNewVat( shipFeeWithoutVat, newVAT );

      var newOrderPrice = order.PriceBeforeFees.Add( payFeeWithoutVat ).Add( shipFeeWithoutVat );

      // set new orderprice
      order.AllowOverridePrices = true;
      order.Price.PriceWithoutVAT = newOrderPrice.PriceWithoutVAT;
      order.Price.PriceWithVAT = newOrderPrice.PriceWithVAT;
      order.Price.VAT = (newOrderPrice.VAT >= 0 ? newOrderPrice.VAT : 0);
      order.Price.VATPercent = (newOrderPrice.VATPercent >= 0 ? newOrderPrice.VATPercent : 0);
    }

    private void SetNewVat( Dynamicweb.eCommerce.Prices.PriceInfo price, double newVat )
    {
      price.PriceWithVAT = price.PriceWithoutVAT * (1 + (newVat / 100));
      price.VAT = price.PriceWithVAT - price.PriceWithoutVAT;
      price.VATPercent = newVat;
    }
  }

Last week we updated to 8.4.1.8 and now the code above removes Discounts from the cart. Do you have any idea what part of the code does this, and what i should change to avoid it.

// Martin

 


Replies

 
Martin Nielsen
Reply

I would really appreciate some input here. we currently have a running shop with no discounts due the what ever changed between the old DW version and the new.

 
Martin Nielsen
Reply

Okay, so went through my code from start to end, removing one line at a time to see when the error stopped happening, and i found that this line is what causes the error:

ol.Type = ((int)Dynamicweb.eCommerce.Orders.OrderLine.OrderLineType.Fixed).ToString();

Could this be because the voucher is bound to the orderline and that logic doesn't work when OrderLintType.Fixed anymore?

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Hi Martin,

I have found and fixed the issue. We've created a hotfix, 8.4.1.10, which can be made available by contacting the Service Desk.

- Jeppe

 

You must be logged in to post in the forum