我正在尋找一種在另一個頁面上顯示最后訂購的產品的方法。
我認為有可能在函式中創建一個短代碼來獲取訂單詳細資訊并在我添加短代碼的任何地方顯示它們。

但我似乎無法弄清楚如何讓它作業。到目前為止,我得到了以下資訊:
add_shortcode( 'displaylast', 'last' );
function last(){
$customer_id = get_current_user_id();
$order = wc_get_customer_last_order( $customer_id );
return $order->get_order();
}
[displaylast]目前正在顯示我注意到。當我更改get_order()為get_billing_first_name().
這將顯示訂單名稱。但是我好像拿不到買的東西。也許有一個get_()我沒有看到?
uj5u.com熱心網友回復:
您已經接近了,但是您必須從訂單物件中獲取最后一個產品。
所以你得到:
function last() {
// 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 items
$order_items = $last_order->get_items();
// Latest WC_Order_Item_Product Object instance
$last_item = end( $order_items );
// Get product ID
$product_id = $last_item->get_variation_id() > 0 ? $last_item->get_variation_id() : $last_item->get_product_id();
// Pass product ID to products shortcode
return do_shortcode("[product id='$product_id']");
}
// Register shortcode
add_shortcode( 'display_last', 'last' );
短代碼用法
在現有頁面中:
[display_last]
或者在 PHP 中:
echo do_shortcode('[display_last]');
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/397651.html
標籤:WordPress的 求购 产品 短代码 命令
