Get the refund reason in Woocommerce customer refunded order notification
Updated
First you don't need __( '%s', 'woocommerce' )
as there is nothing to translate, so you don't need printf()
function either.
Since WooCommerce 3, the WC_Order_Refund
method get_refund_reason()
is deprecated and replaced by get_reason()
method.
It doesn't work because you need to get the refunded orders (they can be many for an order) from the WC_Order
Object, using the method get_refunds()
…
Try the following instead:
<?php
// Get the Order refunds (array of refunds)
$order_refunds = $order->get_refunds();
// Loop through refunded orders for the current WC_Order object
foreach( $order_refunds as $order_refund ){
// To be sure we check if that method exist and that is not empty
if( method_exists( $order_refund, 'get_reason' ) && $order_refund->get_reason() ) {
echo '<p>' . esc_html( $order_refund->get_reason() ) . '</p>';
}
}
?>
It should work.
How to edit additional content in WooCommerce emails?
You can use the 'woocommerce_email_additional_content_' . $this->id
.
Here you find a complete list of email ids.
So in your case:
woocommerce_email_additional_content_customer_refunded_order
for full refundwoocommerce_email_additional_content_customer_partially_refunded_order
for partial refund
Try this:
// edit the additional content of the "Refunded order" email
add_filter( 'woocommerce_email_additional_content_customer_refunded_order', 'custom_additional_content_customer_refunded_order', 99, 3 );
add_filter( 'woocommerce_email_additional_content_customer_partially_refunded_order', 'custom_additional_content_customer_refunded_order', 99, 3 );
function custom_additional_content_customer_refunded_order( $content, $object, $email ) {
$content = 'Your personalized additional content';
return $content;
}
The code has been tested and works. Add it to your active theme's functions.php.
Remove refund row(s) from WooCommerce order details table
When we look at /includes/class-wc-order.php in detail we see the following function is used in WooCommerce for adding the total refunds row(s).
/**
* Add total row for refunds.
*
* @param array $total_rows Total rows.
* @param string $tax_display Tax to display.
*/
protected function add_order_item_totals_refund_rows( &$total_rows, $tax_display ) {
$refunds = $this->get_refunds();
if ( $refunds ) {
foreach ( $refunds as $id => $refund ) {
$total_rows[ 'refund_' . $id ] = array(
'label' => $refund->get_reason() ? $refund->get_reason() : __( 'Refund', 'woocommerce' ) . ':',
'value' => wc_price( '-' . $refund->get_amount(), array( 'currency' => $this->get_currency() ) ),
);
}
}
}
Since an order can consist of several refunds, 'refund_' . $id
is used opposite 'refund'
So to remove it, you have to use a loop. So you get:
function filter_woocommerce_get_order_item_totals( $total_rows, $order, $tax_display ) {
// Get the Order refunds (array of refunds)
$order_refunds = $order->get_refunds();
// NOT empty
if ( ! empty( $order_refunds) ) {
// Unset
foreach ( $order_refunds as $id => $refund ) {
unset( $total_rows[ 'refund_' . $id ] );
}
}
return $total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'filter_woocommerce_get_order_item_totals', 10, 3 );
Related Topics
Simplexmlelement and Xpath, Getting Empty Array()
Moving from MySQL to MySQLi or Pdo
Xpath in Simplexml for Default Namespaces Without Needing Prefixes
PHP Domdocument - Get HTML Source of Body
Laravel Store Array in Session
How to Set Base Url for All Pages of My Website
How to Get the First Day of the Week for the Current Locale? (Php/L8N)
How to Use File_Get_Contents or File_Get_Html
How to Make a Select in PHP/MySQL Case Insensitive
Simplexml: Append One Tree to Another
How to Store Variable Values Over Multiple Page Loads
Facebook Oauthexception: "User Hasn't Authorized the Application to Perform This Action"
Php: What Is the Purpose of Session_Name
How to Select a Result from the Select2 Search Results
Fatal Error: Call to Undefined Function MySQL_Connect() in C:\Apache\Htdocs\Test.PHP on Line 2