本篇文章更新時間:2023/05/30
如有資訊過時或語誤之處,歡迎使用 Contact 功能通知或向一介資男的 LINE 社群反應。
如果本站內容對你有幫助,歡迎贊助支持


預設使用 WP_List_Table 建立的列表清單,搜尋功能並不是最完整。

WooCommerce 這邊還有提供一個事件過濾器的 Hook 來強化搜尋的結果: woocommerce_shop_order_search_results

使用這個 Hook 來加入貨號(SKU)條件的方法如下:

function mxp_order_search_by_sku($order_ids, $term, $search_fields) {
    global $wpdb;
    if (!empty($term)) {
        // 確認有沒有這個商品,沒有就跳開了
        $product_id = wc_get_product_id_by_sku($wpdb->esc_like(wc_clean($term)));
        if (!$product_id) {
            return $order_ids;
        }
        // 找找看關聯資料,輸出不重複的訂單編號
        $order_ids = array_unique(
            $wpdb->get_col(
                $wpdb->prepare("SELECT order_id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key IN ( '_product_id', '_variation_id' ) AND meta_value = %d ) AND order_item_type = 'line_item'", $product_id)
            )
        );
    }
    return $order_ids;
}
add_filter('woocommerce_shop_order_search_results', 'mxp_order_search_by_sku', 999, 3);

這樣如果要簡單調出某個產品銷售訂單的情況,這方法就還不錯用了!


Share:

作者: Chun

WordPress 社群貢獻者、開源社群推廣者。專注於 WordPress 外掛開發、網站效能最佳化、伺服器管理,以及 iDempiere 開源 ERP 導入與客製開發。曾參與 WordCamp Taipei 等社群活動,GitHub Arctic Code Vault Contributor。提供資訊顧問、WordPress 開發教學、主機最佳化與企業 ERP 整合服務。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *


文章
Filter
Apply Filters
Mastodon