PluginProbe
Tiered Pricing Table for WooCommerce / 2.1.2
Tiered Pricing Table for WooCommerce v2.1.2
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 2.3.3 All 91 releases
← All changes | src/Frontend/CatalogPriceManager.php +59 -4 2.1 → 2.1.2 View file →
@@ -2,8 +2,9 @@
2 2
3 3 use TierPricingTable\Settings\Settings;
4 4 use WC_Product;
5 5 use TierPricingTable\PriceManager;
6 +use WC_Product_Variable;
6 7
7 8 /**
8 9 * Class CatalogPriceManager
9 10 *
@@ -38,14 +39,14 @@
38 39 * @return string
39 40 */
40 41 public function formatPrice( $priceHtml, $product ) {
41 42 if ( ! is_product() ) {
43 + $displayPriceType = $this->getDisplayType();
44 +
42 45 if ( in_array( $product->get_type(), [ 'simple', 'variation' ] ) ) {
43 46 $rules = PriceManager::getPriceRules( $product->get_id() );
44 47
45 48 if ( ! empty( $rules ) ) {
46 - $displayPriceType = $this->getDisplayType();
47 -
48 49 if ( $displayPriceType === 'range' ) {
49 50 return $this->getRange( $rules, $product );
50 51 } else {
51 52 return $this->getLowestPrice( $rules, $product );
@@ -51,8 +52,15 @@
51 52 return $this->getLowestPrice( $rules, $product );
52 53 }
53 54 }
54 55 }
56 +
57 + if ( $product instanceof WC_Product_Variable && $this->useForVariable() === 'yes' ) {
58 + $price = $this->formatPriceForVariableProduct( $product );
59 + if ( $price ) {
60 + return $price;
61 + }
62 + }
55 63 }
56 64
57 65 return $priceHtml;
58 66 }
@@ -57,8 +65,42 @@
57 65 return $priceHtml;
58 66 }
59 67
60 68 /**
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 + /**
61 103 * Get range from lowest to highest price from price rules
62 104 *
63 105 * @param array $rules
64 106 * @param WC_Product $product
@@ -65,8 +107,9 @@
65 107 *
66 108 * @return string
67 109 */
68 110 protected function getRange( $rules, $product ) {
111 +
69 112 $lowest = array_pop( $rules );
70 113
71 114 $highest_html = wc_price( wc_get_price_to_display( $product, [
72 115 'price' => $product->get_price(),
@@ -97,11 +140,13 @@
97 140 *
98 141 * @param array $rules
99 142 * @param WC_Product $product
100 143 *
101 - * @return string
144 + * @param bool $html
145 + *
146 + * @return string|float
102 147 */
103 - protected function getLowestPrice( $rules, $product ) {
148 + protected function getLowestPrice( $rules, $product, $html = true ) {
104 149 if ( PriceManager::getPricingType( $product->get_id() ) === 'percentage' ) {
105 150 $lowest = PriceManager::getPriceByPercentDiscount( $product->get_price(),
106 151 array_shift( $rules ) );
107 152 } else {
@@ -107,8 +152,14 @@
107 152 } else {
108 153 $lowest = array_pop( $rules );
109 154 }
110 155
156 + if ( ! $html ) {
157 + return wc_get_price_to_display( $product, [
158 + 'price' => $lowest
159 + ] );
160 + }
161 +
111 162 return $this->getLowestPrefix() . ' ' . wc_price( wc_get_price_to_display( $product, [
112 163 'price' => $lowest
113 164 ] ) );
114 165 }
@@ -122,7 +173,11 @@
122 173 }
123 174
124 175 public function getDisplayType() {
125 176 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' );
126 181 }
127 182
128 183 }