如何通过短代码获取 WooCommerce 中最后一个订单的订单状态
我试图让我们的客户在不访问他们的帐户的情况下更好地了解他们的订单状态。我的计划是在有人订购后在主页上打印信息。
我正在努力让订单状态显示在其他地方。
这是我根据用于获取最后一个订单的产品的代码编写的当前代码。
function woostatus() {
// Not available
$na = __( 'N/A', 'woocommerce' );
// For logged in users only
if ( ! is_user_logged_in() ) return $na;
// The current user ID
$user_id = get_current_user_id();
// Get the WC_Customer instance Object for the current user
$customer = new WC_Customer( $user_id );
// Get the last WC_Order Object instance from current customer
$last_order = $customer->get_last_order();
// When empty
if ( empty ( $last_order ) ) return $na;
// Get order date
$order_items = $last_order->get_status();
// Latest WC_Order_Item_Product Object instance
$last_item = end( $order_items );
// Pass product ID to products shortcode
return $order_items;
}
// Register shortcode
add_shortcode( 'display_woostatus', 'woostatus' );
I'm trying to give our customers a better insight in their order status without going to their account. My plan is to print the info on the home page once someone has ordered.
I'm struggling to get the order status to display elsewhere.
Here is the current code I've whipped up based on a code used to grab the product of the last order.
function woostatus() {
// Not available
$na = __( 'N/A', 'woocommerce' );
// For logged in users only
if ( ! is_user_logged_in() ) return $na;
// The current user ID
$user_id = get_current_user_id();
// Get the WC_Customer instance Object for the current user
$customer = new WC_Customer( $user_id );
// Get the last WC_Order Object instance from current customer
$last_order = $customer->get_last_order();
// When empty
if ( empty ( $last_order ) ) return $na;
// Get order date
$order_items = $last_order->get_status();
// Latest WC_Order_Item_Product Object instance
$last_item = end( $order_items );
// Pass product ID to products shortcode
return $order_items;
}
// Register shortcode
add_shortcode( 'display_woostatus', 'woostatus' );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
$order_items = $last_order->get_status()
将返回一个字符串,因此不是一个数组。因此,使用end( $order_items )
是一个多余的步骤。请改用:
SHORTCODE USAGE
在现有页面中:
或在 PHP 中:
$order_items = $last_order->get_status()
will return a string and is therefore not an array. So usingend( $order_items )
is a superfluous step.Use instead:
SHORTCODE USAGE
In an existing page:
Or in PHP: