diff --git a/includes/abstracts/abstract-wc-legacy-product.php b/includes/abstracts/abstract-wc-legacy-product.php index 6b27386ff59..c77aa1ad918 100644 --- a/includes/abstracts/abstract-wc-legacy-product.php +++ b/includes/abstracts/abstract-wc-legacy-product.php @@ -203,80 +203,6 @@ abstract class WC_Abstract_Legacy_Product extends WC_Data { wc_deprecated_function( 'WC_Product::check_stock_status', '2.7' ); } - /** - * Returns the availability of the product. - * - * @deprecated 2.7.0 - * @return string - */ - public function get_availability() { - wc_deprecated_function( 'WC_Product::get_availability', '2.7', 'Handled in stock.php template file and wc_format_stock_for_display function.' ); - return apply_filters( 'woocommerce_get_availability', array( - 'availability' => $this->get_availability_text(), - 'class' => $this->get_availability_class(), - ), $this ); - } - - /** - * Get availability text based on stock status. - * - * @deprecated 2.7.0 - * @return string - */ - protected function get_availability_text() { - wc_deprecated_function( 'WC_Product::get_availability_text', '2.7' ); - if ( ! $this->is_in_stock() ) { - $availability = __( 'Out of stock', 'woocommerce' ); - } elseif ( $this->managing_stock() && $this->is_on_backorder( 1 ) ) { - $availability = $this->backorders_require_notification() ? __( 'Available on backorder', 'woocommerce' ) : __( 'In stock', 'woocommerce' ); - } elseif ( $this->managing_stock() ) { - switch ( get_option( 'woocommerce_stock_format' ) ) { - case 'no_amount' : - $availability = __( 'In stock', 'woocommerce' ); - break; - case 'low_amount' : - if ( $this->get_total_stock() <= get_option( 'woocommerce_notify_low_stock_amount' ) ) { - $availability = sprintf( __( 'Only %s left in stock', 'woocommerce' ), $this->get_total_stock() ); - - if ( $this->backorders_allowed() && $this->backorders_require_notification() ) { - $availability .= ' ' . __( '(also available on backorder)', 'woocommerce' ); - } - } else { - $availability = __( 'In stock', 'woocommerce' ); - } - break; - default : - $availability = sprintf( __( '%s in stock', 'woocommerce' ), $this->get_total_stock() ); - - if ( $this->backorders_allowed() && $this->backorders_require_notification() ) { - $availability .= ' ' . __( '(also available on backorder)', 'woocommerce' ); - } - break; - } - } else { - $availability = ''; - } - return apply_filters( 'woocommerce_get_availability_text', $availability, $this ); - } - - /** - * Get availability classname based on stock status. - * - * @deprecated 2.7.0 - * @return string - */ - protected function get_availability_class() { - wc_deprecated_function( 'WC_Product::get_availability_class', '2.7' ); - if ( ! $this->is_in_stock() ) { - $class = 'out-of-stock'; - } elseif ( $this->managing_stock() && $this->is_on_backorder( 1 ) && $this->backorders_require_notification() ) { - $class = 'available-on-backorder'; - } else { - $class = 'in-stock'; - } - return apply_filters( 'woocommerce_get_availability_class', $class, $this ); - } - /** * Get and return related products. * @deprecated 2.7.0 Use wc_get_related_products instead. @@ -323,9 +249,8 @@ abstract class WC_Abstract_Legacy_Product extends WC_Data { * @return string */ public function get_price_html_from_text() { - wc_deprecated_function( 'WC_Product::get_price_html_from_text', '2.7' ); - $from = '' . _x( 'From:', 'min_price', 'woocommerce' ) . ' '; - return apply_filters( 'woocommerce_get_price_html_from_text', $from, $this ); + wc_deprecated_function( 'WC_Product::get_price_html_from_text', '2.7', 'wc_get_price_from_prefix' ); + return wc_get_price_html_from_text(); } /** @@ -341,19 +266,6 @@ abstract class WC_Abstract_Legacy_Product extends WC_Data { return apply_filters( 'woocommerce_get_price_html_from_to', wc_format_sale_price( $from, $to ), $from, $to, $this ); } - /** - * Get the suffix to display after prices > 0. - * - * @deprecated 2.7.0 Use wc_get_price_suffix instead. - * @param string $price to calculate, left blank to just use get_price() - * @param integer $qty passed on to get_price_including_tax() or get_price_excluding_tax() - * @return string - */ - public function get_price_suffix( $price = '', $qty = 1 ) { - wc_deprecated_function( 'WC_Product::get_price_suffix', '2.7', 'wc_get_price_suffix' ); - return wc_get_price_suffix( $this, $price, $qty ); - } - /** * Lists a table of attributes for the product page. * @deprecated 2.7.0 Use wc_display_product_attributes instead. @@ -537,7 +449,7 @@ abstract class WC_Abstract_Legacy_Product extends WC_Data { * @return boolean */ public function has_all_attributes_set() { - wc_deprecated_function( 'WC_Product::has_all_attributes_set', '2.7' ); + wc_deprecated_function( 'WC_Product::has_all_attributes_set', '2.7', 'Use array filter on get_variation_attributes for a quick solution.' ); $set = true; // undefined attributes have null strings as array values diff --git a/includes/abstracts/abstract-wc-product.php b/includes/abstracts/abstract-wc-product.php index 3b62130c1a5..0d90b75554d 100644 --- a/includes/abstracts/abstract-wc-product.php +++ b/includes/abstracts/abstract-wc-product.php @@ -1661,9 +1661,9 @@ class WC_Product extends WC_Abstract_Legacy_Product { } if ( $this->is_on_sale() ) { - $price = wc_format_sale_price( wc_get_price_to_display( $this, array( 'price' => $this->get_regular_price() ) ), wc_get_price_to_display( $this ) ) . wc_get_price_suffix( $this ); + $price = wc_format_sale_price( wc_get_price_to_display( $this, array( 'price' => $this->get_regular_price() ) ), wc_get_price_to_display( $this ) ) . $this->get_price_suffix(); } else { - $price = wc_price( wc_get_price_to_display( $this ) ) . wc_get_price_suffix( $this ); + $price = wc_price( wc_get_price_to_display( $this ) ) . $this->get_price_suffix(); } return apply_filters( 'woocommerce_get_price_html', $price, $this ); @@ -1816,4 +1816,73 @@ class WC_Product extends WC_Abstract_Legacy_Product { // allow overriding based on the particular file being requested return apply_filters( 'woocommerce_product_file_download_path', $file_path, $this, $download_id ); } + + /** + * Get the suffix to display after prices > 0. + * + * @param string $price to calculate, left blank to just use get_price() + * @param integer $qty passed on to get_price_including_tax() or get_price_excluding_tax() + * @return string + */ + public function get_price_suffix( $price = '', $qty = 1 ) { + $html = ''; + + if ( ( $suffix = get_option( 'woocommerce_price_display_suffix' ) ) && wc_tax_enabled() ) { + if ( '' === $price ) { + $price = $this->get_price(); + } + $replacements = array( + '{price_including_tax}' => wc_price( wc_get_price_including_tax( $this, array( 'qty' => $qty, 'price' => $price ) ) ), + '{price_excluding_tax}' => wc_price( wc_get_price_excluding_tax( $this, array( 'qty' => $qty, 'price' => $price ) ) ), + ); + $html = str_replace( array_keys( $replacements ), array_values( $replacements ), ' ' . wp_kses_post( $suffix ) . '' ); + } + return apply_filters( 'woocommerce_get_price_suffix', $html, $this ); + } + + /** + * Returns the availability of the product. + * + * @return string + */ + public function get_availability() { + return apply_filters( 'woocommerce_get_availability', array( + 'availability' => $this->get_availability_text(), + 'class' => $this->get_availability_class(), + ), $this ); + } + + /** + * Get availability text based on stock status. + * + * @return string + */ + protected function get_availability_text() { + if ( ! $this->is_in_stock() ) { + $availability = __( 'Out of stock', 'woocommerce' ); + } elseif ( $this->managing_stock() && $this->is_on_backorder( 1 ) ) { + $availability = __( 'Available on backorder', 'woocommerce' ); + } elseif ( $this->managing_stock() ) { + $availability = wc_format_stock_for_display( $this->get_stock_quantity(), $this->backorders_allowed() && $this->backorders_require_notification() ); + } else { + $availability = ''; + } + return apply_filters( 'woocommerce_get_availability_text', $availability, $this ); + } + + /** + * Get availability classname based on stock status. + * + * @return string + */ + protected function get_availability_class() { + if ( ! $this->is_in_stock() ) { + $class = 'out-of-stock'; + } elseif ( $this->managing_stock() && $this->is_on_backorder( 1 ) ) { + $class = 'available-on-backorder'; + } else { + $class = 'in-stock'; + } + return apply_filters( 'woocommerce_get_availability_class', $class, $this ); + } } diff --git a/includes/class-wc-product-grouped.php b/includes/class-wc-product-grouped.php index 5beb725d839..cbc7eac9c15 100644 --- a/includes/class-wc-product-grouped.php +++ b/includes/class-wc-product-grouped.php @@ -105,7 +105,7 @@ class WC_Product_Grouped extends WC_Product { if ( $is_free ) { $price = apply_filters( 'woocommerce_grouped_free_price_html', __( 'Free!', 'woocommerce' ), $this ); } else { - $price = apply_filters( 'woocommerce_grouped_price_html', $price . wc_get_price_suffix( $this ), $this, $child_prices ); + $price = apply_filters( 'woocommerce_grouped_price_html', $price . $this->get_price_suffix(), $this, $child_prices ); } } else { $price = apply_filters( 'woocommerce_grouped_empty_price_html', '', $this ); diff --git a/includes/class-wc-product-variable.php b/includes/class-wc-product-variable.php index 840d6a7deee..4c7adf652e3 100644 --- a/includes/class-wc-product-variable.php +++ b/includes/class-wc-product-variable.php @@ -175,9 +175,9 @@ class WC_Product_Variable extends WC_Product { $max_price = end( $prices['price'] ); if ( $min_price !== $max_price ) { - $price = apply_filters( 'woocommerce_variable_price_html', wc_format_price_range( $min_price, $max_price ) . wc_get_price_suffix( $this ), $this ); + $price = apply_filters( 'woocommerce_variable_price_html', wc_format_price_range( $min_price, $max_price ), $this ); } else { - $price = apply_filters( 'woocommerce_variable_price_html', wc_price( $min_price ) . wc_get_price_suffix( $this ), $this ); + $price = apply_filters( 'woocommerce_variable_price_html', wc_price( $min_price ) . $this->get_price_suffix(), $this ); } return apply_filters( 'woocommerce_get_price_html', $price, $this ); } diff --git a/includes/wc-template-functions.php b/includes/wc-template-functions.php index 96ddc4377dd..eb5094b22be 100644 --- a/includes/wc-template-functions.php +++ b/includes/wc-template-functions.php @@ -2500,50 +2500,24 @@ function wc_display_product_attributes( $product ) { function wc_get_stock_html( $product ) { ob_start(); + $availability = $product->get_availability(); + wc_get_template( 'single-product/stock.php', array( - 'product' => $product, + 'product' => $product, + 'class' => $availability['class'], + 'availability' => $availability['availability'], ) ); $html = ob_get_clean(); if ( has_filter( 'woocommerce_stock_html' ) ) { wc_deprecated_function( 'The woocommerce_stock_html filter', '', 'woocommerce_get_stock_html' ); - $html = apply_filters( 'woocommerce_stock_html', $html, $product->get_availability_text(), $product ); + $html = apply_filters( 'woocommerce_stock_html', $html, $availability['availability'], $product ); } return apply_filters( 'woocommerce_get_stock_html', $html, $product ); } -/** - * Get the price suffix for a product if needed. - * @since 2.7.0 - * @param WC_Product $product - * @param string $price - * @param integer $qty - * @return string - */ -function wc_get_price_suffix( $product, $price = '', $qty = 1 ) { - if ( ( $price_display_suffix = get_option( 'woocommerce_price_display_suffix' ) ) && wc_tax_enabled() ) { - $price = '' === $price ? $product->get_price() : $price; - $price_display_suffix = ' ' . wp_kses_post( $price_display_suffix ) . ''; - - $find = array( - '{price_including_tax}', - '{price_excluding_tax}', - ); - - $replace = array( - wc_price( wc_get_price_including_tax( $product, array( 'qty' => $qty, 'price' => $price ) ) ), - wc_price( wc_get_price_excluding_tax( $product, array( 'qty' => $qty, 'price' => $price ) ) ), - ); - - $price_display_suffix = str_replace( $find, $replace, $price_display_suffix ); - } else { - $price_display_suffix = ''; - } - return apply_filters( 'woocommerce_get_price_suffix', $price_display_suffix, $product ); -} - /** * Get HTML for ratings. * @@ -2561,3 +2535,13 @@ function wc_get_rating_html( $rating ) { } return apply_filters( 'woocommerce_product_get_rating_html', $rating_html, $rating ); } + +/** + * Returns a 'from' prefix if you want to show where prices start at. + * + * @since 2.7.0 + * @return string + */ +function wc_get_price_html_from_text() { + return apply_filters( 'woocommerce_get_price_html_from_text', '' . _x( 'From:', 'min_price', 'woocommerce' ) . ' ' ); +} diff --git a/templates/single-product/stock.php b/templates/single-product/stock.php index 0974dce1b29..e502a1b3059 100644 --- a/templates/single-product/stock.php +++ b/templates/single-product/stock.php @@ -20,16 +20,5 @@ if ( ! defined( 'ABSPATH' ) ) { exit; } -if ( ! $product->is_in_stock() ) : ?> - -
- -managing_stock() && $product->is_on_backorder( 1 ) ) : ?> - - - -managing_stock() ) : ?> - -get_stock_quantity(), $product->backorders_allowed() && $product->backorders_require_notification() ) ); ?>
- - +?> +