malsubrata / woo-wallet

WooCommerce Wallet
GNU General Public License v3.0
78 stars 37 forks source link

Wallet bug with product refunds #53

Open fabiomail opened 4 years ago

fabiomail commented 4 years ago

Hello,

I found a major bug with the cashback calculation: the wallet balance is not rectified in case of a product refund.

This is a major issue because a user could keep buying expensive products and then asking for a refund, keeping the entire cashback amount. Obviously, the cashback earned from a refunded order needs to be written off.

Please see this test on plain installation: https://drive.google.com/file/d/1XPCPnIRzFu7mSiWn4Mk-EcB25CCqoOGB/view?usp=sharing

I look forward to finding a solution. Hope it helps to make TeraWallet the best cashback solution on the market.

malsubrata commented 4 years ago

Hi,

Please use the bellow code in the active theme function.php file to resolve this issue.

add_action('woocommerce_order_status_refunded', 'woocommerce_order_status_refunded_callback');

if (!function_exists('woocommerce_order_status_refunded_callback') && function_exists('get_total_order_cashback_amount')) {
    /** debit cashback amount * */
    if ($total_cashback_amount = get_total_order_cashback_amount($order_id)) {
        if ($this->debit($order->get_customer_id(), $total_cashback_amount, sprintf(__('Cashback for #%s has been debited', 'woo-wallet'), $order->get_order_number()))) {
            delete_post_meta($order_id, '_general_cashback_transaction_id');
            delete_post_meta($order_id, '_coupon_cashback_transaction_id');
        }
    }
}
fabiomail commented 4 years ago

Hi @malsubrata , thank you for your prompt reply.

I tried your code, but didn't solve the issue yet: https://drive.google.com/file/d/1lPeMxq-98uePMTMg_n2JscAkMF6GJQC7/view?usp=sharing

Could you double check?

malsubrata commented 4 years ago

@fabiomail Sorry for the error. Please use below updated code.

add_action('woocommerce_order_status_refunded', 'woocommerce_order_status_refunded_callback');

if (!function_exists('woocommerce_order_status_refunded_callback')) {
    function woocommerce_order_status_refunded_callback($order_id) {
        $order = wc_get_order( $order_id );
        /** debit cashback amount * */
        if (function_exists('get_total_order_cashback_amount') && $total_cashback_amount = get_total_order_cashback_amount($order_id)) {
            if (woo_wallet()->wallet->debit($order->get_customer_id(), $total_cashback_amount, sprintf(__('Cashback for #%s has been debited', 'woo-wallet'), $order->get_order_number()))) {
                delete_post_meta($order_id, '_general_cashback_transaction_id');
                delete_post_meta($order_id, '_coupon_cashback_transaction_id');
            }
        }
    }
}
fabiomail commented 4 years ago

Hi @malsubrata , the code works perfectly this time! Thank you very much 👍

There would be one more scenario that could be improved, if possible: cashback write-off in case of partial refunds (sub-order of a multiple vendor order).

Here the demo: https://drive.google.com/file/d/1lRTPLyeHTYvyYT9DoMAeigxVXaHdef19/view?usp=sharing

Let me know if this is something you can take care as well.

fabiomail commented 4 years ago

Hi @malsubrata any updates about my latest message? It is really important that refunds are compatible with multiple vendor orders to really make your wallet compatible with Dokan multi-vendor. Looking forward to hearing from you :)

malsubrata commented 4 years ago

@fabiomail Yes, we looked into this issue and trying to fix this. I Will let you know.