| @@ -2,9 +2,8 @@ | ||
| 2 | 2 | |
| 3 | 3 | use TierPricingTable\Settings\Settings; |
| 4 | 4 | use WC_Product; |
| 5 | 5 | use TierPricingTable\PriceManager; |
| 6 | -use WC_Product_Variable; | |
| 7 | 6 | |
| 8 | 7 | /** |
| 9 | 8 | * Class CatalogPriceManager |
| 10 | 9 | * |
| @@ -38,19 +37,15 @@ | ||
| 38 | 37 | * |
| 39 | 38 | * @return string |
| 40 | 39 | */ |
| 41 | 40 | public function formatPrice( $priceHtml, $product ) { |
| 42 | - | |
| 43 | - $formatCondition = $this->settings->get( 'tiered_price_at_product_page', | |
| 44 | - 'no' ) === 'yes' || get_queried_object_id() != $product->get_id(); | |
| 45 | - | |
| 46 | - if ( $formatCondition ) { | |
| 47 | - $displayPriceType = $this->getDisplayType(); | |
| 48 | - | |
| 41 | + if ( ! is_product() ) { | |
| 49 | 42 | if ( in_array( $product->get_type(), [ 'simple', 'variation' ] ) ) { |
| 50 | 43 | $rules = PriceManager::getPriceRules( $product->get_id() ); |
| 51 | 44 | |
| 52 | 45 | if ( ! empty( $rules ) ) { |
| 46 | + $displayPriceType = $this->getDisplayType(); | |
| 47 | + | |
| 53 | 48 | if ( $displayPriceType === 'range' ) { |
| 54 | 49 | return $this->getRange( $rules, $product ); |
| 55 | 50 | } else { |
| 56 | 51 | return $this->getLowestPrice( $rules, $product ); |
| @@ -56,15 +51,8 @@ | ||
| 56 | 51 | return $this->getLowestPrice( $rules, $product ); |
| 57 | 52 | } |
| 58 | 53 | } |
| 59 | 54 | } |
| 60 | - | |
| 61 | - if ( $product instanceof WC_Product_Variable && $this->useForVariable() === 'yes' ) { | |
| 62 | - $price = $this->formatPriceForVariableProduct( $product ); | |
| 63 | - if ( $price ) { | |
| 64 | - return $price; | |
| 65 | - } | |
| 66 | - } | |
| 67 | 55 | } |
| 68 | 56 | |
| 69 | 57 | return $priceHtml; |
| 70 | 58 | } |
| @@ -69,42 +57,8 @@ | ||
| 69 | 57 | return $priceHtml; |
| 70 | 58 | } |
| 71 | 59 | |
| 72 | 60 | /** |
| 73 | - * Format price for variable product. Range uses lowest and high prices from all variations | |
| 74 | - * | |
| 75 | - * @param WC_Product_Variable $product | |
| 76 | - * | |
| 77 | - * @return bool|string | |
| 78 | - */ | |
| 79 | - protected function formatPriceForVariableProduct( WC_Product_Variable $product ) { | |
| 80 | - | |
| 81 | - $prices = $product->get_variation_prices( true ); | |
| 82 | - | |
| 83 | - $maxPrice = end( $prices['price'] ); | |
| 84 | - $minPrices = []; | |
| 85 | - | |
| 86 | - foreach ( $product->get_available_variations() as $variation ) { | |
| 87 | - $rules = PriceManager::getPriceRules( $variation['variation_id'] ); | |
| 88 | - | |
| 89 | - if ( ! empty( $rules ) ) { | |
| 90 | - | |
| 91 | - $minPrices[] = $this->getLowestPrice( $rules, $product, false ); | |
| 92 | - } | |
| 93 | - } | |
| 94 | - | |
| 95 | - if ( ! empty( $minPrices ) ) { | |
| 96 | - if ( $this->getDisplayType() === 'range' ) { | |
| 97 | - return wc_price( min( $minPrices ) ) . ' - ' . wc_price( $maxPrice ); | |
| 98 | - } else { | |
| 99 | - return $this->getLowestPrefix() . ' ' . wc_price( min( $minPrices ) ); | |
| 100 | - } | |
| 101 | - } | |
| 102 | - | |
| 103 | - return false; | |
| 104 | - } | |
| 105 | - | |
| 106 | - /** | |
| 107 | 61 | * Get range from lowest to highest price from price rules |
| 108 | 62 | * |
| 109 | 63 | * @param array $rules |
| 110 | 64 | * @param WC_Product $product |
| @@ -111,33 +65,19 @@ | ||
| 111 | 65 | * |
| 112 | 66 | * @return string |
| 113 | 67 | */ |
| 114 | 68 | protected function getRange( $rules, $product ) { |
| 69 | + if ( PriceManager::getPricingType( $product->get_id() ) === 'percentage' ) { | |
| 115 | 70 | |
| 116 | - $lowest = array_pop( $rules ); | |
| 71 | + $lowest = wc_price( PriceManager::getPriceByPercentDiscount( $product->get_price(), | |
| 72 | + array_pop( $rules ) ) ); | |
| 117 | 73 | |
| 118 | - $highest_html = wc_price( wc_get_price_to_display( $product, [ | |
| 119 | - 'price' => $product->get_price(), | |
| 120 | - ] ) ); | |
| 74 | + $highest = wc_price( $product->get_price() ); | |
| 121 | 75 | |
| 122 | - if ( PriceManager::getPricingType( $product->get_id() ) === 'percentage' ) { | |
| 123 | - | |
| 124 | - $lowest_html = wc_price( | |
| 125 | - wc_get_price_to_display( $product, [ | |
| 126 | - 'price' => PriceManager::getPriceByPercentDiscount( $product->get_price(), $lowest ), | |
| 127 | - ] ) | |
| 128 | - ); | |
| 129 | - | |
| 130 | - return $lowest_html . ' - ' . $highest_html; | |
| 76 | + return $lowest . ' - ' . $highest; | |
| 131 | 77 | } |
| 132 | 78 | |
| 133 | - $lowest_html = wc_price( | |
| 134 | - wc_get_price_to_display( $product, [ | |
| 135 | - 'price' => $lowest, | |
| 136 | - ] ) | |
| 137 | - ); | |
| 138 | - | |
| 139 | - return $lowest_html . ' - ' . $highest_html; | |
| 79 | + return wc_price( array_pop( $rules ) ) . ' - ' . wc_price( $product->get_price() ); | |
| 140 | 80 | } |
| 141 | 81 | |
| 142 | 82 | /** |
| 143 | 83 | * Get lowest price from price rules |
| @@ -144,29 +84,19 @@ | ||
| 144 | 84 | * |
| 145 | 85 | * @param array $rules |
| 146 | 86 | * @param WC_Product $product |
| 147 | 87 | * |
| 148 | - * @param bool $html | |
| 149 | - * | |
| 150 | - * @return string|float | |
| 88 | + * @return string | |
| 151 | 89 | */ |
| 152 | - protected function getLowestPrice( $rules, $product, $html = true ) { | |
| 90 | + protected function getLowestPrice( $rules, $product ) { | |
| 153 | 91 | if ( PriceManager::getPricingType( $product->get_id() ) === 'percentage' ) { |
| 154 | - $lowest = PriceManager::getPriceByPercentDiscount( $product->get_price(), | |
| 155 | - array_pop( $rules ) ); | |
| 92 | + $lowest = wc_price( PriceManager::getPriceByPercentDiscount( $product->get_price(), | |
| 93 | + array_shift( $rules ) ) ); | |
| 156 | 94 | } else { |
| 157 | - $lowest = array_pop( $rules ); | |
| 95 | + $lowest = wc_price( array_pop( $rules ) ); | |
| 158 | 96 | } |
| 159 | 97 | |
| 160 | - if ( ! $html ) { | |
| 161 | - return wc_get_price_to_display( $product, [ | |
| 162 | - 'price' => $lowest | |
| 163 | - ] ); | |
| 164 | - } | |
| 165 | - | |
| 166 | - return $this->getLowestPrefix() . ' ' . wc_price( wc_get_price_to_display( $product, [ | |
| 167 | - 'price' => $lowest | |
| 168 | - ] ) ); | |
| 98 | + return $this->getLowestPrefix() . ' ' . $lowest; | |
| 169 | 99 | } |
| 170 | 100 | |
| 171 | 101 | public function getLowestPrefix() { |
| 172 | 102 | return $this->settings->get( 'lowest_prefix', __( 'From', 'tier-pricing-table' ) ); |
| @@ -177,11 +107,7 @@ | ||
| 177 | 107 | } |
| 178 | 108 | |
| 179 | 109 | public function getDisplayType() { |
| 180 | 110 | return $this->settings->get( 'tiered_price_at_catalog_type', 'range' ); |
| 181 | - } | |
| 182 | - | |
| 183 | - public function useForVariable() { | |
| 184 | - return $this->settings->get( 'tiered_price_at_catalog_for_variable', 'yes' ); | |
| 185 | 111 | } |
| 186 | 112 | |
| 187 | 113 | } |