darryldecode / laravelshoppingcart

Shopping Cart Implementation for Laravel Framework
1.35k stars 452 forks source link

Cart total or discount total display incorrect #66

Open virsoni opened 8 years ago

virsoni commented 8 years ago

cart conditions Hi I have configure cart and applied few condition for testing purpose but cart total or discount total is not display correctly can you please help me below is my code?

$condition1 = new \Darryldecode\Cart\CartCondition(array( 'name' => 'VAT', 'type' => 'tax', 'target' => 'subtotal', 'value' => '10%', ));

$itemCondition3 = new \Darryldecode\Cart\CartCondition(array( 'name' => 'Discount', 'type' => 'misc', 'target' => 'subtotal', 'value' => '-5%', ));

Cart::condition([$condition1,$itemCondition3]);

        <table class="table">
            <tbody>
                <tr>
                    <td>Order subtotal</td>
                    <th align="right">${{number_format(Cart::getSubTotal(),2)}}</th>
                </tr>
                @foreach(Cart::getConditions() as $condition)
                    <tr>
                        <td>{{$condition->getName()}} ({{$condition->getValue()}})</td>
                        <th align="right">${{number_format($condition->getCalculatedValue(Cart::getSubTotal()),2)}}</th>
                    </tr>                       
                @endforeach
                <tr class="total">
                    <td>Total</td>
                    <th align="right">${{number_format(Cart::getTotal(),2)}}</th>
                </tr>
            </tbody>
        </table>
virsoni commented 8 years ago

Can anyone reply on my issue please?

virsoni commented 8 years ago

Can anyone reply on this issue please?

virsoni commented 8 years ago

Can anyone reply on this issue please?

wouldhide commented 8 years ago

Your problem is the order of counting the VAT and the discount. The way Total gets counted:

  1. subtotal with discount = order subtotal (200) - discount (10) = 190
  2. subtotal with discount (190) * VAT (1.1) = 209

If VAT applies to the subtotal after substracting discount, then 209 is fine. If you want 210 for total, you have to apply VAT to the subtotal, and then discount on subtotal, and then add them, but i don't think this is the correct way.

virsoni commented 8 years ago

thank you for reply. If i go with your suggestion as under.

  1. subtotal with discount = order subtotal (200) - discount (10) = 190
  2. subtotal with discount (190) * VAT (1.1) = 209

How can I get 190 as subtotal with discount to apply VAT?

wouldhide commented 8 years ago

I'm not sure, but try changing the order off applying conditions:

  1. subtotal with VAT applied = order subtotal (200) * VAT = 220
  2. total = subtotal with VAT applied (220) - discount (10) = 210

If it's not working that means VAT is applied to the total after applying other conditions. If that doesn't work, try the following workarounds: a) for the discount use item conditions (for every item) (see docs for per-item conditions) b) try to add VAT as normal condition (don't use type = tax). you will still be able to get the amount of vat applied.

virsoni commented 8 years ago

Okay. we can get normal sub total using this function: Cart::getSubTotal().

My query is: How can i get the sub total with applied condition using one call is there any function?

wouldhide commented 8 years ago

/**

wouldhide commented 8 years ago

NOTE: All cart based conditions should be applied before calling Cart::getTotal() Then Finally you can call Cart::getTotal() to get the Cart Total with the applied conditions.

$cartTotal = Cart::getTotal(); // the total will be calculated based on the conditions you ave provided

Next is the Condition on Per-Item Bases.

wouldhide commented 8 years ago

is this what you are looking for?

virsoni commented 8 years ago

yes that is correct but assume when we applied both conditions and again go to any product page than come again to cart page order calculation not working properly as getTotal() function take the final total.

I have to follow this way to display order summary.

                <tr>
                    <td>Order subtotal</td>
                    <th align="right">${{number_format(Cart::getSubTotal(),2)}}</th>
                </tr>
                {{--*/ $condition = Cart::getCondition('VAT') /*--}}
                {{--*/ $vat = 0 /*--}}
                @if($condition)
                    <tr>
                        <td>{{$condition->getName()}} ({{$condition->getValue()}})</td>
                        <th align="right">${{number_format($condition->getCalculatedValue(Cart::getSubTotal()),2)}}</th>
                    </tr>
                    {{--*/ $vat = $condition->getCalculatedValue(Cart::getSubTotal()) /*--}}
                @endif

                {{--*/ $condition = Cart::getCondition('Discount') /*--}}
                @if($condition)
                    <tr>
                        <td>{{$condition->getName()}} ({{$condition->getValue()}})</td>
                        <th align="right">${{number_format($condition->getCalculatedValue(Cart::getSubTotal()+$vat),2)}}     </th>
                    </tr>
                @endif
                <tr class="total">
                    <td>Total</td>
                    <th align="right">${{number_format(Cart::getTotal(),2)}}</th>
                </tr>
virsoni commented 8 years ago

I can not pass Cart::getTotal() function as parameter of $condition->getCalculatedValue() for discount condition.... otherwise it will calculate discount on final total.

virsoni commented 8 years ago

please let me know your thoughts if you get my problem.

ebisbe commented 8 years ago

@virsoni You can add an order of appliance for the conditions. So you can add them in every part of the code and the getConditions will apply in the order you have specified.