WooCommerce - Renaming and using renamed order sta

2020-03-25 04:17发布

I've already renamed my order status 'completed' to 'paid' using this code

function wc_renaming_order_status( $order_statuses ) {
foreach ( $order_statuses as $key => $status ) {
    $new_order_statuses[ $key ] = $status;
    if ( 'wc-completed' === $key ) {
        $order_statuses['wc-completed'] = _x( 'Paid', 'Order status', 'woocommerce' );
    }
}
return $order_statuses;
}
add_filter( 'wc_order_statuses', 'wc_renaming_order_status' );

My problem is that I did a page template with a list of all my orders:

<?php   
while ( $loop->have_posts() ) : $loop->the_post();
$order_id = $loop->post->ID;
$order = new WC_Order($order_id);
?>
<tr>
<td style="text-align:left;"><?php echo $order->get_order_number(); ?></td>
<td style="text-align:left;"><?php echo $order->billing_first_name; ?> 
<?php echo $order->billing_last_name; ?></td>
<td style="text-align:left;"><?php echo $order->billing_company; ?></td>
<td style="text-align:left;"><?php echo $order->status; ?></td>
</tr>
<?php endwhile; ?>

And the $order->status still returns 'completed' instead of 'paid'.

How can I solve this problem?

Thanks

1条回答
Evening l夕情丶
2楼-- · 2020-03-25 04:53

This is normal and your this specific case you could use some additional code, creating a function to display your custom renamed status:

function custom_status($order){
    if($order->status == 'completed')
        return _x( 'Paid', 'woocommerce' );
    else
        return $order->status;
}

This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

In your template page you will use it this way:

<?php   
while ( $loop->have_posts() ) : $loop->the_post();
$order_id = $loop->post->ID;
$order = new WC_Order($order_id);
?>
<tr>
<td style="text-align:left;"><?php echo $order->get_order_number(); ?></td>
<td style="text-align:left;"><?php echo $order->billing_first_name; ?> 
<?php echo $order->billing_last_name; ?></td>
<td style="text-align:left;"><?php echo $order->billing_company; ?></td>
<td style="text-align:left;"><?php echo custom_status($order); ?></td>
</tr>

<?php endwhile; ?>

This code is tested and works.

Reference: Renaming WooCommerce Order Status

查看更多
登录 后发表回答