WooCommerce - rename and use renamed order status

I already renamed the order status "completed" to "paid" with 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 created 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 still returns instead . $order->status 'completed' 'paid'

How can I solve this problem?

thank

+4
source share
1 answer

This is normal, and in this particular case, you can use additional code by creating a function to display your custom renamed state:

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

function.php ( ), .

:

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

.

: WooCommerce

+2

Source: https://habr.com/ru/post/1654241/


All Articles