1

I have a store selling fresh produce and quite often have products purchased and paid for that aren't available on the day from the market. I then refund the customer (using the woocommerce manual refund facility) and apply the refund total as a credit on the next order. I am displaying a list of all items refunded from the customers previous order and applying the total refund as a discount (negative cart fee) at checkout. Its working perfectly thanks to code from this answer and here

What I would like though is to override the woocommerce procedure of reducing the refunded order total. In my case I am getting a double discount - i.e the initial order that had a refund is being discounted and the subsequent order is also being discounted by the refunded amount.

What I would like to achieve is to stop woocommerce from changing the order total on a refund i.e keep it the same as what the customer paid. Is this possible?

Here is my code so far:

//Check total of refunded items from last order - add as a fee at checkout
function add_last_order_refund_total_at_checkout($cart_object){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $user_id = get_current_user_id(); // The current user ID
    $customer = new WC_Customer( $user_id );
    $last_order = $customer->get_last_order();
    $order_id = $last_order;

    // Get the WC_Order Object instance (from the order ID)
    $order = wc_get_order( $order_id );

    // Get the Order refunds (array of refunds)
    $order_refunds = $order->get_refunds();
    $total_to_refund = $order->get_total_refunded()*(-1);//WIP need to check which items have tax
    
    if (!empty($order_refunds)) WC()->cart->add_fee( 'Refund', $total_to_refund  );  
}
add_action( 'woocommerce_cart_calculate_fees', 'add_last_order_refund_total_at_checkout', 10, 1 );
     
//----------------------------------------------------------------
//get list of refunded items from last order - display in a table at checkout
function the_refunded_tems(){
    
    $user_id = get_current_user_id(); // The current user ID
    $customer = new WC_Customer( $user_id );
    $last_order = $customer->get_last_order();
    $order_id = $last_order;

    // Get the WC_Order Object instance (from the order ID)
    $order = wc_get_order( $order_id );

    // Get the Order refunds (array of refunds)
    $order_refunds = $order->get_refunds();
    if ($order_refunds){
    ?>
    <div class="refund-checkout-table">
        <h4>Refunds from previous order:</h4>
        <table class="refunded-items-at-checkout">
            <tbody>
            <?php
            // Loop through the order refunds array
            foreach( $order_refunds as $refund ){
                // Loop through the order refund line items
                foreach( $refund->get_items() as $item_id => $item ){

                    ## --- Using WC_Order_Item_Product methods --- ##
                    $refunded_quantity      = $item->get_quantity(); // Quantity: zero or negative integer
                    $refunded_name         = $item->get_name(); // Quantity: zero or negative integer
                    $refunded_line_subtotal = $item->get_subtotal(); // line subtotal: zero or negative number
                    $refunded_line_tax = $item->get_subtotal_tax();

                    // Get the original refunded item ID
                    $refunded_item_id       = $item->get_meta('_refunded_item_id'); // line subtotal: zero or negative number
                    $total_to_refund = $order->get_total_refunded();
                ?>    
                    <tr class="">
                        <td class="checkout-refund-product-name">
                            <?php echo $refunded_name . '&nbsp;';  ?>
                            <?php echo  '&times;&nbsp;'. ($refunded_quantity*-1) ;?>
                        </td>
                        <td class="checkout-refund-product-total">
                            <?php echo  round((($refunded_line_subtotal+$refunded_line_tax)*-1),2); ?>
                        </td>
                    </tr>
                <?php
                }
            }
        ?>  
        </tbody>
    </table>
    </div>
<?php
        }//end if has refunds
}
add_action( 'woocommerce_checkout_order_review', 'the_refunded_tems', 10 );
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
southafricanrob
  • 331
  • 3
  • 15

0 Answers0