WooCommerce - Renaming and using renamed order status

旧巷老猫 提交于 2019-12-01 05:27:14

问题


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:


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



来源:https://stackoverflow.com/questions/39432039/woocommerce-renaming-and-using-renamed-order-status

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!