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