gpt4 book ai didi

php - WooCommerce - 重命名和使用重命名的订单状态

转载 作者:可可西里 更新时间:2023-10-31 23:28:20 26 4
gpt4 key购买 nike

我已经使用此代码将我的订单状态“已完成”重命名为“已付款”

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' );

我的问题是我做了一个包含我所有订单列表的页面模板:

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

并且 $order->status 仍然返回 'completed' 而不是 “付费”

我该如何解决这个问题?

谢谢

最佳答案

这是正常的,您可以使用一些额外的代码,创建一个函数来显示您的自定义重命名状态:

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

此代码已经过测试并且可以工作。

引用:Renaming WooCommerce Order Status

关于php - WooCommerce - 重命名和使用重命名的订单状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39432039/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com