PluginProbe
Tiered Pricing Table for WooCommerce / 2.2.2
Tiered Pricing Table for WooCommerce v2.2.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 2.3.4 All 90 releases
← All changes | src/Frontend/CatalogPriceManager.php +89 -15 2.0.12.2.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 *
@@ -37,15 +38,19 @@
37 38 *
38 39 * @return string
39 40 */
40 41 public function formatPrice( $priceHtml, $product ) {
41 - if ( ! is_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 +
42 49 if ( in_array( $product->get_type(), [ 'simple', 'variation' ] ) ) {
43 50 $rules = PriceManager::getPriceRules( $product->get_id() );
44 51
45 52 if ( ! empty( $rules ) ) {
46 - $displayPriceType = $this->getDisplayType();
47 -
48 53 if ( $displayPriceType === 'range' ) {
49 54 return $this->getRange( $rules, $product );
50 55 } else {
51 56 return $this->getLowestPrice( $rules, $product );
@@ -51,8 +56,15 @@
51 56 return $this->getLowestPrice( $rules, $product );
52 57 }
53 58 }
54 59 }
60 +
61 + if ( $product instanceof WC_Product_Variable && $this->useForVariable() === 'yes' ) {
62 + $price = $this->formatPriceForVariableProduct( $product );
63 + if ( $price ) {
64 + return $price;
65 + }
66 + }
55 67 }
56 68
57 69 return $priceHtml;
58 70 }
@@ -57,8 +69,42 @@
57 69 return $priceHtml;
58 70 }
59 71
60 72 /**
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 + /**
61 107 * Get range from lowest to highest price from price rules
62 108 *
63 109 * @param array $rules
64 110 * @param WC_Product $product
@@ -65,19 +111,33 @@
65 111 *
66 112 * @return string
67 113 */
68 114 protected function getRange( $rules, $product ) {
115 +
116 + $lowest = array_pop( $rules );
117 +
118 + $highest_html = wc_price( wc_get_price_to_display( $product, [
119 + 'price' => $product->get_price(),
120 + ] ) );
121 +
69 122 if ( PriceManager::getPricingType( $product->get_id() ) === 'percentage' ) {
70 123
71 - $lowest = wc_price( PriceManager::getPriceByPercentDiscount( $product->get_price(),
72 - array_pop( $rules ) ) );
124 + $lowest_html = wc_price(
125 + wc_get_price_to_display( $product, [
126 + 'price' => PriceManager::getPriceByPercentDiscount( $product->get_price(), $lowest ),
127 + ] )
128 + );
73 129
74 - $highest = wc_price( $product->get_price() );
130 + return $lowest_html . ' - ' . $highest_html;
131 + }
75 132
76 - return $lowest . ' - ' . $highest;
77 - }
133 + $lowest_html = wc_price(
134 + wc_get_price_to_display( $product, [
135 + 'price' => $lowest,
136 + ] )
137 + );
78 138
79 - return wc_price( array_pop( $rules ) ) . ' - ' . wc_price( $product->get_price() );
139 + return $lowest_html . ' - ' . $highest_html;
80 140 }
81 141
82 142 /**
83 143 * Get lowest price from price rules
@@ -84,19 +144,29 @@
84 144 *
85 145 * @param array $rules
86 146 * @param WC_Product $product
87 147 *
88 - * @return string
148 + * @param bool $html
149 + *
150 + * @return string|float
89 151 */
90 - protected function getLowestPrice( $rules, $product ) {
152 + protected function getLowestPrice( $rules, $product, $html = true ) {
91 153 if ( PriceManager::getPricingType( $product->get_id() ) === 'percentage' ) {
92 - $lowest = wc_price( PriceManager::getPriceByPercentDiscount( $product->get_price(),
93 - array_shift( $rules ) ) );
154 + $lowest = PriceManager::getPriceByPercentDiscount( $product->get_price(),
155 + array_pop( $rules ) );
94 156 } else {
95 - $lowest = wc_price( array_pop( $rules ) );
157 + $lowest = array_pop( $rules );
96 158 }
97 159
98 - return $this->getLowestPrefix() . ' ' . $lowest;
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 + ] ) );
99 169 }
100 170
101 171 public function getLowestPrefix() {
102 172 return $this->settings->get( 'lowest_prefix', __( 'From', 'tier-pricing-table' ) );
@@ -107,7 +177,11 @@
107 177 }
108 178
109 179 public function getDisplayType() {
110 180 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' );
111 185 }
112 186
113 187 }