virtual and downloadable

This commit is contained in:
Mike Jolley 2019-03-07 14:07:51 +00:00
parent 94b19dc3ca
commit 1ab455ec09
4 changed files with 61 additions and 5 deletions

View File

@ -442,6 +442,7 @@ class WC_Admin_List_Table_Products extends WC_Admin_List_Table {
* @return array
*/
protected function query_filters( $query_vars ) {
// Custom order by arguments.
if ( isset( $query_vars['orderby'] ) ) {
$orderby = strtolower( $query_vars['orderby'] );
$order = isset( $query_vars['order'] ) ? strtoupper( $query_vars['order'] ) : 'DESC';
@ -457,15 +458,14 @@ class WC_Admin_List_Table_Products extends WC_Admin_List_Table {
}
}
// Type filtering.
if ( isset( $query_vars['product_type'] ) ) {
if ( 'downloadable' === $query_vars['product_type'] ) {
$query_vars['product_type'] = '';
$query_vars['meta_value'] = 'yes'; // phpcs:ignore WordPress.VIP.SlowDBQuery.slow_db_query_meta_value
$query_vars['meta_key'] = '_downloadable'; // phpcs:ignore WordPress.VIP.SlowDBQuery.slow_db_query_meta_key
add_filter( 'posts_clauses', array( $this, 'filter_downloadable_post_clauses' ) );
} elseif ( 'virtual' === $query_vars['product_type'] ) {
$query_vars['product_type'] = '';
$query_vars['meta_value'] = 'yes'; // phpcs:ignore WordPress.VIP.SlowDBQuery.slow_db_query_meta_value
$query_vars['meta_key'] = '_virtual'; // phpcs:ignore WordPress.VIP.SlowDBQuery.slow_db_query_meta_key
add_filter( 'posts_clauses', array( $this, 'filter_virtual_post_clauses' ) );
}
}
@ -524,6 +524,8 @@ class WC_Admin_List_Table_Products extends WC_Admin_List_Table {
remove_filter( 'posts_clauses', array( $this, 'order_by_price_desc_post_clauses' ) );
remove_filter( 'posts_clauses', array( $this, 'order_by_sku_asc_post_clauses' ) );
remove_filter( 'posts_clauses', array( $this, 'order_by_sku_desc_post_clauses' ) );
remove_filter( 'posts_clauses', array( $this, 'filter_downloadable_post_clauses' ) );
remove_filter( 'posts_clauses', array( $this, 'filter_virtual_post_clauses' ) );
return $posts;
}
@ -575,6 +577,30 @@ class WC_Admin_List_Table_Products extends WC_Admin_List_Table {
return $args;
}
/**
* Filter by type.
*
* @param array $args Query args.
* @return array
*/
public function filter_downloadable_post_clauses( $args ) {
$args['join'] = $this->append_product_sorting_table_join( $args['join'] );
$args['where'] = ' AND wc_product_meta_lookup.downloadable=1 ';
return $args;
}
/**
* Filter by type.
*
* @param array $args Query args.
* @return array
*/
public function filter_virtual_post_clauses( $args ) {
$args['join'] = $this->append_product_sorting_table_join( $args['join'] );
$args['where'] = ' AND wc_product_meta_lookup.virtual=1 ';
return $args;
}
/**
* Join wc_product_meta_lookup to posts if not already joined.
*

View File

@ -832,6 +832,8 @@ CREATE TABLE {$wpdb->prefix}wc_product_meta_lookup (
`average_rating` decimal(3,2) NULL default 0.00,
`total_sales` bigint(20) NULL default 0,
`stock` bigint(20) NULL default NULL,
`virtual` tinyint(1) NULL default 0,
`downloadable` tinyint(1) NULL default 0,
PRIMARY KEY (`product_id`),
KEY min_max_price (`min_price`, `max_price`)
) $collate;

View File

@ -1888,6 +1888,8 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
'average_rating' => get_post_meta( $id, '_wc_average_rating', true ),
'total_sales' => get_post_meta( $id, 'total_sales', true ),
'stock' => $stock,
'virtual' => 'yes' === get_post_meta( $id, '_virtual', true ) ? 1 : 0,
'downloadable' => 'yes' === get_post_meta( $id, '_downloadable', true ) ? 1 : 0,
);
}
return array();

View File

@ -1278,6 +1278,7 @@ function wc_deferred_product_sync( $product_id ) {
function wc_update_product_lookup_tables() {
global $wpdb;
// Move meta data into the lookup table.
$wpdb->query(
"
INSERT IGNORE INTO {$wpdb->wc_product_meta_lookup} (`product_id`, `min_price`, `max_price`, `average_rating`, `total_sales`, `sku`)
@ -1296,6 +1297,7 @@ function wc_update_product_lookup_tables() {
"
);
// Move stock data into the lookup table.
$wpdb->query(
"
UPDATE
@ -1309,7 +1311,31 @@ function wc_update_product_lookup_tables() {
"
);
// Rating counts are serialised.
// Move type data into table.
$wpdb->query(
"
UPDATE
{$wpdb->wc_product_meta_lookup} lookup_table
LEFT JOIN {$wpdb->postmeta} meta1 ON lookup_table.product_id = meta1.post_id AND meta1.meta_key = '_downloadable'
SET
lookup_table.downloadable = 1
WHERE
meta1.meta_value = 'yes'
"
);
$wpdb->query(
"
UPDATE
{$wpdb->wc_product_meta_lookup} lookup_table
LEFT JOIN {$wpdb->postmeta} meta1 ON lookup_table.product_id = meta1.post_id AND meta1.meta_key = '_virtual'
SET
lookup_table.virtual = 1
WHERE
meta1.meta_value = 'yes'
"
);
// Rating counts are serialised so add them gradually using queue.
$rating_count_rows = $wpdb->get_results(
"
SELECT post_id, meta_value FROM {$wpdb->postmeta}