title: Don't change order total when refunding items in Woocommerce
body: <p>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 <a href="https://stackoverflow.com/questions/52490697/get-refunded-orders-and-refunded-order-items-details-in-woocommerce-3/52491796#52491796">this answer</a> and <a href="https://www.businessbloomer.com/woocommerce-update-user-meta-successful-order/" rel="nofollow noreferrer">here</a></p>
  <p>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.</p>
  <p><strong>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?</strong></p>
  <p>Here is my code so far:</p>
  <pre><code>//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() &amp;&amp; ! defined( 'DOING_AJAX' ) )
          return;
  
      $user_id = get_current_user_id(); // The current user ID
      $customer = new WC_Customer( $user_id );
      $last_order = $customer-&gt;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-&gt;get_refunds();
      $total_to_refund = $order-&gt;get_total_refunded()*(-1);//WIP need to check which items have tax
      
      if (!empty($order_refunds)) WC()-&gt;cart-&gt;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-&gt;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-&gt;get_refunds();
      if ($order_refunds){
      ?&gt;
      &lt;div class=&quot;refund-checkout-table&quot;&gt;
          &lt;h4&gt;Refunds from previous order:&lt;/h4&gt;
          &lt;table class=&quot;refunded-items-at-checkout&quot;&gt;
              &lt;tbody&gt;
              &lt;?php
              // Loop through the order refunds array
              foreach( $order_refunds as $refund ){
                  // Loop through the order refund line items
                  foreach( $refund-&gt;get_items() as $item_id =&gt; $item ){
  
                      ## --- Using WC_Order_Item_Product methods --- ##
                      $refunded_quantity      = $item-&gt;get_quantity(); // Quantity: zero or negative integer
                      $refunded_name         = $item-&gt;get_name(); // Quantity: zero or negative integer
                      $refunded_line_subtotal = $item-&gt;get_subtotal(); // line subtotal: zero or negative number
                      $refunded_line_tax = $item-&gt;get_subtotal_tax();
  
                      // Get the original refunded item ID
                      $refunded_item_id       = $item-&gt;get_meta('_refunded_item_id'); // line subtotal: zero or negative number
                      $total_to_refund = $order-&gt;get_total_refunded();
                  ?&gt;    
                      &lt;tr class=&quot;&quot;&gt;
                          &lt;td class=&quot;checkout-refund-product-name&quot;&gt;
                              &lt;?php echo $refunded_name . '&amp;nbsp;';  ?&gt;
                              &lt;?php echo  '&amp;times;&amp;nbsp;'. ($refunded_quantity*-1) ;?&gt;
                          &lt;/td&gt;
                          &lt;td class=&quot;checkout-refund-product-total&quot;&gt;
                              &lt;?php echo  round((($refunded_line_subtotal+$refunded_line_tax)*-1),2); ?&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;?php
                  }
              }
          ?&gt;  
          &lt;/tbody&gt;
      &lt;/table&gt;
      &lt;/div&gt;
  &lt;?php
          }//end if has refunds
  }
  add_action( 'woocommerce_checkout_order_review', 'the_refunded_tems', 10 );
  </code></pre>