DataStore.php
144 lines
| 1 | <?php |
| 2 | /** |
| 3 | * API\Reports\Stock\Stats\DataStore class file. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Admin\API\Reports\Stock\Stats; |
| 7 | |
| 8 | defined( 'ABSPATH' ) || exit; |
| 9 | |
| 10 | use Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore; |
| 11 | use Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface; |
| 12 | use Automattic\WooCommerce\Enums\ProductStockStatus; |
| 13 | |
| 14 | /** |
| 15 | * API\Reports\Stock\Stats\DataStore. |
| 16 | */ |
| 17 | class DataStore extends ReportsDataStore implements DataStoreInterface { |
| 18 | |
| 19 | /** |
| 20 | * Get stock counts for the whole store. |
| 21 | * |
| 22 | * @override ReportsDataStore::get_data() |
| 23 | * |
| 24 | * @param array $query Not used for the stock stats data store, but needed for the interface. |
| 25 | * @return array Array of counts. |
| 26 | */ |
| 27 | public function get_data( $query ) { |
| 28 | $report_data = array(); |
| 29 | $cache_expire = DAY_IN_SECONDS * 30; |
| 30 | $low_stock_transient_name = 'wc_admin_stock_count_lowstock'; |
| 31 | $low_stock_count = get_transient( $low_stock_transient_name ); |
| 32 | if ( false === $low_stock_count ) { |
| 33 | $low_stock_count = $this->get_low_stock_count(); |
| 34 | set_transient( $low_stock_transient_name, $low_stock_count, $cache_expire ); |
| 35 | } else { |
| 36 | $low_stock_count = intval( $low_stock_count ); |
| 37 | } |
| 38 | $report_data[ ProductStockStatus::LOW_STOCK ] = $low_stock_count; |
| 39 | |
| 40 | $status_options = wc_get_product_stock_status_options(); |
| 41 | foreach ( $status_options as $status => $label ) { |
| 42 | $transient_name = 'wc_admin_stock_count_' . $status; |
| 43 | $count = get_transient( $transient_name ); |
| 44 | if ( false === $count ) { |
| 45 | $count = $this->get_count( $status ); |
| 46 | set_transient( $transient_name, $count, $cache_expire ); |
| 47 | } else { |
| 48 | $count = intval( $count ); |
| 49 | } |
| 50 | $report_data[ $status ] = $count; |
| 51 | } |
| 52 | |
| 53 | $product_count_transient_name = 'wc_admin_product_count'; |
| 54 | $product_count = get_transient( $product_count_transient_name ); |
| 55 | if ( false === $product_count ) { |
| 56 | $product_count = $this->get_product_count(); |
| 57 | set_transient( $product_count_transient_name, $product_count, $cache_expire ); |
| 58 | } else { |
| 59 | $product_count = intval( $product_count ); |
| 60 | } |
| 61 | $report_data['products'] = $product_count; |
| 62 | return $report_data; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Get low stock count (products with stock < low stock amount, but greater than no stock amount). |
| 67 | * |
| 68 | * @return int Low stock count. |
| 69 | */ |
| 70 | private function get_low_stock_count() { |
| 71 | global $wpdb; |
| 72 | |
| 73 | $no_stock_amount = absint( max( get_option( 'woocommerce_notify_no_stock_amount' ), 0 ) ); |
| 74 | $low_stock_amount = absint( max( get_option( 'woocommerce_notify_low_stock_amount' ), 1 ) ); |
| 75 | |
| 76 | return (int) $wpdb->get_var( |
| 77 | $wpdb->prepare( |
| 78 | " |
| 79 | SELECT count( DISTINCT posts.ID ) FROM {$wpdb->posts} posts |
| 80 | LEFT JOIN {$wpdb->wc_product_meta_lookup} wc_product_meta_lookup ON posts.ID = wc_product_meta_lookup.product_id |
| 81 | LEFT JOIN {$wpdb->postmeta} low_stock_amount_meta ON posts.ID = low_stock_amount_meta.post_id AND low_stock_amount_meta.meta_key = '_low_stock_amount' |
| 82 | WHERE posts.post_type IN ( 'product', 'product_variation' ) |
| 83 | AND posts.post_status IN ( 'publish', 'private' ) |
| 84 | AND wc_product_meta_lookup.stock_quantity IS NOT NULL |
| 85 | AND wc_product_meta_lookup.stock_status = 'instock' |
| 86 | AND ( |
| 87 | ( |
| 88 | low_stock_amount_meta.meta_value > '' |
| 89 | AND wc_product_meta_lookup.stock_quantity <= CAST(low_stock_amount_meta.meta_value AS SIGNED) |
| 90 | AND wc_product_meta_lookup.stock_quantity > %d |
| 91 | ) |
| 92 | OR ( |
| 93 | ( |
| 94 | low_stock_amount_meta.meta_value IS NULL OR low_stock_amount_meta.meta_value <= '' |
| 95 | ) |
| 96 | AND wc_product_meta_lookup.stock_quantity <= %d |
| 97 | AND wc_product_meta_lookup.stock_quantity > %d |
| 98 | ) |
| 99 | ) |
| 100 | ", |
| 101 | $no_stock_amount, |
| 102 | $low_stock_amount, |
| 103 | $no_stock_amount |
| 104 | ) |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Get count for the passed in stock status. |
| 110 | * |
| 111 | * @param string $status Status slug. |
| 112 | * @return int Count. |
| 113 | */ |
| 114 | private function get_count( $status ) { |
| 115 | global $wpdb; |
| 116 | |
| 117 | return (int) $wpdb->get_var( |
| 118 | $wpdb->prepare( |
| 119 | " |
| 120 | SELECT count( DISTINCT posts.ID ) FROM {$wpdb->posts} posts |
| 121 | LEFT JOIN {$wpdb->wc_product_meta_lookup} wc_product_meta_lookup ON posts.ID = wc_product_meta_lookup.product_id |
| 122 | WHERE posts.post_type IN ( 'product', 'product_variation' ) |
| 123 | AND posts.post_status IN ( 'publish', 'private' ) |
| 124 | AND wc_product_meta_lookup.stock_status = %s |
| 125 | ", |
| 126 | $status |
| 127 | ) |
| 128 | ); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Get product count for the store. |
| 133 | * |
| 134 | * @return int Product count. |
| 135 | */ |
| 136 | private function get_product_count() { |
| 137 | $query_args = array(); |
| 138 | $query_args['post_type'] = array( 'product', 'product_variation' ); |
| 139 | $query = new \WP_Query(); |
| 140 | $query->query( $query_args ); |
| 141 | return intval( $query->found_posts ); |
| 142 | } |
| 143 | } |
| 144 |