首页 > 解决方案 > 带有条件的订单明细表下的文本

问题描述

我想在我的帐户页面的订单详细信息表下添加一个文本,如下面的屏幕截图所示,但如果订单状态更改为完成,我希望将此文本更改为另一个文本。

这是我用来显示文本的内容,但如果状态从任何状态更改为完成,我不能有条件地将文本更改为另一个文本。

add_action('woocommerce_order_details_after_order_table', 'action_order_details_after_order_table', 10, 4 );
function action_order_details_after_order_table( $order, $sent_to_admin = '', $plain_text = '', $email = '' ) {
    // Only on "My Account" > "Order View"
    if ( is_wc_endpoint_url( 'view-order' ) ) {
        printf( '<p class="custom-text">' .
        __("To cancel your license within the 30 day trial period click on %s" ,"woocommerce"),
        '<strong>"' .__("Refund my entire order", "woocommerce") . '"</strong>.</p>' );
    }
}

我只需要在订单状态完成后将此消息更改为另一条消息。

标签: phpwordpresswoocommerce

解决方案


Change your above codes with follows -

add_action('woocommerce_order_details_after_order_table', 'action_order_details_after_order_table', 10, 4 );
function action_order_details_after_order_table( $order, $sent_to_admin = '', $plain_text = '', $email = '' ) {
    // Only on "My Account" > "Order View"
    if ( is_wc_endpoint_url( 'view-order' ) ) {
        if( $order->get_status() == 'completed' ){ // for completed status
            printf( '<p class="custom-text">' .
        __("Your order completed message goes here %s" ,"woocommerce"),
        '<strong>"' .__("Refund my entire order", "woocommerce") . '"</strong>.</p>' );
        }elseif($order->get_status() == 'cancelled'){ // for cancelled status
           printf( '<p class="custom-text">' .
        __("Your order cancelled message goes here %s" ,"woocommerce"),
        '<strong>"' .__("Refund my entire order", "woocommerce") . '"</strong>.</p>' );
        }else{ // for rest statuses
            printf( '<p class="custom-text">' .
        __("To cancel your license within the 30 day trial period click on %s" ,"woocommerce"),
        '<strong>"' .__("Refund my entire order", "woocommerce") . '"</strong>.</p>' );
        }
    }
}

推荐阅读