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