如何通过短代码获取 WooCommerce 中最后一个订单的订单状态

发布于 2025-01-16 08:15:46 字数 1176 浏览 2 评论 0原文

我试图让我们的客户在不访问他们的帐户的情况下更好地了解他们的订单状态。我的计划是在有人订购后在主页上打印信息。

样本我想要它的外观

我正在努力让订单状态显示在其他地方。

这是我根据用于获取最后一个订单的产品的代码编写的当前代码。

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.

Sample of how I want it to look

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必在意 2025-01-23 08:15:46

$order_items = $last_order->get_status() 将返回一个字符串,因此不是一个数组。因此,使用 end( $order_items ) 是一个多余的步骤。

请改用:

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 status
    $order_status = $last_order->get_status();
    
    // Return
    return $order_status;
} 
// Register shortcode
add_shortcode( 'display_woostatus', 'woostatus' ); 

SHORTCODE USAGE

在现有页面中:

[display_woostatus]

或在 PHP 中:

echo do_shortcode('[display_woostatus]');

$order_items = $last_order->get_status() will return a string and is therefore not an array. So using end( $order_items ) is a superfluous step.

Use instead:

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 status
    $order_status = $last_order->get_status();
    
    // Return
    return $order_status;
} 
// Register shortcode
add_shortcode( 'display_woostatus', 'woostatus' ); 

SHORTCODE USAGE

In an existing page:

[display_woostatus]

Or in PHP:

echo do_shortcode('[display_woostatus]');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文