Last active
April 25, 2017 16:29
-
-
Save claudiulodro/d266b2164c21a212a6745ccde47c9c83 to your computer and use it in GitHub Desktop.
WC_Query demo implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* First draft of the WooCommerce CRUD search helpers. | |
*/ | |
/** | |
* Base query class intended for extending. | |
*/ | |
class WC_Object_Query { | |
public $query_vars = array(); | |
public function __construct( $args = array() ) { | |
$this->query_vars = wp_parse_args( $args, $this->get_default_query_vars() ); | |
} | |
/** | |
* Get a query var. | |
*/ | |
public function get( $query_var, $default ) { | |
if ( isset( $this->query_vars[ $query_var ] ) ) { | |
return $this->query_vars[ $query_var ]; | |
} | |
return $default; | |
} | |
/** | |
* Set a query var. | |
*/ | |
public function set( $query_var, $value ) { | |
$this->query_vars[$query_var] = $value; | |
} | |
/** | |
* Get all of the defaults for a query. | |
*/ | |
protected function get_default_query_vars() { | |
return array( | |
'status' => array( 'draft', 'pending', 'private', 'publish' ), | |
'meta_query' => array(), | |
's' => '', | |
// etc. | |
); | |
} | |
} | |
/** | |
* Product queries. | |
*/ | |
class WC_Product_Query extends WC_Query { | |
/** | |
* Add product-specific query defaults. | |
*/ | |
protected function get_default_query_vars() { | |
return array_merge( | |
parent::get_default_query_vars(), | |
array( | |
'status' => array( 'draft', 'pending', 'private', 'publish' ), | |
'type' => array_merge( array_keys( wc_get_product_types() ) ), | |
'weight' => '', | |
'downloadable' => '' | |
// etc. | |
) | |
); | |
} | |
/** | |
* Turning the query args into Product objects is left to the data store. | |
* @return array of WC_Product objects | |
*/ | |
public function get_products() { | |
return WC_Data_Store::load( 'product' )->get_products( $this->query_vars ); | |
} | |
} | |
/** | |
* Order queries. | |
*/ | |
class WC_Order_Query extends WC_Object_Query { | |
/** | |
* Add order-specific query defaults. | |
*/ | |
protected function get_default_query_vars() { | |
return array_merge( | |
parent::get_default_query_vars(), | |
array( | |
'status' => array_keys( wc_get_order_statuses() ), | |
'type' => wc_get_order_types( 'view-orders' ), | |
// etc. | |
) | |
); | |
} | |
/** | |
* Turning the query args into Order objects is left to the data store. | |
* @return array of WC_Order objects | |
*/ | |
public function get_orders() { | |
return WC_Data_Store::load( 'order' )->get_orders( $this->query_vars ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
class WC_Product_Query extends WC_Object_Query
*