Stock status

This commit is contained in:
Mike Jolley 2019-03-07 14:32:20 +00:00
parent 1ab455ec09
commit 0782d39a55
5 changed files with 42 additions and 28 deletions

View File

@ -215,7 +215,7 @@ if ( ! class_exists( 'WC_Admin_Dashboard', false ) ) :
if ( false === $lowinstock_count ) { if ( false === $lowinstock_count ) {
$lowinstock_count = (int) $wpdb->get_var( $lowinstock_count = (int) $wpdb->get_var(
$wpdb->prepare( $wpdb->prepare(
"SELECT COUNT( product_id ) FROM {$wpdb->wc_product_meta_lookup} WHERE stock <= %d AND stock > %d", "SELECT COUNT( product_id ) FROM {$wpdb->wc_product_meta_lookup} WHERE stock_quantity <= %d AND stock > %d",
$stock, $stock,
$nostock $nostock
) )
@ -229,7 +229,7 @@ if ( ! class_exists( 'WC_Admin_Dashboard', false ) ) :
if ( false === $outofstock_count ) { if ( false === $outofstock_count ) {
$outofstock_count = (int) $wpdb->get_var( $outofstock_count = (int) $wpdb->get_var(
$wpdb->prepare( $wpdb->prepare(
"SELECT COUNT( product_id ) FROM {$wpdb->wc_product_meta_lookup} WHERE stock <= %d", "SELECT COUNT( product_id ) FROM {$wpdb->wc_product_meta_lookup} WHERE stock_quantity <= %d",
$nostock $nostock
) )
); );

View File

@ -469,7 +469,13 @@ class WC_Admin_List_Table_Products extends WC_Admin_List_Table {
} }
} }
if ( isset( $_GET['product_shipping_class'] ) && '0' === $_GET['product_shipping_class'] ) { // WPCS: input var ok. // Stock status filter.
if ( ! empty( $_GET['stock_status'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification
add_filter( 'posts_clauses', array( $this, 'filter_stock_status_post_clauses' ) );
}
// Shipping class taxonomy.
if ( ! empty( $_GET['product_shipping_class'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification
$query_vars['tax_query'][] = array( $query_vars['tax_query'][] = array(
'taxonomy' => 'product_shipping_class', 'taxonomy' => 'product_shipping_class',
'field' => 'id', 'field' => 'id',
@ -478,17 +484,6 @@ class WC_Admin_List_Table_Products extends WC_Admin_List_Table {
); );
} }
if ( ! empty( $_GET['stock_status'] ) ) {
if ( ! isset( $query_vars['meta_query'] ) ) {
$query_vars['meta_query'] = array(); // phpcs:ignore WordPress.VIP.SlowDBQuery.slow_db_query_meta_query
}
$query_vars['meta_query'][] = array(
'key' => '_stock_status',
'value' => wc_clean( wp_unslash( $_GET['stock_status'] ) ),
);
}
// Search using CRUD. // Search using CRUD.
if ( ! empty( $query_vars['s'] ) ) { if ( ! empty( $query_vars['s'] ) ) {
$data_store = WC_Data_Store::load( 'product' ); $data_store = WC_Data_Store::load( 'product' );
@ -526,6 +521,7 @@ class WC_Admin_List_Table_Products extends WC_Admin_List_Table {
remove_filter( 'posts_clauses', array( $this, 'order_by_sku_desc_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_downloadable_post_clauses' ) );
remove_filter( 'posts_clauses', array( $this, 'filter_virtual_post_clauses' ) ); remove_filter( 'posts_clauses', array( $this, 'filter_virtual_post_clauses' ) );
remove_filter( 'posts_clauses', array( $this, 'filter_stock_status_post_clauses' ) );
return $posts; return $posts;
} }
@ -584,8 +580,8 @@ class WC_Admin_List_Table_Products extends WC_Admin_List_Table {
* @return array * @return array
*/ */
public function filter_downloadable_post_clauses( $args ) { public function filter_downloadable_post_clauses( $args ) {
$args['join'] = $this->append_product_sorting_table_join( $args['join'] ); $args['join'] = $this->append_product_sorting_table_join( $args['join'] );
$args['where'] = ' AND wc_product_meta_lookup.downloadable=1 '; $args['where'] .= ' AND wc_product_meta_lookup.downloadable=1 ';
return $args; return $args;
} }
@ -596,8 +592,23 @@ class WC_Admin_List_Table_Products extends WC_Admin_List_Table {
* @return array * @return array
*/ */
public function filter_virtual_post_clauses( $args ) { public function filter_virtual_post_clauses( $args ) {
$args['join'] = $this->append_product_sorting_table_join( $args['join'] ); $args['join'] = $this->append_product_sorting_table_join( $args['join'] );
$args['where'] = ' AND wc_product_meta_lookup.virtual=1 '; $args['where'] .= ' AND wc_product_meta_lookup.virtual=1 ';
return $args;
}
/**
* Filter by stock status.
*
* @param array $args Query args.
* @return array
*/
public function filter_stock_status_post_clauses( $args ) {
global $wpdb;
if ( ! empty( $_GET['stock_status'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification
$args['join'] = $this->append_product_sorting_table_join( $args['join'] );
$args['where'] .= $wpdb->prepare( ' AND wc_product_meta_lookup.stock_status=%s ', wc_clean( wp_unslash( $_GET['stock_status'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification
}
return $args; return $args;
} }

View File

@ -826,14 +826,15 @@ CREATE TABLE {$wpdb->prefix}wc_download_log (
CREATE TABLE {$wpdb->prefix}wc_product_meta_lookup ( CREATE TABLE {$wpdb->prefix}wc_product_meta_lookup (
`product_id` bigint(20) NOT NULL, `product_id` bigint(20) NOT NULL,
`sku` varchar(100) NULL default '', `sku` varchar(100) NULL default '',
`virtual` tinyint(1) NULL default 0,
`downloadable` tinyint(1) NULL default 0,
`min_price` decimal(10,2) NULL default NULL, `min_price` decimal(10,2) NULL default NULL,
`max_price` decimal(10,2) NULL default NULL, `max_price` decimal(10,2) NULL default NULL,
`stock_quantity` double NULL default NULL,
`stock_status` varchar(100) NULL default 'instock',
`rating_count` bigint(20) NULL default 0, `rating_count` bigint(20) NULL default 0,
`average_rating` decimal(3,2) NULL default 0.00, `average_rating` decimal(3,2) NULL default 0.00,
`total_sales` bigint(20) NULL default 0, `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`), PRIMARY KEY (`product_id`),
KEY min_max_price (`min_price`, `max_price`) KEY min_max_price (`min_price`, `max_price`)
) $collate; ) $collate;

View File

@ -1882,14 +1882,15 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
return array( return array(
'product_id' => absint( $id ), 'product_id' => absint( $id ),
'sku' => get_post_meta( $id, '_sku', true ), 'sku' => get_post_meta( $id, '_sku', true ),
'virtual' => 'yes' === get_post_meta( $id, '_virtual', true ) ? 1 : 0,
'downloadable' => 'yes' === get_post_meta( $id, '_downloadable', true ) ? 1 : 0,
'min_price' => reset( $price_meta ), 'min_price' => reset( $price_meta ),
'max_price' => end( $price_meta ), 'max_price' => end( $price_meta ),
'stock_quantity' => $stock,
'stock_status' => get_post_meta( $id, '_stock_status', true ),
'rating_count' => array_sum( (array) get_post_meta( $id, '_wc_rating_count', true ) ), 'rating_count' => array_sum( (array) get_post_meta( $id, '_wc_rating_count', true ) ),
'average_rating' => get_post_meta( $id, '_wc_average_rating', true ), 'average_rating' => get_post_meta( $id, '_wc_average_rating', true ),
'total_sales' => get_post_meta( $id, 'total_sales', 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(); return array();

View File

@ -1281,19 +1281,20 @@ function wc_update_product_lookup_tables() {
// Move meta data into the lookup table. // Move meta data into the lookup table.
$wpdb->query( $wpdb->query(
" "
INSERT IGNORE INTO {$wpdb->wc_product_meta_lookup} (`product_id`, `min_price`, `max_price`, `average_rating`, `total_sales`, `sku`) INSERT IGNORE INTO {$wpdb->wc_product_meta_lookup} (`product_id`, `min_price`, `max_price`, `average_rating`, `total_sales`, `sku`, `stock_status`)
SELECT SELECT
posts.ID, MIN(meta1.meta_value), MAX(meta1.meta_value), meta2.meta_value, meta3.meta_value, meta4.meta_value posts.ID, MIN(meta1.meta_value), MAX(meta1.meta_value), meta2.meta_value, meta3.meta_value, meta4.meta_value, meta5.meta_value
FROM {$wpdb->posts} posts FROM {$wpdb->posts} posts
LEFT JOIN {$wpdb->postmeta} meta1 ON posts.ID = meta1.post_id AND meta1.meta_key = '_price' LEFT JOIN {$wpdb->postmeta} meta1 ON posts.ID = meta1.post_id AND meta1.meta_key = '_price'
LEFT JOIN {$wpdb->postmeta} meta2 ON posts.ID = meta2.post_id AND meta2.meta_key = '_wc_average_rating' LEFT JOIN {$wpdb->postmeta} meta2 ON posts.ID = meta2.post_id AND meta2.meta_key = '_wc_average_rating'
LEFT JOIN {$wpdb->postmeta} meta3 ON posts.ID = meta3.post_id AND meta3.meta_key = 'total_sales' LEFT JOIN {$wpdb->postmeta} meta3 ON posts.ID = meta3.post_id AND meta3.meta_key = 'total_sales'
LEFT JOIN {$wpdb->postmeta} meta4 ON posts.ID = meta4.post_id AND meta4.meta_key = '_sku' LEFT JOIN {$wpdb->postmeta} meta4 ON posts.ID = meta4.post_id AND meta4.meta_key = '_sku'
LEFT JOIN {$wpdb->postmeta} meta5 ON posts.ID = meta5.post_id AND meta5.meta_key = '_stock_status'
WHERE WHERE
posts.post_type IN ('product', 'product_variation') posts.post_type IN ('product', 'product_variation')
AND meta1.meta_value <> '' AND meta1.meta_value <> ''
GROUP BY GROUP BY
posts.ID, meta2.meta_value, meta3.meta_value, meta4.meta_value posts.ID, meta2.meta_value, meta3.meta_value, meta4.meta_value, meta5.meta_value
" "
); );
@ -1305,7 +1306,7 @@ function wc_update_product_lookup_tables() {
LEFT JOIN {$wpdb->postmeta} meta1 ON lookup_table.product_id = meta1.post_id AND meta1.meta_key = '_manage_stock' LEFT JOIN {$wpdb->postmeta} meta1 ON lookup_table.product_id = meta1.post_id AND meta1.meta_key = '_manage_stock'
LEFT JOIN {$wpdb->postmeta} meta2 ON lookup_table.product_id = meta2.post_id AND meta2.meta_key = '_stock' LEFT JOIN {$wpdb->postmeta} meta2 ON lookup_table.product_id = meta2.post_id AND meta2.meta_key = '_stock'
SET SET
lookup_table.stock = meta2.meta_value lookup_table.stock_quantity = meta2.meta_value
WHERE WHERE
meta1.meta_value = 'yes' meta1.meta_value = 'yes'
" "