Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dan 1288 posts 3921 karma points c-trib
    Aug 06, 2013 @ 01:35
    Dan
    0

    Tea Commerce: How to manipulate price in price object in .NET

    Hi,

    I'm trying to add a 50% discount to the 'CalculateOrderTotalPrice' method with .NET. I have this code:

        protected override Price CalculateOrderTotalPrice(Price subtotalPrice, Price shipmentTotalPrice, Price paymentTotalPrice, PriceWithoutVat transactionFee, Currency currency, Order order)
        {
            // new subtotalPrice value here
            return base.CalculateOrderTotalPrice(subtotalPrice, shipmentTotalPrice, paymentTotalPrice, transactionFee, currency, order);
        }
    

    How would I multiply the subTotalPrice value by 0.5? Currently it's set as a 'Price' object and I'm not having any luck in being able to manipulate it.

    Many thanks

  • Rune Grønkjær 1372 posts 3103 karma points
    Aug 06, 2013 @ 08:06
    Rune Grønkjær
    100

    Hi Dan,

    We are doing it like this:

    protected virtual Price CalculateOrderTotalPrice( Price subtotalPrice, Price shipmentTotalPrice, Price paymentTotalPrice, PriceWithoutVat transactionFee, Currency currency, Order order ) {
          subtotalPrice.MustNotBeNull( "subtotalPrice" );
          shipmentTotalPrice.MustNotBeNull( "shipmentTotalPrice" );
          paymentTotalPrice.MustNotBeNull( "paymentTotalPrice" );
          transactionFee.MustNotBeNull( "transactionFee" );
          currency.MustNotBeNull( "currency" );
          order.MustNotBeNull( "order" );
    
          decimal price = subtotalPrice.Value + shipmentTotalPrice.Value + paymentTotalPrice.Value + transactionFee.Value;
          decimal vat = subtotalPrice.Vat + shipmentTotalPrice.Vat + paymentTotalPrice.Vat;
          return new Price( price, vat, price + vat, currency );
        }

    The MustNotBeNull method is placed in the TeaCommerce.Api.Common.AssertionsExtensions class.

    If you do the same plus your own magic, that should do the trick.

    /Rune

  • Dan 1288 posts 3921 karma points c-trib
    Aug 06, 2013 @ 10:14
    Dan
    0

    Thanks Rune, I'll give this a go.

    Just so I understand a little better (and you understand more what I'm trying to do)...  On the other thread you'd suggested some code which will discount the items in the cart.  I've implemented this and it works nicely, I can multiply the product prices by 0.5 to get the discount on those, but the total at the end doesn't take that into account (e.g. one product in the cart, original price $500 - the order line says $250, which is right, but the total at the end still says $500).  Will implementing the code above complete the process so that the total values throughout take into account the discount?  (The discount is supposed to be on products only).

    So something like this:

    decimal price = (subtotalPrice.Value*0.5m)+ shipmentTotalPrice.Value+ paymentTotalPrice.Value+ transactionFee.Value;

    Is this all that's needed to make the discount persist throughout?

     

    Final question is, if I were to make the discount only available to logged-in members, I can put a check for 'is logged in' in the logic above, but would this persist in the call-back from the payment gateway?  I'm assuming once you click the final checkout button in TeaCommerce it saves all of this stuff, along with any discount logic, rather than calculating it dynamically during the gateway call-back.

    Thanks again.

     

  • Rune Grønkjær 1372 posts 3103 karma points
    Aug 06, 2013 @ 10:22
    Rune Grønkjær
    1

    Hmm. That sounds weird that your changes to the orderlines does not change the order total as well. Maybe you could post your code or send it to us so we could check it? I would like to have Anders taking a look at it. Either you are doing something wrong, or Tea Commerce is, or else I'm just not understanding any of this :)

    /Rune

  • Dan 1288 posts 3921 karma points c-trib
    Aug 06, 2013 @ 10:26
    Dan
    0

    Hi Rune,

    All I'm doing is proof of concept at the moment, like this (queue the dodgy formatting on the forum ;) ):

    using TeaCommerce.Api.Dependency; using TeaCommerce.Api.Models; using TeaCommerce.Api.Services; using TeaCommerce.Api.InformationExtractors; using TeaCommerce.Api.PriceCalculators;

     

    namespace YourApp.TeaCommerceExtension {

     

    [SuppressDependency( "TeaCommerce.Api.PriceCalculators.IOrderCalculator", "TeaCommerce.Api" )] public class OrderCalculator : TeaCommerce.Api.PriceCalculators.OrderCalculator {

    public OrderCalculator( IVatGroupService vatGroupService, ICurrencyService currencyService, IShippingMethodService shippingMethodService, IPaymentMethodService paymentMethodService, IShippingCalculator shippingCalculator, IPaymentCalculator paymentCalculator, IProductInformationExtractor productInformationExtractor )

      : base( vatGroupService, currencyService, shippingMethodService, paymentMethodService, shippingCalculator, paymentCalculator, productInformationExtractor ) {
    }

    protected override decimal CalculateOrderLineUnitPrice( OriginalUnitPrice originalUnitPrice, OrderLine orderLine, Currency currency, Order order ) {

      return originalUnitPrice*0.5M;

    }
    }
    }

     

    It discounts the item line in the cart but the total remains the original total price.

    Can you see where I'm going wrong?

    Thanks

  • Anders Burla 2560 posts 8256 karma points
    Aug 06, 2013 @ 13:17
    Anders Burla
    1

    Hi Dan

    This should work. Can you check the subtotal of the order - that is the total of all order lines. Is that correct? The order total also have shipping prices and etc so maybe that is tricking you somehow :)

    Kind regards
    Anders

  • Dan 1288 posts 3921 karma points c-trib
    Aug 06, 2013 @ 14:55
    Dan
    0

    This is weird, but I restarted the app and indeed it does work, thanks Anders.

    The exact synax is return (originalUnitPrice.Value * 0.5m);

    Thanks

  • Anders Burla 2560 posts 8256 karma points
    Aug 06, 2013 @ 15:17
    Anders Burla
    0

    Great - it should work, and it does :)

    Kind regards
    Anders

Please Sign in or register to post replies

Write your reply to:

Draft