settings = $settings; if ( $this->isEnable() && ! is_admin() ) { add_filter( 'woocommerce_get_price_html', [ $this, 'formatPrice' ], 999, 2 ); } } /** * Change logic showing prince at catalog for product with tiered price rules * * @param string $priceHtml * @param WC_Product $product * * @return string */ public function formatPrice( $priceHtml, $product ) { $formatCondition = $this->settings->get( 'tiered_price_at_product_page', 'no' ) === 'yes' || get_queried_object_id() != $product->get_id(); if ( $formatCondition ) { $displayPriceType = $this->getDisplayType(); if ( in_array( $product->get_type(), [ 'simple', 'variation' ] ) ) { $rules = PriceManager::getPriceRules( $product->get_id() ); if ( ! empty( $rules ) ) { if ( $displayPriceType === 'range' ) { return $this->getRange( $rules, $product ); } else { return $this->getLowestPrice( $rules, $product ); } } } if ( $product instanceof WC_Product_Variable && $this->useForVariable() === 'yes' ) { $price = $this->formatPriceForVariableProduct( $product ); if ( $price ) { return $price; } } } return $priceHtml; } /** * Format price for variable product. Range uses lowest and high prices from all variations * * @param WC_Product_Variable $product * * @return bool|string */ protected function formatPriceForVariableProduct( WC_Product_Variable $product ) { $prices = $product->get_variation_prices( true ); $maxPrice = end( $prices['price'] ); $minPrices = []; foreach ( $product->get_available_variations() as $variation ) { $rules = PriceManager::getPriceRules( $variation['variation_id'] ); if ( ! empty( $rules ) ) { $minPrices[] = $this->getLowestPrice( $rules, $product, false ); } } if ( ! empty( $minPrices ) ) { if ( $this->getDisplayType() === 'range' ) { return wc_price( min( $minPrices ) ) . ' - ' . wc_price( $maxPrice ); } else { return $this->getLowestPrefix() . ' ' . wc_price( min( $minPrices ) ); } } return false; } /** * Get range from lowest to highest price from price rules * * @param array $rules * @param WC_Product $product * * @return string */ protected function getRange( $rules, $product ) { $lowest = array_pop( $rules ); $highest_html = wc_price( wc_get_price_to_display( $product, [ 'price' => $product->get_price(), ] ) ); if ( PriceManager::getPricingType( $product->get_id() ) === 'percentage' ) { $lowest_html = wc_price( wc_get_price_to_display( $product, [ 'price' => PriceManager::getPriceByPercentDiscount( $product->get_price(), $lowest ), ] ) ); return $lowest_html . ' - ' . $highest_html; } $lowest_html = wc_price( wc_get_price_to_display( $product, [ 'price' => $lowest, ] ) ); return $lowest_html . ' - ' . $highest_html; } /** * Get lowest price from price rules * * @param array $rules * @param WC_Product $product * * @param bool $html * * @return string|float */ protected function getLowestPrice( $rules, $product, $html = true ) { if ( PriceManager::getPricingType( $product->get_id() ) === 'percentage' ) { $lowest = PriceManager::getPriceByPercentDiscount( $product->get_price(), array_pop( $rules ) ); } else { $lowest = array_pop( $rules ); } if ( ! $html ) { return wc_get_price_to_display( $product, [ 'price' => $lowest ] ); } return $this->getLowestPrefix() . ' ' . wc_price( wc_get_price_to_display( $product, [ 'price' => $lowest ] ) ); } public function getLowestPrefix() { return $this->settings->get( 'lowest_prefix', __( 'From', 'tier-pricing-table' ) ); } public function isEnable() { return $this->settings->get( 'tiered_price_at_catalog', 'yes' ) === 'yes'; } public function getDisplayType() { return $this->settings->get( 'tiered_price_at_catalog_type', 'range' ); } public function useForVariable() { return $this->settings->get( 'tiered_price_at_catalog_for_variable', 'yes' ); } }