WooCommerce注文を検索する
WooCommerceの注文には多くの情報が含まれており、効果的に検索するのが難しい場合があります。SearchWPを設定して、WooCommerceの注文をすばやく検索できるようにします。
注意: このプロセスには、SearchWPのWooCommerce統合拡張機能がアクティブである必要があります。
ステップ1: WooCommerce注文を検索可能にする
デフォルトでは、SearchWPはWooCommerce注文をソースとして一覧表示しません。これは、その特定のコンテンツタイプが設計上publicではないためです。WooCommerceの注文には機密情報が含まれており、このような変更を行う際には特別な注意が必要です!
以下のフックを使用して、WooCommerce注文をSearchWPソースとして追加します。
All hooks should be added to your custom SearchWP Customizations Plugin.
| <?php | |
| // Add WooCommerce Orders as a SearchWP Source. | |
| // @link https://searchwp.com/documentation/knowledge-base/search-woocommerce-orders/ | |
| add_action( 'plugins_loaded', function() { | |
| if ( class_exists( 'WooCommerce' ) ) { | |
| add_filter( 'searchwp\sources', function( $sources ) { | |
| $sources[] = new \SearchWP\Sources\Post( 'shop_order' ); | |
| return $sources; | |
| } ); | |
| add_filter( 'searchwp\post_stati\shop_order', function( $post_stati ) { | |
| return array_merge( $post_stati, [ | |
| 'wc-pending', | |
| 'wc-processing', | |
| 'wc-on-hold', | |
| 'wc-completed', | |
| 'wc-cancelled', | |
| 'wc-refunded', | |
| 'wc-failed', | |
| ] ); | |
| } ); | |
| } | |
| } ); |
ステップ2: 管理画面検索用の補足エンジンを作成する
次に、管理画面検索に使用する補足SearchWPエンジンを作成します。
注意: このエンジンは、設定でチェックボックスがオンになっているため、管理画面のすべての検索に使用されます。このエンジンからソースを省略すると、SearchWPはそのソースの管理画面検索をスキップし、WordPressのデフォルトに戻ります。
この管理画面検索エンジンを作成するために実行された手順は次のとおりです。
- SearchWP設定画面の「エンジン」タブにある新規追加ボタンをクリックします
- 新しく作成されたエンジンのソースと設定ボタンをクリックします
- このエンジンの該当するソースを有効にします(注文を含めるように注意してください)
- 重要: 管理画面エンジンチェックボックスをオンにします
- エンジンの保存ボタンをクリックして、新しいエンジンを保存します
WooCommerce注文を検索する
インデックスが既存のWooCommerce注文を処理すると、それらはWordPress管理画面で検索できるようになります!
注意: WooCommerce注文には、さらに統合が必要な側面がいくつかあります。
注文アイテムデータを検索可能にする
注文アイテムデータ(例: 注文アイテムの製品SKU番号)を検索可能にしたい場合は、各注文のインデックス付けされるデータをカスタマイズして、必要な注文アイテムデータを含めることができます。このスニペットでは、SKU、製品名、製品スラッグ、製品説明、製品短い説明、および製品属性を検索可能にして、一致する注文を返します。
All hooks should be added to your custom SearchWP Customizations Plugin.
| <?php | |
| // Add WooCommerce Order item data to WooCommerce Orders Search in SearchWP. | |
| // @link https://searchwp.com/documentation/knowledge-base/search-woocommerce-orders/ | |
| add_filter( 'searchwp\entry\data', function( $data, \SearchWP\Entry $entry ) { | |
| // The Product data keys to index for each Order item. | |
| $data_to_index = [ | |
| 'sku', | |
| 'name', | |
| 'slug', | |
| 'description', | |
| 'short_description', | |
| 'attributes', | |
| ]; | |
| if ( | |
| 'post' . SEARCHWP_SEPARATOR . 'shop_order' !== $entry->get_source()->get_name() | |
| || ! function_exists( 'wc_get_order' ) | |
| || ! class_exists( 'WC_Order_Item_Product' ) | |
| || ! method_exists( 'WC_Order_Item_Product', 'get_product' ) | |
| || ! class_exists( 'WC_Product' ) | |
| || ! method_exists( 'WC_Product', 'get_data' ) | |
| ) { | |
| return $data; | |
| } | |
| $order = wc_get_order( $entry->get_id() ); | |
| $data['meta']['searchwp_wc_order_item_data'] = array_map( function ( $item ) use ( $data_to_index ) { | |
| $product = $item->get_product(); | |
| $product_data = $product->get_data(); | |
| return array_filter( array_map( function( $item_key, $item_value ) use ( $data_to_index ) { | |
| return in_array( $item_key, $data_to_index ) ? $item_value : ''; | |
| }, array_keys( $product_data ), array_values( $product_data ) ) ); | |
| }, $order->get_items() ); | |
| // We need to prevent WooCommerce from excluding the order comments from our query. | |
| remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ) ); | |
| $order_comments = get_comments( array( | |
| 'post_id' => $order->get_id(), | |
| 'orderby' => 'comment_ID', | |
| 'order' => 'DESC', | |
| 'status' => 'any', | |
| 'type' => 'order_note', | |
| ) ); | |
| add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ) ); | |
| $notes = wp_list_pluck( $order_comments, 'comment_content' ); | |
| $data['meta']['searchwp_wc_order_notes'] = $notes; | |
| return $data; | |
| }, 20, 2 ); | |
| // Prevent WooCommerce from limiting the results to post IDs. | |
| add_filter( 'searchwp\native\args', function( $args, $query ) { | |
| if ( | |
| is_admin() | |
| && is_search() | |
| && isset( $_REQUEST['post_type'] ) | |
| && 'shop_order' == $_REQUEST['post_type'] | |
| ) { | |
| $args['post__in'] = []; | |
| } | |
| return $args; | |
| }, 10, 2 ); |
注意: 作成した管理画面エンジンに「任意のメタキー」を追加したため、注文アイテムデータも検索できるようになります。
注文フィルターとの統合
デフォルトでは、SearchWPは設定されている管理画面フィルターと統合されません。このフックを使用して、WooCommerceの注文日および顧客フィルターのサポートを構築できます。
All hooks should be added to your custom SearchWP Customizations Plugin.
| <?php | |
| // @link https://searchwp.com/documentation/knowledge-base/search-woocommerce-orders/ | |
| // Add support for WooCommerce Admin filters when searching Orders with SearchWP. | |
| add_filter( 'searchwp\query\mods', function( $mods, $query ) { | |
| global $wpdb; | |
| if ( isset( $_GET['_customer_user'] ) && ! empty( $_GET['_customer_user'] ) ) { | |
| $mod = new \SearchWP\Mod( \SearchWP\Utils::get_post_type_source_name( 'shop_order' ) ); | |
| $mod->set_local_table( $wpdb->postmeta ); | |
| $mod->on( 'post_id', [ 'column' => 'id' ] ); | |
| $mod->on( 'meta_key', [ 'value' => '_customer_user' ] ); | |
| $mod->raw_where_sql( function( $runtime_mod ) use ( $wpdb ) { | |
| return $wpdb->prepare( "{$runtime_mod->get_local_table_alias()}.meta_value = %d", absint( $_GET['_customer_user'] ) ); | |
| } ); | |
| $mods[] = $mod; | |
| } | |
| if ( isset( $_GET['m'] ) && ! empty( $_GET['m'] ) ) { | |
| $mod = new \SearchWP\Mod( \SearchWP\Utils::get_post_type_source_name( 'shop_order' ) ); | |
| $mod->raw_where_sql( function( $runtime_mod ) use ( $wpdb ) { | |
| return $wpdb->prepare( "DATE_FORMAT( {$runtime_mod->get_local_table_alias()}.post_date, \"%Y%m\" ) = %d", absint( $_GET['m'] ) ); | |
| } ); | |
| $mods[] = $mod; | |
| } | |
| return $mods; | |
| }, 99, 2 ); |
このフックを配置すると、SearchWPはWooCommerce注文を検索する際に、注文日および顧客フィルターによって定義された制限を適用します。
注文番号を検索可能にする
注文番号は投稿IDであり、デフォルトではインデックス付けされません。このスニペットのようなもので、SearchWPに他の注文データとともにWooCommerce注文番号をインデックス付けするように指示できます。
All hooks should be added to your custom SearchWP Customizations Plugin.
| <?php | |
| // Add WooCommerce Order Numbers to WooCommerce Orders. | |
| // @link https://searchwp.com/documentation/knowledge-base/search-woocommerce-orders/ | |
| add_filter( 'searchwp\entry\data', function( $data, \SearchWP\Entry $entry ) { | |
| $my_extra_meta_key = 'searchwp_order_number'; | |
| $my_extra_meta = $entry->get_id() . ' order-' . $entry->get_id() . ' o-' . $entry->get_id(); | |
| $data['meta'][ $my_extra_meta_key ] = $my_extra_meta; | |
| return $data; | |
| }, 20, 2 ); |
注意: 作成した管理画面エンジンに「任意のメタキー」を追加したため、注文番号も検索できるようになります。
注文メモを検索可能にする
思い出していただければ、上記のように管理エンジンに「コメント」を追加しました。WooCommerceはコメントを注文メモに使用しますが、注文メモのプライベートな性質によりWooCommerceによって追加の制限が設けられています。この制限は、次のようなスニペットを使用してSearchWPに対して削除できます。
All hooks should be added to your custom SearchWP Customizations Plugin.
| <?php | |
| // Make WooCommerce Order Notes searchable by SearchWP. | |
| // @link https://searchwp.com/documentation/knowledge-base/search-woocommerce-orders/ | |
| add_action( 'searchwp\source\post\attributes\comments', function() { | |
| if ( did_action( 'searchwp\indexer\batch' ) ) { | |
| remove_filter( 'comments_clauses', [ 'WC_Comments', 'exclude_order_comments' ] ); | |
| } | |
| } ); | |
| // Add Order Notes to SearchWP's Comments Source. | |
| add_filter( 'searchwp\source\comment\db_where', function( $db_where, $source ) { | |
| $db_where[0]['value'] = [ 'comment', 'order_note', ]; | |
| $db_where[0]['compare'] = 'IN'; | |
| return $db_where; | |
| }, 10, 2 ); |
このスニペットを追加したら、SearchWP設定画面のエンジンのタブにあるインデックスの再構築ボタンをクリックしてインデックスを再構築する必要があります。インデックスが再構築されると、WooCommerceの注文メモも検索可能になります。


