PluginProbe
Tiered Pricing Table for WooCommerce / 2.3.0
Tiered Pricing Table for WooCommerce v2.3.0
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.3.0, at src/Frontend/CatalogPriceManager.php

189 lines 4.5 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 $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
51 if ( in_array( $product->get_type(), [ 'simple', 'variation' ] ) ) {
52 $rules = PriceManager::getPriceRules( $product->get_id() );
53
54 if ( ! empty( $rules ) ) {
55 if ( $displayPriceType === 'range' ) {
56 return $this->getRange( $rules, $product );
57 } else {
58 return $this->getLowestPrice( $rules, $product );
59 }
60 }
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 }
69 }
70
71 return $priceHtml;
72 }
73
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 /**
109 * Get range from lowest to highest price from price rules
110 *
111 * @param array $rules
112 * @param WC_Product $product
113 *
114 * @return string
115 */
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
124 if ( PriceManager::getPricingType( $product->get_id() ) === 'percentage' ) {
125
126 $lowest_html = wc_price(
127 wc_get_price_to_display( $product, [
128 'price' => PriceManager::getPriceByPercentDiscount( $product->get_price(), $lowest ),
129 ] )
130 );
131
132 return $lowest_html . ' - ' . $highest_html;
133 }
134
135 $lowest_html = wc_price(
136 wc_get_price_to_display( $product, [
137 'price' => $lowest,
138 ] )
139 );
140
141 return $lowest_html . ' - ' . $highest_html;
142 }
143
144 /**
145 * Get lowest price from price rules
146 *
147 * @param array $rules
148 * @param WC_Product $product
149 *
150 * @param bool $html
151 *
152 * @return string|float
153 */
154 protected function getLowestPrice( $rules, $product, $html = true ) {
155 if ( PriceManager::getPricingType( $product->get_id() ) === 'percentage' ) {
156 $lowest = PriceManager::getPriceByPercentDiscount( $product->get_price(),
157 array_pop( $rules ) );
158 } else {
159 $lowest = array_pop( $rules );
160 }
161
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 ] ) );
171 }
172
173 public function getLowestPrefix() {
174 return $this->settings->get( 'lowest_prefix', __( 'From', 'tier-pricing-table' ) );
175 }
176
177 public function isEnable() {
178 return $this->settings->get( 'tiered_price_at_catalog', 'yes' ) === 'yes';
179 }
180
181 public function getDisplayType() {
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' );
187 }
188
189 }