class-order-search-manager.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. /**
  3. * Extends WooCommerce order search to find orders by external_ref_ord_no.
  4. *
  5. * The hooks below cover both legacy (CPT) and HPOS storage modes. They make the
  6. * existing "Search orders" input also match the external reference number meta
  7. * — no additional UI is required.
  8. */
  9. defined('ABSPATH') || exit;
  10. class Order_Search_Manager {
  11. const META_KEY = 'external_ref_ord_no';
  12. public function __construct() {
  13. // Legacy CPT-backed storage: WC includes these post-meta keys in the search query.
  14. add_filter('woocommerce_shop_order_search_fields', array($this, 'add_meta_key_to_search'));
  15. // HPOS-backed storage: WC searches order meta listed here.
  16. add_filter('woocommerce_order_table_search_query_meta_keys', array($this, 'add_meta_key_to_search'));
  17. }
  18. /**
  19. * Append the external reference meta key to whichever list WC passes us.
  20. * Same callback works for both filters above.
  21. *
  22. * @param array $keys Meta keys / search fields collected by WC.
  23. * @return array
  24. */
  25. public function add_meta_key_to_search($keys) {
  26. if (!is_array($keys)) {
  27. $keys = array();
  28. }
  29. if (!in_array(self::META_KEY, $keys, true)) {
  30. $keys[] = self::META_KEY;
  31. }
  32. return $keys;
  33. }
  34. }