PluginProbe
Tiered Pricing Table for WooCommerce / 2.0
Tiered Pricing Table for WooCommerce v2.0
8.1.0 8.0.2 7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 All 92 releases
← All changes | src/Frontend/CatalogPriceManager.php +15 -91 2.3.2 → 2.0 View file →
@@ -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,21 +37,15 @@
38 37 *
39 38 * @return string
40 39 */
41 40 public function formatPrice( $priceHtml, $product ) {
42 -
43 - $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
44 -
45 - $formatCondition = $this->settings->get( 'tiered_price_at_product_page',
46 - 'no' ) === 'yes' || get_queried_object_id() != $product_id;
47 -
48 - if ( $formatCondition ) {
49 - $displayPriceType = $this->getDisplayType();
50 -
41 + if ( ! is_product() ) {
51 42 if ( in_array( $product->get_type(), [ 'simple', 'variation' ] ) ) {
52 43 $rules = PriceManager::getPriceRules( $product->get_id() );
53 44
54 45 if ( ! empty( $rules ) ) {
46 + $displayPriceType = $this->getDisplayType();
47 +
55 48 if ( $displayPriceType === 'range' ) {
56 49 return $this->getRange( $rules, $product );
57 50 } else {
58 51 return $this->getLowestPrice( $rules, $product );
@@ -58,15 +51,8 @@
58 51 return $this->getLowestPrice( $rules, $product );
59 52 }
60 53 }
61 54 }
62 -
63 - if ( $product instanceof WC_Product_Variable && $this->useForVariable() === 'yes' ) {
64 - $price = $this->formatPriceForVariableProduct( $product );
65 - if ( $price ) {
66 - return $price;
67 - }
68 - }
69 55 }
70 56
71 57 return $priceHtml;
72 58 }
@@ -71,42 +57,8 @@
71 57 return $priceHtml;
72 58 }
73 59
74 60 /**
75 - * Format price for variable product. Range uses lowest and high prices from all variations
76 - *
77 - * @param WC_Product_Variable $product
78 - *
79 - * @return bool|string
80 - */
81 - protected function formatPriceForVariableProduct( WC_Product_Variable $product ) {
82 -
83 - $prices = $product->get_variation_prices( true );
84 -
85 - $maxPrice = end( $prices['price'] );
86 - $minPrices = [];
87 -
88 - foreach ( $product->get_available_variations() as $variation ) {
89 - $rules = PriceManager::getPriceRules( $variation['variation_id'] );
90 -
91 - if ( ! empty( $rules ) ) {
92 -
93 - $minPrices[] = $this->getLowestPrice( $rules, $product, false );
94 - }
95 - }
96 -
97 - if ( ! empty( $minPrices ) ) {
98 - if ( $this->getDisplayType() === 'range' ) {
99 - return wc_price( min( $minPrices ) ) . ' - ' . wc_price( $maxPrice );
100 - } else {
101 - return $this->getLowestPrefix() . ' ' . wc_price( min( $minPrices ) );
102 - }
103 - }
104 -
105 - return false;
106 - }
107 -
108 - /**
109 61 * Get range from lowest to highest price from price rules
110 62 *
111 63 * @param array $rules
112 64 * @param WC_Product $product
@@ -113,33 +65,19 @@
113 65 *
114 66 * @return string
115 67 */
116 68 protected function getRange( $rules, $product ) {
69 + if ( PriceManager::getPricingType( $product->get_id() ) === 'percentage' ) {
117 70
118 - $lowest = array_pop( $rules );
71 + $lowest = wc_price( PriceManager::getPriceByPercentDiscount( $product->get_price(),
72 + array_pop( $rules ) ) );
119 73
120 - $highest_html = wc_price( wc_get_price_to_display( $product, [
121 - 'price' => $product->get_price(),
122 - ] ) );
74 + $highest = wc_price( $product->get_price() );
123 75
124 - if ( PriceManager::getPricingType( $product->get_id() ) === 'percentage' ) {
125 -
126 - $lowest_html = wc_price(
127 - wc_get_price_to_display( $product, [
128 - 'price' => PriceManager::getPriceByPercentDiscount( $product->get_price(), $lowest ),
129 - ] )
130 - );
131 -
132 - return $lowest_html . ' - ' . $highest_html;
76 + return $lowest . ' - ' . $highest;
133 77 }
134 78
135 - $lowest_html = wc_price(
136 - wc_get_price_to_display( $product, [
137 - 'price' => $lowest,
138 - ] )
139 - );
140 -
141 - return $lowest_html . ' - ' . $highest_html;
79 + return wc_price( array_pop( $rules ) ) . ' - ' . wc_price( $product->get_price() );
142 80 }
143 81
144 82 /**
145 83 * Get lowest price from price rules
@@ -146,29 +84,19 @@
146 84 *
147 85 * @param array $rules
148 86 * @param WC_Product $product
149 87 *
150 - * @param bool $html
151 - *
152 - * @return string|float
88 + * @return string
153 89 */
154 - protected function getLowestPrice( $rules, $product, $html = true ) {
90 + protected function getLowestPrice( $rules, $product ) {
155 91 if ( PriceManager::getPricingType( $product->get_id() ) === 'percentage' ) {
156 - $lowest = PriceManager::getPriceByPercentDiscount( $product->get_price(),
157 - array_pop( $rules ) );
92 + $lowest = wc_price( PriceManager::getPriceByPercentDiscount( $product->get_price(),
93 + array_shift( $rules ) ) );
158 94 } else {
159 - $lowest = array_pop( $rules );
95 + $lowest = wc_price( array_pop( $rules ) );
160 96 }
161 97
162 - if ( ! $html ) {
163 - return wc_get_price_to_display( $product, [
164 - 'price' => $lowest
165 - ] );
166 - }
167 -
168 - return $this->getLowestPrefix() . ' ' . wc_price( wc_get_price_to_display( $product, [
169 - 'price' => $lowest
170 - ] ) );
98 + return $this->getLowestPrefix() . ' ' . $lowest;
171 99 }
172 100
173 101 public function getLowestPrefix() {
174 102 return $this->settings->get( 'lowest_prefix', __( 'From', 'tier-pricing-table' ) );
@@ -179,11 +107,7 @@
179 107 }
180 108
181 109 public function getDisplayType() {
182 110 return $this->settings->get( 'tiered_price_at_catalog_type', 'range' );
183 - }
184 -
185 - public function useForVariable() {
186 - return $this->settings->get( 'tiered_price_at_catalog_for_variable', 'yes' );
187 111 }
188 112
189 113 }