Just needs visibility support and cleanup

This commit is contained in:
claudiulodro 2017-07-25 10:01:24 -07:00
parent 689c5e7006
commit ec6d11bd67
2 changed files with 169 additions and 30 deletions

View File

@ -45,6 +45,8 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
'_product_attributes',
'_virtual',
'_downloadable',
'_download_limit',
'_download_expiry',
'_featured',
'_downloadable_files',
'_wc_rating_count',
@ -1257,6 +1259,19 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
}
}
public function reviews_allowed_query_where( $where, $wp_query ) {
global $wpdb;
if ( isset( $wp_query->query_vars['reviews_allowed'] ) && is_bool( $wp_query->query_vars['reviews_allowed'] ) ) {
if ( $wp_query->query_vars['reviews_allowed'] ) {
$where .= " AND $wpdb->posts.comment_status = 'open'";
} else {
$where .= " AND $wpdb->posts.comment_status = 'closed'";
}
}
return $where;
}
/**
* Get valid WP_Query args from a WC_Product_Query's query variables.
@ -1269,9 +1284,12 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
// Map query vars to ones that get_wp_query_args or WP_Query recognize.
$key_mapping = array(
'status' => 'post_status',
'page' => 'paged',
'include' => 'post__in',
'status' => 'post_status',
'page' => 'paged',
'include' => 'post__in',
'stock_quantity' => 'stock',
'average_rating' => 'wc_average_rating',
'review_count' => 'wc_review_count',
);
foreach ( $key_mapping as $query_key => $db_key ) {
if ( isset( $query_vars[ $query_key ] ) ) {
@ -1284,10 +1302,8 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
$boolean_queries = array(
'virtual',
'downloadable',
'featured',
'sold_individually',
'manage_stock',
'reviews_allowed',
);
foreach ( $boolean_queries as $boolean_query ) {
if ( isset( $query_vars[ $boolean_query ] ) && is_bool( $query_vars[ $boolean_query ] ) ) {
@ -1303,6 +1319,13 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
unset( $query_vars['sku'] );
}
// Featured needs special handling because it's stored in terms not meta.
$featured_query = '';
if ( isset( $query_vars['featured'] ) ) {
$featured_query = $query_vars['featured'];
unset( $query_vars['featured'] );
}
$wp_query_args = parent::get_wp_query_args( $query_vars );
if ( ! isset( $wp_query_args['date_query'] ) ) {
@ -1312,25 +1335,6 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
$wp_query_args['meta_query'] = array();
}
// Add the special SKU query if needed.
if ( $sku_query ) {
$wp_query_args['meta_query'][] = array(
'key' => '_sku',
'value' => $sku_query,
'compare' => 'LIKE',
);
}
// Manually build the total_sales query if needed.
// This query doesn't get auto-generated since the meta key doesn't have the underscore prefix.
if ( isset( $query_vars['total_sales'] ) && '' !== $query_vars['total_sales'] ) {
$wp_query_args['meta_query'][] = array(
'key' => 'total_sales',
'value' => absint( $query_vars['total_sales'] ),
'compare' => '=',
);
}
// Handle product types.
if ( 'variation' === $query_vars['type'] ) {
$wp_query_args['post_type'] = 'product_variation';
@ -1375,6 +1379,25 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
);
}
// Add the special SKU query if needed.
if ( $sku_query ) {
$wp_query_args['meta_query'][] = array(
'key' => '_sku',
'value' => $sku_query,
'compare' => 'LIKE',
);
}
// Manually build the total_sales query if needed.
// This query doesn't get auto-generated since the meta key doesn't have the underscore prefix.
if ( isset( $query_vars['total_sales'] ) && '' !== $query_vars['total_sales'] ) {
$wp_query_args['meta_query'][] = array(
'key' => 'total_sales',
'value' => absint( $query_vars['total_sales'] ),
'compare' => '=',
);
}
if ( ! empty( $query_vars['shipping_class'] ) ) {
$wp_query_args['tax_query'][] = array(
'taxonomy' => 'product_shipping_class',
@ -1383,6 +1406,30 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
);
}
if ( '' !== $featured_query ) {
$product_visibility_term_ids = wc_get_product_visibility_term_ids();
if ( $featured_query ) {
$wp_query_args['tax_query'][] = array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => array( $product_visibility_term_ids['featured'] ),
);
$wp_query_args['tax_query'][] = array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => array( $product_visibility_term_ids['exclude-from-catalog'] ),
'operator' => 'NOT IN',
);
} else {
$wp_query_args['tax_query'][] = array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => array( $product_visibility_term_ids['featured'] ),
'operator' => 'NOT IN',
);
}
}
$date_queries = array(
'date_created' => 'post_date',
'date_modified' => 'post_modified',
@ -1406,6 +1453,10 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
$wp_query_args['no_found_rows'] = true;
}
if ( isset( $query_vars['reviews_allowed'] ) && is_bool( $query_vars['reviews_allowed'] ) ) {
add_filter( 'posts_where', array( $this, 'reviews_allowed_query_where' ), 10, 2 );
}
return apply_filters( 'woocommerce_product_data_store_cpt_get_products_query', $wp_query_args, $query_vars, $this );
}

View File

@ -237,9 +237,10 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case {
$products = wc_get_products( array( 'return' => 'ids', 'downloadable' => false ) );
$this->assertEquals( array( $product_2->get_id() ), $products );
// TODO: Fix featured query.
//$products = wc_get_products( array( 'return' => 'ids', 'featured' => true ) );
//$this->assertEquals( array( $product_1->get_id() ), $products );
$products = wc_get_products( array( 'return' => 'ids', 'featured' => true ) );
$this->assertEquals( array( $product_1->get_id() ), $products );
$products = wc_get_products( array( 'return' => 'ids', 'featured' => false ) );
$this->assertEquals( array( $product_2->get_id() ), $products );
$products = wc_get_products( array( 'return' => 'ids', 'sold_individually' => true ) );
$this->assertEquals( array( $product_1->get_id() ), $products );
@ -250,9 +251,10 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case {
$products = wc_get_products( array( 'return' => 'ids', 'manage_stock' => true ) );
$this->assertEquals( array( $product_2->get_id() ), $products );
// TODO: Fix this to use comment status in query args
//$products = wc_get_products( array( 'return' => 'ids', 'reviews_allowed' => true ) );
//$this->assertEquals( array( $product_1->get_id() ), $products );
$products = wc_get_products( array( 'return' => 'ids', 'reviews_allowed' => true ) );
$this->assertEquals( array( $product_1->get_id() ), $products );
$products = wc_get_products( array( 'return' => 'ids', 'reviews_allowed' => false ) );
$this->assertEquals( array( $product_2->get_id() ), $products );
}
/**
@ -270,7 +272,25 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case {
* @since 3.2.0
*/
public function test_wc_get_products_stock() {
$product_1 = new WC_Product_Simple;
$product_1->set_manage_stock( true );
$product_1->set_stock_status( 'instock' );
$product_1->set_stock_quantity( 5 );
$product_1->save();
$product_2 = new WC_Product_Simple;
$product_2->set_manage_stock( true );
$product_2->set_stock_status( 'outofstock' );
$product_2->set_stock_quantity( 0 );
$product_2->save();
$products = wc_get_products( array( 'return' => 'ids', 'stock_quantity' => 5 ) );
$this->assertEquals( array( $product_1->get_id() ), $products );
$products = wc_get_products( array( 'return' => 'ids', 'stock_quantity' => 0 ) );
$this->assertEquals( array( $product_2->get_id() ), $products );
$products = wc_get_products( array( 'return' => 'ids', 'stock_status' => 'outofstock' ) );
$this->assertEquals( array( $product_2->get_id() ), $products );
}
/**
@ -279,7 +299,23 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case {
* @since 3.2.0
*/
public function test_wc_get_products_tax() {
$product_1 = new WC_Product_Simple;
$product_1->set_tax_status( 'taxable' );
$product_1->set_tax_class( 'reduced-rate' );
$product_1->save();
$product_2 = new WC_Product_Simple;
$product_2->set_tax_status( 'none' );
$product_2->set_tax_class( 'standard' );
$product_2->save();
$products = wc_get_products( array( 'return' => 'ids', 'tax_status' => 'taxable' ) );
$this->assertEquals( array( $product_1->get_id() ), $products );
$products = wc_get_products( array( 'return' => 'ids', 'tax_status' => 'none' ) );
$this->assertEquals( array( $product_2->get_id() ), $products );
$products = wc_get_products( array( 'return' => 'ids', 'tax_class' => 'reduced-rate' ) );
$this->assertEquals( array( $product_1->get_id() ), $products );
}
/**
@ -288,7 +324,21 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case {
* @since 3.2.0
*/
public function test_wc_get_products_shipping_class() {
$shipping_class_1 = wp_insert_term( 'Bulky', 'product_shipping_class' );
$shipping_class_2 = wp_insert_term( 'Standard', 'product_shipping_class' );
$product_1 = new WC_Product_Simple;
$product_1->set_shipping_class_id( $shipping_class_1['term_id'] );
$product_1->save();
$product_2 = new WC_Product_Simple;
$product_2->set_shipping_class_id( $shipping_class_2['term_id'] );
$product_2->save();
$products = wc_get_products( array( 'return' => 'ids', 'shipping_class' => 'bulky' ) );
$this->assertEquals( array( $product_1->get_id() ), $products );
$products = wc_get_products( array( 'return' => 'ids', 'shipping_class' => 'standard' ) );
$this->assertEquals( array( $product_2->get_id() ), $products );
}
/**
@ -297,7 +347,27 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case {
* @since 3.2.0
*/
public function test_wc_get_products_download() {
$product_1 = new WC_Product_Simple;
$product_1->set_downloadable( true );
$product_1->set_download_limit( 5 );
$product_1->set_download_expiry( 90 );
$product_1->save();
$product_2 = new WC_Product_Simple;
$product_2->set_downloadable( true );
$product_2->set_download_limit( -1 );
$product_2->set_download_expiry( -1 );
$product_2->save();
$products = wc_get_products( array( 'return' => 'ids', 'download_limit' => 5 ) );
$this->assertEquals( array( $product_1->get_id() ), $products );
$products = wc_get_products( array( 'return' => 'ids', 'download_limit' => -1 ) );
$this->assertEquals( array( $product_2->get_id() ), $products );
$products = wc_get_products( array( 'return' => 'ids', 'download_expiry' => 90 ) );
$this->assertEquals( array( $product_1->get_id() ), $products );
$products = wc_get_products( array( 'return' => 'ids', 'download_expiry' => -1 ) );
$this->assertEquals( array( $product_2->get_id() ), $products );
}
/**
@ -306,7 +376,25 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case {
* @since 3.2.0
*/
public function test_wc_get_products_reviews() {
$product_1 = new WC_Product_Simple;
$product_1->set_average_rating( 5.0 );
$product_1->set_review_count( 5 );
$product_1->save();
$product_2 = new WC_Product_Simple;
$product_2->set_average_rating( 3.0 );
$product_2->set_review_count( 1 );
$product_2->save();
$products = wc_get_products( array( 'return' => 'ids', 'average_rating' => 5.0 ) );
$this->assertEquals( array( $product_1->get_id() ), $products );
$products = wc_get_products( array( 'return' => 'ids', 'average_rating' => 3.0 ) );
$this->assertEquals( array( $product_2->get_id() ), $products );
$products = wc_get_products( array( 'return' => 'ids', 'review_count' => 5 ) );
$this->assertEquals( array( $product_1->get_id() ), $products );
$products = wc_get_products( array( 'return' => 'ids', 'review_count' => 1 ) );
$this->assertEquals( array( $product_2->get_id() ), $products );
}
/**