Email order notification not containing coupon discount


  • Default avatar
    svizec    
     13 years ago
    0

    Hi,

    today i succesfully instaled a free AWO coupon version. So far so good. Just needed to make some changes in php based on blog instructions - total order amount was not calculated correctly - and now it works just fine. I need some extra features that PRO version has but I would just like to test the free version properli before I place my order.

    Now I have this problem:
    In checkout the coupon discount and total amount for payment is shown and calculated correctly. But when a buyer submits his order the administrator and the buyer both get an email notifiocation with order details. The problem is that the email content shows the order without coupon discount - only regular prices and total amount. That is not OK.
    I tried to confirm the order by administrator and the buyer still has the wrong order details shown.

    Please advise how to fix this. Thanks.

    P.S.: Sorry if there was a similar topic allready - just could not find it.
  • Default avatar
    svizec    
     13 years ago
    0

    Just noticed something new/additional problem:

    Backend od Joomla, when entering in the customers order, shows the coupon code OK but the discount is not accepted and shown even though the Checkout showd it correctly.
  • Your avatar
    seyi    
     13 years ago
    0

    Yes, this means the coupon discount is not being stored in the database for some reason. What version of virtuemart are you using? Have you made modifications to it? Particularly class/ps_checkout.php? The order calculation for entry into the database is done in that file in the function calc_order_totals. In that function, there should be a line that looks similar to this:

    <?php
        $d
    ['order_total'] = $totals['order_total'] =     $tmp_subtotal 
                                            
    $totals['order_tax']
                                            + 
    $totals['order_shipping']
                                            + 
    $totals['order_shipping_tax']
                                            - 
    $totals['coupon_discount']
                                            - 
    $totals['payment_discount'];
    ?>
  • Default avatar
    svizec    
     13 years ago
    0

    Thanks for fast reply.
    I'm using Virtuemart version 1.1.4. I made PHP modifiacations during instal based on the instructions that are also written in user manual. Also made adjustments in class/ps_checkout.php but I'm no expert in PHP - better to say I have no experience :) ....

    The original ps_checkout.php looked like this - starting in line 1284:
    <?php
            
    /* DISCOUNT HANDLING */
            
    if( !empty($_SESSION['coupon_discount']) ) {
                
    $totals['coupon_discount'] = floatval($_SESSION['coupon_discount']);
            }
            else {
                
    $totals['coupon_discount'] = 0.00;
            }

            
    // make sure Total doesn't become negative
            
    if( $tmp_subtotal $totals['order_subtotal'] = $tmp_subtotal 0;
            if( 
    $totals['order_taxable'] < $totals['order_taxable'] = 0;

            
    // from now on we have $order_tax_details
            
    $d['order_tax'] = $totals['order_tax'] = round$this->calc_order_tax($totals['order_taxable'], $d), );
            
            if( 
    is_object($this->_SHIPPING) ) {
                
    /* sets _shipping */
                
    $d['order_shipping'] = $totals['order_shipping'] = round$this->calc_order_shipping$d ), );

                
    /* sets _shipping_tax
                * btw: This is WEIRD! To get an exactly rounded value we have to convert
                * the amount to a String and call "round" with the string. */
                
    $d['order_shipping_tax'] = $totals['order_shipping_tax'] = roundstrval($this->calc_order_shipping_tax($d)), );
            }
            else {
                
    $d['order_shipping'] = $totals['order_shipping'] = $totals['order_shipping_tax'] = $d['order_shipping_tax'] = 0.00;
            }

            
    $d['order_total'] = $totals['order_total'] =     $tmp_subtotal 
                                                
    $totals['order_tax']
                                                + 
    $totals['order_shipping']
                                                + 
    $totals['order_shipping_tax']
                                                - 
    $totals['coupon_discount']
                                                - 
    $totals['payment_discount'];
            
            
    $totals['order_tax'] *= $discount_factor;

            return 
    $totals;
        }

    Now when I made changes it lookes like this:

    /* DISCOUNT HANDLING */
            
    $tmp_coupon_discount $totals['coupon_discount'];
            if(!empty(
    $totals['coupon_discount']) && PAYMENT_DISCOUNT_BEFORE == '1') {
            
    // coupon includes tax, we need to remove tax from coupon
                
    $tax_before_discount $d['order_subtotal_withtax'] - $totals['order_taxable'];
                
    $tmp_coupon_discount = ($d['order_subtotal_withtax']-$tax_before_discount)/$d['order_subtotal_withtax']*$totals['coupon_discount'];
            }

            
    // make sure Total doesn't become negative
            
    if( $tmp_subtotal $totals['order_subtotal'] = $tmp_subtotal 0;
            if( 
    $totals['order_taxable'] < $totals['order_taxable'] = 0;

            
    // from now on we have $order_tax_details
            
    $d['order_tax'] = $totals['order_tax'] = round$this->calc_order_tax($totals['order_taxable'], $d), );
            
            if( 
    is_object($this->_SHIPPING) ) {
                
    /* sets _shipping */
                
    $d['order_shipping'] = $totals['order_shipping'] = round$this->calc_order_shipping$d ), );

                
    /* sets _shipping_tax
                * btw: This is WEIRD! To get an exactly rounded value we have to convert
                * the amount to a String and call "round" with the string. */
                
    $d['order_shipping_tax'] = $totals['order_shipping_tax'] = roundstrval($this->calc_order_shipping_tax($d)), );
            }
            else {
                
    $d['order_shipping'] = $totals['order_shipping'] = $totals['order_shipping_tax'] = $d['order_shipping_tax'] = 0.00;
            }

            
    $d['order_total'] = $totals['order_total'] =     $tmp_subtotal
                                        
    $totals['order_tax']
                                        + 
    $totals['order_shipping']
                                        + 
    $totals['order_shipping_tax']
                                        - 
    $tmp_coupon_discount
                                        
    $totals['payment_discount'];
            
            
    $totals['order_tax'] *= $discount_factor;

            return 
    $totals;
        }
    ?>

    I probably made something wrong??
  • Your avatar
    seyi    
     13 years ago
    0

    I quickly looked at the code, and that seems to be fine. In the same function should be these lines:
    <?php
    /* DISCOUNT HANDLING */
    if( !empty($_SESSION['coupon_discount']) ) {
        
    $totals['coupon_discount'] = floatval($_SESSION['coupon_discount']);
    }
    else {
        
    $totals['coupon_discount'] = 0.00;
    }
    ?>


    Do you see it there?
  • Default avatar
    svizec    
     13 years ago
    0

    In original ps_checkout file I had, in line 1268 i have function calc_order_totals

    Then in line 1284 is a comment /* DISCOUNT HANDLING */ and right after it are the lines that you posted. I've deleted them in the new file during instal based on the instructions.

    Now I have these:

    /* DISCOUNT HANDLING */
    $tmp_coupon_discount = $totals['coupon_discount'];
    if(!empty($totals['coupon_discount']) && PAYMENT_DISCOUNT_BEFORE == '1') {
    // coupon includes tax, we need to remove tax from coupon
    $tax_before_discount = $d['order_subtotal_withtax'] - $totals['order_taxable'];
    $tmp_coupon_discount = ($d['order_subtotal_withtax']-$tax_before_discount)/$d['order_subtotal_withtax']*$totals['coupon_discount'];
    }

    Where should I insert the lines again? Right after comment /* DISCOUNT HANDLING */ and befor the lines I have now in ps_checkout?
  • Your avatar
    seyi    
     13 years ago
    0

    It should be inserted right before these lines
    <?php
    $d
    ['order_total'] = $totals['order_total'] = $tmp_subtotal
    $totals['order_tax']
    $totals['order_shipping']
    $totals['order_shipping_tax']
    $tmp_coupon_discount
    $totals['payment_discount'];
    ?>
  • Default avatar
    svizec    
     12 years ago
    0

    Sory seyi for replying so late .... just could not find the time to work on this issue.

    I've inserted the lines in place and now the discount value is shown on the e-mail notifications but the calculation of total amount for payment is wrong - the discount is not taken in total amount calculation.

    Here is an example for the order for total amount of 12,00 € with 20% tax and 20% discount.

    Total brutto : €12,00
    Coupon discount: - €2,40
    Shipping and handling : €4,76
    TAX (20%) : €2,39
    TOTAL: €16,36

    During checkout all works fine.

    I kindly ask for your further advise.

    Thanks
  • Your avatar
    seyi    
     12 years ago
    0

    looks like the total is excluding the coupon discount tax, 39 cents. where is the incorrect total being shown?
  • Default avatar
    svizec    
     12 years ago
    0

    It seems like TAX is calculated but the netto discount value is not deducted from TOTAL. If there would be no coupon discount then total with tax would be 16,76 € (12,00 + 4,76). So I think that only discount TAX ( 20%) was deducted from total (amount 0,40 €) but not the netto discount (2,00 €).

    The correct calculation should be:
    Total brutto : €12,00 (10,00€ + 2,00 TAX)
    Coupon discount: - €2,40 (2,00€ + 0,40€ TAX)
    Shipping and handling : €4,76 (3,97€ + 0,79€ TAX)
    TAX (20%) : €2,39 (calculated and shown correctly (2,00 - 0,40 + 0,79)
    TOTAL: €14,36 and not €16,36 (Total did not include 2,00€ neto discount - tax is calculated though)

    The incorrect total is show in:
    - notification emails that the customer receives
    - on the web page when a customer follows the link in the notification email and the web page shows Order details information.

    Thanks for helping and answering so quickly.

    svizec
  • Your avatar
    seyi    
     12 years ago
    0

    ok, this is calculated in ps_checkout.php. Can you paste the contents of function calc_order_totals? Whatever is calculated in that function is what gets put into the database, so something there must be off.
  • Default avatar
    svizec    
     12 years ago
    0

    Pasted below. Plese advise what and where to change. Thanks seyi.

    <?php
        
    /**
         * Calculates the current order totals and fills an array with all the values
         *
         * @param array $d
         * @return array
         */
        
    function calc_order_totals( &$d ) {
            global 
    $discount_factor$mosConfig_offset;
            
            
    $totals = array();
            
            
    /* sets _subtotal */
            
    $totals['order_subtotal'] = $tmp_subtotal $this->calc_order_subtotal($d);
            
            
    $totals['order_taxable'] = $this->calc_order_taxable($d);
            
            if( !empty(
    $d['payment_method_id'])) {
                
    $totals['payment_discount'] = $d['payment_discount'] = $this->get_payment_discount($d['payment_method_id'], $totals['order_subtotal']);
            } else {
                
    $totals['payment_discount'] = $d['payment_discount'] = 0.00;
            }

            
    /* DISCOUNT HANDLING */
            
    $tmp_coupon_discount $totals['coupon_discount'];
            if(!empty(
    $totals['coupon_discount']) && PAYMENT_DISCOUNT_BEFORE == '1') {
            
    // coupon includes tax, we need to remove tax from coupon
                
    $tax_before_discount $d['order_subtotal_withtax'] - $totals['order_taxable'];
                
    $tmp_coupon_discount = ($d['order_subtotal_withtax']-$tax_before_discount)/$d['order_subtotal_withtax']*$totals['coupon_discount'];
            }

            
    // make sure Total doesn't become negative
            
    if( $tmp_subtotal $totals['order_subtotal'] = $tmp_subtotal 0;
            if( 
    $totals['order_taxable'] < $totals['order_taxable'] = 0;

            
    // from now on we have $order_tax_details
            
    $d['order_tax'] = $totals['order_tax'] = round$this->calc_order_tax($totals['order_taxable'], $d), );
            
            if( 
    is_object($this->_SHIPPING) ) {
                
    /* sets _shipping */
                
    $d['order_shipping'] = $totals['order_shipping'] = round$this->calc_order_shipping$d ), );

                
    /* sets _shipping_tax
                * btw: This is WEIRD! To get an exactly rounded value we have to convert
                * the amount to a String and call "round" with the string. */
                
    $d['order_shipping_tax'] = $totals['order_shipping_tax'] = roundstrval($this->calc_order_shipping_tax($d)), );
            }
            else {
                
    $d['order_shipping'] = $totals['order_shipping'] = $totals['order_shipping_tax'] = $d['order_shipping_tax'] = 0.00;
            }

            if( !empty(
    $_SESSION['coupon_discount']) ) {
                
    $totals['coupon_discount'] = floatval($_SESSION['coupon_discount']);
            }
            else {
                
    $totals['coupon_discount'] = 0.00;
            }

            
    $d['order_total'] = $totals['order_total'] =     $tmp_subtotal
                                        
    $totals['order_tax']
                                        + 
    $totals['order_shipping']
                                        + 
    $totals['order_shipping_tax']
                                        - 
    $tmp_coupon_discount
                                        
    $totals['payment_discount'];
            
            
    $totals['order_tax'] *= $discount_factor;

            return 
    $totals;
        }
    ?>
  • Your avatar
    seyi    
     12 years ago
    0

    browsing through your code, you do not have the coupon discount for calculation. First you do
    <?php
            $tmp_coupon_discount 
    $totals['coupon_discount'];
    ?>


    and later on, you define totals['coupon_discount']
    <?php
            
    if( !empty($_SESSION['coupon_discount']) ) {
                
    $totals['coupon_discount'] = floatval($_SESSION['coupon_discount']);
            }
            else {
                
    $totals['coupon_discount'] = 0.00;
            }
    ?>


    I think if you enter that solution in the same order as the blog it should work:
    https://awodev.com/blog/virtuemart-coupon-error-discount-before-tax#solution3

    or use
    <?php
            $tmp_coupon_discount 
    floatval($_SESSION['coupon_discount']);
    ?>

    instead