在 Woocommerce 客户退款订单通知中获取退款原因

Get the refund reason in Woocommerce customer refunded order notification

我想在 woocommerce 的退款邮件中显示退款原因。我直接在 customer-refunded-order.php 中编辑,它被复制到我的子主题中。

我在 crud 对象中看到我可以从那里到达 refund_reson

https://github.com/woocommerce/woocommerce/wiki/Order-and-Order-Line-Item-Data#refund

<?php printf( __( '%s', 'woocommerce' ), $order->get_refund_reason() ); ?>

我只是想在电子邮件中看到退款原因,但是这段代码打断了流程(当我开始退款时,页面永远加载并且不刷新,因为它卡在了电子邮件创建过程中).

已更新

首先你不需要__( '%s', 'woocommerce' )因为没有什么要翻译的,所以你也不需要printf()函数。

从 WooCommerce 3 开始,WC_Order_Refund 方法 get_refund_reason() is deprecated and replaced by get_reason() 方法。

它不起作用,因为您需要使用方法 get_refunds()

WC_Order 对象中获取已退款的订单(一个订单可能有很多)

请尝试以下操作:

<?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>';
    }
}
?>

应该可以。