From 1ac2c1a94f9a028c622f7428d5b8667df540cb2c Mon Sep 17 00:00:00 2001 From: Brent Shepherd Date: Mon, 23 Jan 2017 11:44:25 -0800 Subject: [PATCH] Move price hash to separate function So that it can be used by child classes without copy/pasting code. --- ...ass-wc-product-variable-data-store-cpt.php | 53 +++++++++++-------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/includes/data-stores/class-wc-product-variable-data-store-cpt.php b/includes/data-stores/class-wc-product-variable-data-store-cpt.php index 4154c3f6f61..1c2d4e6d1b9 100644 --- a/includes/data-stores/class-wc-product-variable-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-variable-data-store-cpt.php @@ -143,7 +143,6 @@ class WC_Product_Variable_Data_Store_CPT extends WC_Product_Data_Store_CPT imple * @return array of prices */ public function read_price_data( &$product, $include_taxes = false ) { - global $wp_filter; /** * Transient name for storing prices for this product (note: Max transient length is 45) @@ -151,26 +150,7 @@ class WC_Product_Variable_Data_Store_CPT extends WC_Product_Data_Store_CPT imple */ $transient_name = 'wc_var_prices_' . $product->get_id(); - /** - * Create unique cache key based on the tax location (affects displayed/cached prices), product version and active price filters. - * DEVELOPERS should filter this hash if offering conditonal pricing to keep it unique. - * @var string - */ - $price_hash = $include_taxes ? array( get_option( 'woocommerce_tax_display_shop', 'excl' ), WC_Tax::get_rates() ) : array( false ); - $filter_names = array( 'woocommerce_variation_prices_price', 'woocommerce_variation_prices_regular_price', 'woocommerce_variation_prices_sale_price' ); - - foreach ( $filter_names as $filter_name ) { - if ( ! empty( $wp_filter[ $filter_name ] ) ) { - $price_hash[ $filter_name ] = array(); - - foreach ( $wp_filter[ $filter_name ] as $priority => $callbacks ) { - $price_hash[ $filter_name ][] = array_values( wp_list_pluck( $callbacks, 'function' ) ); - } - } - } - - $price_hash[] = WC_Cache_Helper::get_transient_version( 'product' ); - $price_hash = md5( json_encode( apply_filters( 'woocommerce_get_variation_prices_hash', $price_hash, $product, $include_taxes ) ) ); + $price_hash = $this->get_price_hash( $product, $include_taxes ); /** * $this->prices_array is an array of values which may have been modified from what is stored in transients - this may not match $transient_cached_prices_array. @@ -247,6 +227,37 @@ class WC_Product_Variable_Data_Store_CPT extends WC_Product_Data_Store_CPT imple return $this->prices_array[ $price_hash ]; } + /** + * Create unique cache key based on the tax location (affects displayed/cached prices), product version and active price filters. + * DEVELOPERS should filter this hash if offering conditonal pricing to keep it unique. + * + * @since 2.7.0 + * @param WC_Product + * @param bool $include_taxes If taxes should be calculated or not. + * @return string + */ + protected function get_price_hash( &$product, $include_taxes = false ) { + global $wp_filter; + + $price_hash = $include_taxes ? array( get_option( 'woocommerce_tax_display_shop', 'excl' ), WC_Tax::get_rates() ) : array( false ); + $filter_names = array( 'woocommerce_variation_prices_price', 'woocommerce_variation_prices_regular_price', 'woocommerce_variation_prices_sale_price' ); + + foreach ( $filter_names as $filter_name ) { + if ( ! empty( $wp_filter[ $filter_name ] ) ) { + $price_hash[ $filter_name ] = array(); + + foreach ( $wp_filter[ $filter_name ] as $priority => $callbacks ) { + $price_hash[ $filter_name ][] = array_values( wp_list_pluck( $callbacks, 'function' ) ); + } + } + } + + $price_hash[] = WC_Cache_Helper::get_transient_version( 'product' ); + $price_hash = md5( json_encode( apply_filters( 'woocommerce_get_variation_prices_hash', $price_hash, $product, $include_taxes ) ) ); + + return $price_hash; + } + /** * Does a child have a weight set? *