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 +65 -6 2.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,8 +111,9 @@
65 111 *
66 112 * @return string
67 113 */
68 114 protected function getRange( $rules, $product ) {
115 +
69 116 $lowest = array_pop( $rules );
70 117
71 118 $highest_html = wc_price( wc_get_price_to_display( $product, [
72 119 'price' => $product->get_price(),
@@ -97,18 +144,26 @@
97 144 *
98 145 * @param array $rules
99 146 * @param WC_Product $product
100 147 *
101 - * @return string
148 + * @param bool $html
149 + *
150 + * @return string|float
102 151 */
103 - protected function getLowestPrice( $rules, $product ) {
152 + protected function getLowestPrice( $rules, $product, $html = true ) {
104 153 if ( PriceManager::getPricingType( $product->get_id() ) === 'percentage' ) {
105 154 $lowest = PriceManager::getPriceByPercentDiscount( $product->get_price(),
106 - array_shift( $rules ) );
155 + array_pop( $rules ) );
107 156 } else {
108 157 $lowest = array_pop( $rules );
109 158 }
110 159
160 + if ( ! $html ) {
161 + return wc_get_price_to_display( $product, [
162 + 'price' => $lowest
163 + ] );
164 + }
165 +
111 166 return $this->getLowestPrefix() . ' ' . wc_price( wc_get_price_to_display( $product, [
112 167 'price' => $lowest
113 168 ] ) );
114 169 }
@@ -122,7 +177,11 @@
122 177 }
123 178
124 179 public function getDisplayType() {
125 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' );
126 185 }
127 186
128 187 }