Woocommerce:获取产品的所有订单
Woocommerce: Get all orders for a product
在 Woocommerce API 我看到一个方法 wc_get_orders
在提到的参数中,我没有看到可以提供产品 ID 或对象来限制该特定产品的订单结果的参数。
有什么方法可以过滤特定产品的订单吗?
我需要使用这个方法,因为需要一些参数
已编辑
该函数不存在,但可以构建。因此下面的函数将 return 给定产品 ID 的所有订单 ID 的数组,进行正确的 SQL 查询:
/**
* Get All orders IDs for a given product ID.
*
* @param integer $product_id (required)
* @param array $order_status (optional) Default is 'wc-completed'
*
* @return array
*/
function get_orders_ids_by_product_id( $product_id, $order_status = array( 'wc-completed' ) ){
global $wpdb;
$results = $wpdb->get_col("
SELECT order_items.order_id
FROM {$wpdb->prefix}woocommerce_order_items as order_items
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id
LEFT JOIN {$wpdb->posts} AS posts ON order_items.order_id = posts.ID
WHERE posts.post_type = 'shop_order'
AND posts.post_status IN ( '" . implode( "','", $order_status ) . "' )
AND order_items.order_item_type = 'line_item'
AND order_item_meta.meta_key = '_product_id'
AND order_item_meta.meta_value = '$product_id'
");
return $results;
}
USAGE 1(对于给定的产品 ID 37
和默认的已完成订单状态):
$orders_ids = get_orders_ids_by_product_id( 37 );
// The output (for testing)
print_r( $orders_ids );
USAGE 2(对于给定的产品 ID 37
和 一些定义的订单状态):
// Set the orders statuses
$statuses = array( 'wc-completed', 'wc-processing', 'wc-on-hold' );
$orders_ids = get_orders_ids_by_product_id( 37, $statuses );
// The output (for testing)
print_r( $orders_ids );
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已经过测试并且有效。
所以我想获取特定于产品和人的订单。我使用@LoicTheActec 提供的解决方案和 wc_get_orders woocommerce 方法来获得我想要的结果。
函数如下:
/**
* Get All orders IDs for a given product ID.
*
* @param integer $product_id (required)
* @param array $order_status (optional) Default is 'wc-completed'
*
* @return array
*/
public function get_orders_ids_by_product_id( $product_id, $order_status = array( 'wc-completed' ) ) {
global $wpdb;
$results = $wpdb->get_col("
SELECT order_items.order_id
FROM {$wpdb->prefix}woocommerce_order_items as order_items
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id
LEFT JOIN {$wpdb->posts} AS posts ON order_items.order_id = posts.ID
WHERE posts.post_type = 'shop_order'
AND posts.post_status IN ( '" . implode( "','", $order_status ) . "' )
AND order_items.order_item_type = 'line_item'
AND order_item_meta.meta_key = '_product_id'
AND order_item_meta.meta_value = '$product_id'
");
return $results;
}
过滤出我想要的结果。由于该方法不提供包含参数,我必须获取所有订单,然后删除给定产品 ID 的订单,并从 wc_get_orders:
中排除所有剩余订单
/**
* Check if current user has more than 3 orders
*
* @return void
* @author
**/
public function woocommerce_check_order_count($product_id, $customer_email)
{
$statuses = array( 'wc-processing' );
$orders_ids_for_product = $this->get_orders_ids_by_product_id( $product_id, $statuses );
// Get All Orders
$allorders = wc_get_orders( array(
'status' => 'processing',
'type' => 'shop_order',
'email' => '',
'limit' => -1,
'return' => 'ids',
) );
// Remove the $orders_ids_for_product array from the $allorders array
$orderstoexclude = array_diff($allorders, $orders_ids_for_product);
$orders = wc_get_orders( array(
'status' => 'processing',
'type' => 'shop_order',
'customer' => $customer_email,
'email' => '',
'limit' => 3,
'exclude' => $orderstoexclude,
'return' => 'ids',
) );
return $orders;
}
在 Woocommerce API 我看到一个方法 wc_get_orders
在提到的参数中,我没有看到可以提供产品 ID 或对象来限制该特定产品的订单结果的参数。
有什么方法可以过滤特定产品的订单吗?
我需要使用这个方法,因为需要一些参数
已编辑 该函数不存在,但可以构建。因此下面的函数将 return 给定产品 ID 的所有订单 ID 的数组,进行正确的 SQL 查询:
/**
* Get All orders IDs for a given product ID.
*
* @param integer $product_id (required)
* @param array $order_status (optional) Default is 'wc-completed'
*
* @return array
*/
function get_orders_ids_by_product_id( $product_id, $order_status = array( 'wc-completed' ) ){
global $wpdb;
$results = $wpdb->get_col("
SELECT order_items.order_id
FROM {$wpdb->prefix}woocommerce_order_items as order_items
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id
LEFT JOIN {$wpdb->posts} AS posts ON order_items.order_id = posts.ID
WHERE posts.post_type = 'shop_order'
AND posts.post_status IN ( '" . implode( "','", $order_status ) . "' )
AND order_items.order_item_type = 'line_item'
AND order_item_meta.meta_key = '_product_id'
AND order_item_meta.meta_value = '$product_id'
");
return $results;
}
USAGE 1(对于给定的产品 ID 37
和默认的已完成订单状态):
$orders_ids = get_orders_ids_by_product_id( 37 );
// The output (for testing)
print_r( $orders_ids );
USAGE 2(对于给定的产品 ID 37
和 一些定义的订单状态):
// Set the orders statuses
$statuses = array( 'wc-completed', 'wc-processing', 'wc-on-hold' );
$orders_ids = get_orders_ids_by_product_id( 37, $statuses );
// The output (for testing)
print_r( $orders_ids );
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已经过测试并且有效。
所以我想获取特定于产品和人的订单。我使用@LoicTheActec 提供的解决方案和 wc_get_orders woocommerce 方法来获得我想要的结果。
函数如下:
/**
* Get All orders IDs for a given product ID.
*
* @param integer $product_id (required)
* @param array $order_status (optional) Default is 'wc-completed'
*
* @return array
*/
public function get_orders_ids_by_product_id( $product_id, $order_status = array( 'wc-completed' ) ) {
global $wpdb;
$results = $wpdb->get_col("
SELECT order_items.order_id
FROM {$wpdb->prefix}woocommerce_order_items as order_items
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id
LEFT JOIN {$wpdb->posts} AS posts ON order_items.order_id = posts.ID
WHERE posts.post_type = 'shop_order'
AND posts.post_status IN ( '" . implode( "','", $order_status ) . "' )
AND order_items.order_item_type = 'line_item'
AND order_item_meta.meta_key = '_product_id'
AND order_item_meta.meta_value = '$product_id'
");
return $results;
}
过滤出我想要的结果。由于该方法不提供包含参数,我必须获取所有订单,然后删除给定产品 ID 的订单,并从 wc_get_orders:
中排除所有剩余订单 /**
* Check if current user has more than 3 orders
*
* @return void
* @author
**/
public function woocommerce_check_order_count($product_id, $customer_email)
{
$statuses = array( 'wc-processing' );
$orders_ids_for_product = $this->get_orders_ids_by_product_id( $product_id, $statuses );
// Get All Orders
$allorders = wc_get_orders( array(
'status' => 'processing',
'type' => 'shop_order',
'email' => '',
'limit' => -1,
'return' => 'ids',
) );
// Remove the $orders_ids_for_product array from the $allorders array
$orderstoexclude = array_diff($allorders, $orders_ids_for_product);
$orders = wc_get_orders( array(
'status' => 'processing',
'type' => 'shop_order',
'customer' => $customer_email,
'email' => '',
'limit' => 3,
'exclude' => $orderstoexclude,
'return' => 'ids',
) );
return $orders;
}