从 Woocommerce 中购买的产品 ID 获取最后一个订单 ID
Get last order ID from a purchased product ID in Woocommerce
如果客户购买了产品,如何获取特定产品的订单号?我尝试了下面的代码但没有用。提前感谢您的帮助。
<?php
$order = new wc_get_order( $order_id );
$order->get_id();
echo $order->get_order_number();
?>
也试过此代码但没有用。
$product_ids = array(37,53);
$order_ids = get_order_ids_from_bought_items( $product_ids );
function get_order_ids_from_bought_items() {
$prod_arr = array('37', '53');
$purchased = false;
$customer_orders = get_posts(array(
'numberposts' => -1,
'post_type' => 'shop_order',
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
));
foreach ($customer_orders as $order_post) {
$order = wc_get_order($order_post->ID);
foreach ($order->get_items() as $item) {
$product_id = $item->get_product_id();
if (in_array($product_id, $prod_arr))
$purchased = true;
}
}
return $purchased; // $order_post->ID for Order ID
}
以下函数使用一个非常轻量级的 SQL 查询,它将 return 来自给定客户的定义产品 ID 的最后一个订单 ID:
function get_last_order_id_from_product( $product_id, $user_id = 0 ) {
global $wpdb;
$customer_id = $user_id == 0 ? get_current_user_id() : $user_id;
return $wpdb->get_var( "
SELECT p.ID FROM {$wpdb->prefix}posts AS p
INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
INNER JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON p.ID = woi.order_id
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
WHERE p.post_type = 'shop_order'
AND pm.meta_key = '_customer_user'
AND pm.meta_value = $customer_id
AND woim.meta_key IN ( '_product_id', '_variation_id' )
AND woim.meta_value = $product_id
ORDER BY p.ID DESC LIMIT 1
" );
}
代码在您的活动子主题(或活动主题)的 function.php 文件中继续。已测试并有效。
If the customer has not purchased the product yet the function will return false
.
用法示例:
您将在下面设置产品 ID 37
和可选的用户 ID 153
(如果你不用前端代码,函数可以获取当前用户Id)*/
$order_id = get_last_order_id_from_product( 37, 153 );
或当前用户:
$order_id = get_last_order_id_from_product( 37 );
如果客户购买了产品,如何获取特定产品的订单号?我尝试了下面的代码但没有用。提前感谢您的帮助。
<?php
$order = new wc_get_order( $order_id );
$order->get_id();
echo $order->get_order_number();
?>
也试过此代码但没有用。
$product_ids = array(37,53);
$order_ids = get_order_ids_from_bought_items( $product_ids );
function get_order_ids_from_bought_items() {
$prod_arr = array('37', '53');
$purchased = false;
$customer_orders = get_posts(array(
'numberposts' => -1,
'post_type' => 'shop_order',
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
));
foreach ($customer_orders as $order_post) {
$order = wc_get_order($order_post->ID);
foreach ($order->get_items() as $item) {
$product_id = $item->get_product_id();
if (in_array($product_id, $prod_arr))
$purchased = true;
}
}
return $purchased; // $order_post->ID for Order ID
}
以下函数使用一个非常轻量级的 SQL 查询,它将 return 来自给定客户的定义产品 ID 的最后一个订单 ID:
function get_last_order_id_from_product( $product_id, $user_id = 0 ) {
global $wpdb;
$customer_id = $user_id == 0 ? get_current_user_id() : $user_id;
return $wpdb->get_var( "
SELECT p.ID FROM {$wpdb->prefix}posts AS p
INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
INNER JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON p.ID = woi.order_id
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
WHERE p.post_type = 'shop_order'
AND pm.meta_key = '_customer_user'
AND pm.meta_value = $customer_id
AND woim.meta_key IN ( '_product_id', '_variation_id' )
AND woim.meta_value = $product_id
ORDER BY p.ID DESC LIMIT 1
" );
}
代码在您的活动子主题(或活动主题)的 function.php 文件中继续。已测试并有效。
If the customer has not purchased the product yet the function will return
false
.
用法示例:
您将在下面设置产品 ID 37
和可选的用户 ID 153
(如果你不用前端代码,函数可以获取当前用户Id)*/
$order_id = get_last_order_id_from_product( 37, 153 );
或当前用户:
$order_id = get_last_order_id_from_product( 37 );