2017-08-23 180 views
2

在Woocommerce API我看到一個方法wc_get_ordersWoocommerce:獲取所有訂單產品

WooCommerce Order Functions

在提到ARGS我沒有看到一個論點,我可以提供一個產品ID或對象限制訂單僅針對該特定產品。

有沒有一種方法可以過濾僅針對特定產品的訂單?

我需要使用此方法,因爲有些參數是必要的

+0

此得到真正WC_Order對象的https:// WWW .rfmeier.net/get-all-order-for-product-in-woocommerce/ –

回答

1

編輯 此功能不存在,但它可以建造的。所以,下面的函數將返回所有訂單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; 
} 

用途1(給定產品ID 37和默認完成的訂單狀態) :

$orders_ids = get_orders_ids_by_product_id(37); 

// The output (for testing) 
print_r($orders_ids); 

USAGE 2(對於給定的產品ID 37一些定義ö rders狀態):

// 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文件),或者也可以任何插件文件。

此代碼已經過測試並可正常工作。

+0

Hello LoicTheAztec,如果我有多個元鍵值,我該如何修改sql。例如顏色>紅色?謝謝! – somtam

+0

@somtam您需要再添加一個:'LEFT JOIN {$ wpdb-> prefix} woocommerce_order_itemmeta as order_item_meta2 ON order_items.order_item_id = order_item_meta2.order_item_id' ...然後您將可以使用新的'order_item_meta2.meta_key'或/和'order_item_meta2.meta_value' ... – LoicTheAztec

+0

它完美的作品,謝謝!還有一件事。通常與get_col()我轉義sql,如$結果= $ wpdb-> get_col($ wpdb->準備($ sql,$ id));.我必須這樣做,否則get_col自行逃脫?謝謝。 – somtam

1

所以我想獲取特定產品和人的訂單。我使用@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; 

} 
相關問題