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
tier-pricing-table / src / Frontend / CatalogPriceManager.php

CatalogPriceManager.php in Tiered Pricing Table for WooCommerce 2.2.2, at src/Frontend/CatalogPriceManager.php

187 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable\Frontend;
2
3 use TierPricingTable\Settings\Settings;
4 use WC_Product;
5 use TierPricingTable\PriceManager;
6 use WC_Product_Variable;
7
8 /**
9 * Class CatalogPriceManager
10 *
11 * @package TierPricingTable
12 */
13 class CatalogPriceManager {
14
15 /**
16 * @var Settings
17 */
18 private $settings;
19
20 /**
21 * CatalogPriceManager constructor.
22 *
23 * @param Settings $settings
24 */
25 public function __construct( Settings $settings ) {
26 $this->settings = $settings;
27
28 if ( $this->isEnable() && ! is_admin() ) {
29 add_filter( 'woocommerce_get_price_html', [ $this, 'formatPrice' ], 999, 2 );
30 }
31 }
32
33 /**
34 * Change logic showing prince at catalog for product with tiered price rules
35 *
36 * @param string $priceHtml
37 * @param WC_Product $product
38 *
39 * @return string
40 */
41 public function formatPrice( $priceHtml, $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
49 if ( in_array( $product->get_type(), [ 'simple', 'variation' ] ) ) {
50 $rules = PriceManager::getPriceRules( $product->get_id() );
51
52 if ( ! empty( $rules ) ) {
53 if ( $displayPriceType === 'range' ) {
54 return $this->getRange( $rules, $product );
55 } else {
56 return $this->getLowestPrice( $rules, $product );
57 }
58 }
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 }
67 }
68
69 return $priceHtml;
70 }
71
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 /**
107 * Get range from lowest to highest price from price rules
108 *
109 * @param array $rules
110 * @param WC_Product $product
111 *
112 * @return string
113 */
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
122 if ( PriceManager::getPricingType( $product->get_id() ) === 'percentage' ) {
123
124 $lowest_html = wc_price(
125 wc_get_price_to_display( $product, [
126 'price' => PriceManager::getPriceByPercentDiscount( $product->get_price(), $lowest ),
127 ] )
128 );
129
130 return $lowest_html . ' - ' . $highest_html;
131 }
132
133 $lowest_html = wc_price(
134 wc_get_price_to_display( $product, [
135 'price' => $lowest,
136 ] )
137 );
138
139 return $lowest_html . ' - ' . $highest_html;
140 }
141
142 /**
143 * Get lowest price from price rules
144 *
145 * @param array $rules
146 * @param WC_Product $product
147 *
148 * @param bool $html
149 *
150 * @return string|float
151 */
152 protected function getLowestPrice( $rules, $product, $html = true ) {
153 if ( PriceManager::getPricingType( $product->get_id() ) === 'percentage' ) {
154 $lowest = PriceManager::getPriceByPercentDiscount( $product->get_price(),
155 array_pop( $rules ) );
156 } else {
157 $lowest = array_pop( $rules );
158 }
159
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 ] ) );
169 }
170
171 public function getLowestPrefix() {
172 return $this->settings->get( 'lowest_prefix', __( 'From', 'tier-pricing-table' ) );
173 }
174
175 public function isEnable() {
176 return $this->settings->get( 'tiered_price_at_catalog', 'yes' ) === 'yes';
177 }
178
179 public function getDisplayType() {
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' );
185 }
186
187 }