| @@ -36,9 +36,13 @@ | ||
| 36 | 36 | */ |
| 37 | 37 | public function __construct() { |
| 38 | 38 | $pos_only_products = Settings::instance()->pos_only_products_enabled(); |
| 39 | 39 | |
| 40 | - if ( $pos_only_products ) { | |
| 40 | + // `wp wc product list` dispatches these same routes. A WP-CLI command is the | |
| 41 | + // merchant operating the shop, like wp-admin, so it lists every product. | |
| 42 | + $is_cli = \defined( 'WP_CLI' ) && WP_CLI; | |
| 43 | + | |
| 44 | + if ( $pos_only_products && ! $is_cli ) { | |
| 41 | 45 | add_filter( 'rest_pre_dispatch', array( $this, 'set_woocommerce_rest_api_request_flags' ), 10, 3 ); |
| 42 | 46 | add_filter( 'posts_where', array( $this, 'exclude_pos_only_products_from_api_response' ), 10, 2 ); |
| 43 | 47 | } |
| 44 | 48 | } |
| @@ -54,8 +58,14 @@ | ||
| 54 | 58 | // WordPress matches REST routes case-insensitively, so a mixed-case path |
| 55 | 59 | // still dispatches to the products controller — flag detection must not |
| 56 | 60 | // be skippable by upper-casing the route. |
| 57 | 61 | $route = strtolower( $request->get_route() ); |
| 62 | + | |
| 63 | + // The flags describe THIS dispatch. A `/batch/v1` request dispatches its | |
| 64 | + // sub-requests one after another through this same filter, so a products | |
| 65 | + // sub-request must not leave the flags set for the one that follows it. | |
| 66 | + $this->is_woocommerce_rest_api_products_request = false; | |
| 67 | + $this->is_woocommerce_rest_api_variations_request = false; | |
| 58 | 68 | |
| 59 | 69 | if ( 0 === strpos( $route, '/wc/v3/products' ) || 0 === strpos( $route, '/wc/v2/products' ) || 0 === strpos( $route, '/wc/v1/products' ) ) { |
| 60 | 70 | $this->is_woocommerce_rest_api_products_request = true; |
| 61 | 71 | |