• Resolved nawafgantare

    (@nawafgantare)


    So I am creating a function which will add 5% discount when a certain payment gateway is selected.

    // Add the discount if the Razorpay payment method is selected
    function razorpay_online_payment_discount( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
            return;
        }
    
        $chosen_payment_method = WC()->session->get('chosen_payment_method');
    
        // Check if payment method is set
        if ( ! isset( $chosen_payment_method ) ) {
            return;
        }
    
        $discount_percentage = 5;
    
        // Remove existing fee if any
        foreach ( $cart->get_fees() as $fee_key => $fee ) {
            if ( 'online_payment_discount' === $fee->get_id() ) {
                $cart->remove_fee( $fee_key );
                break;
            }
        }
    
    if ( 'razorpay' === $chosen_payment_method ) {
        // Only consider the subtotal for calculating the discount
        $subtotal = $cart->subtotal;
        $discount = round( $subtotal * ( $discount_percentage / 100 ), 2 ); 
        $cart->add_fee( __( 'Online Payment Discount', 'woocommerce' ), -$discount, false );
    }
    
    }
    add_action( 'woocommerce_cart_calculate_fees', 'razorpay_online_payment_discount', 10, 1 );
    
    // Update the cart/checkout page when the payment method is changed
    function razorpay_online_payment_discount_refresh() {
        if ( ! is_checkout() && ! is_cart() ) {
            return;
        }
    
        wc_clear_notices(); // Clear any displayed notices
        WC()->session->set( 'refresh_totals', true ); // Trigger update of totals
    
        ?>
        <script type="text/javascript">
            (function( $ ) {
                $(document.body).on('change', 'input[name="payment_method"]', function() {
                    $('body').trigger('update_checkout');
                });
            })(jQuery);
        </script>
        <?php
    }
    add_action( 'wp_footer', 'razorpay_online_payment_discount_refresh', 50 );

    Now this code is working fine but tere is an issue. Lets say the cart value is 798.00 then 5% of it should be 39.90 but the code is making it 41.10

    I tried another code where instead of using add_fee function i tried to do it outside just to show text

      if ( 'razorpay' === $chosen_payment_method ) {
            $subtotal = WC()->cart->subtotal + WC()->cart->shipping_total;
            $discount = round( $subtotal * ( $discount_percentage / 100 ), 2 ); // Round the discount amount to two decimal places
            $formatted_discount = wc_price( $discount );
    
            echo '<tr class="discount-row">';
            echo '<th>' . __( 'Online Payment Discount', 'woocommerce' ) . '</th>';
            echo '<td>- ' . $formatted_discount . '</td>';
            echo '</tr>';
        }

    This code works fine and gives me proper 39.90 value. Something is happening in the backend which i am not able to figure out how to solve this.

    I am using these settings in woocommerce:

    • Prices entered with tax -> Yes, I will enter prices inclusive of tax
    • Rounding -> Rounding Round tax at subtotal level, instead of rounding per line
    • Display prices during cart and checkout -> Including Tax

    Any help would be appreciated to find what can be done to debug this.
    Here is a cart var_dump if this helps – https://pastebin.com/raw/47LKk0VR

    • This topic was modified 2 years, 11 months ago by nawafgantare.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @nawafgantare

    Thanks for reaching out!

    I understand that you are using the code snippets above to add a 5% discount to the total price when a certain payment gateway was used upon checkout, however, it is not working as expected, correct?

    This is a bit of a complicated topic that would need some customization to address. Unfortunately, custom coding is not something we can assist with directly. However, I’ll keep this thread open for a bit to see if anyone from the community can lend a hand.

    If you have any other questions related to development or custom coding, don’t hesitate to reach out to some of the great resources we have available for support. The WooCommerce community is filled with talented open-source developers, and many of them are active on the channels listed below:

    Hope this helps!

    Thread Starter nawafgantare

    (@nawafgantare)

    @xue28 indeed I am using Code snippet. but my query is If simple math is giving proper value then why after adding add_fee() is giving 1-2rs extra ? There is not much document available on this as well like what data it takes or what factors affect the hook.

    Hi,

    I understand that you are looking for assistance with a custom code request. However, I’d like to kindly inform you that custom code requests fall outside our support policy. We’re happy to help with the WooCommerce Core plugin and its official extensions, but for custom code-related queries, you might need to seek help from a developer as mentioned previously.

    There is a plugin on the WordPress.org repository: Discounts Per Payment Method on WooCommerce, you can contact the plugin support if you need further assistance with it.
     
    Please note that we do not develop or support for the Discounts Per Payment Method on WooCommerce plugin. While we’re experts on our own products, third-party software is best supported by their own respective developers. If you have further questions about it, you can use their support forum here.

    If you have any other questions related to WooCommerce, please don’t hesitate to ask.
     
    Cheers.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Discounting not Working properly.’ is closed to new replies.