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

183 lines 4.3 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 if ( ! is_product() ) {
43 $displayPriceType = $this->getDisplayType();
44
45 if ( in_array( $product->get_type(), [ 'simple', 'variation' ] ) ) {
46 $rules = PriceManager::getPriceRules( $product->get_id() );
47
48 if ( ! empty( $rules ) ) {
49 if ( $displayPriceType === 'range' ) {
50 return $this->getRange( $rules, $product );
51 } else {
52 return $this->getLowestPrice( $rules, $product );
53 }
54 }
55 }
56
57 if ( $product instanceof WC_Product_Variable && $this->useForVariable() === 'yes' ) {
58 $price = $this->formatPriceForVariableProduct( $product );
59 if ( $price ) {
60 return $price;
61 }
62 }
63 }
64
65 return $priceHtml;
66 }
67
68 /**
69 * Format price for variable product. Range uses lowest and high prices from all variations
70 *
71 * @param WC_Product_Variable $product
72 *
73 * @return bool|string
74 */
75 protected function formatPriceForVariableProduct( WC_Product_Variable $product ) {
76
77 $prices = $product->get_variation_prices( true );
78
79 $maxPrice = end( $prices['price'] );
80 $minPrices = [];
81
82 foreach ( $product->get_available_variations() as $variation ) {
83 $rules = PriceManager::getPriceRules( $variation['variation_id'] );
84
85 if ( ! empty( $rules ) ) {
86
87 $minPrices[] = $this->getLowestPrice( $rules, $product, false );
88 }
89 }
90
91 if ( ! empty( $minPrices ) ) {
92 if ( $this->getDisplayType() === 'range' ) {
93 return wc_price( min( $minPrices ) ) . ' - ' . wc_price( $maxPrice );
94 } else {
95 return $this->getLowestPrefix() . ' ' . wc_price( min( $minPrices ) );
96 }
97 }
98
99 return false;
100 }
101
102 /**
103 * Get range from lowest to highest price from price rules
104 *
105 * @param array $rules
106 * @param WC_Product $product
107 *
108 * @return string
109 */
110 protected function getRange( $rules, $product ) {
111
112 $lowest = array_pop( $rules );
113
114 $highest_html = wc_price( wc_get_price_to_display( $product, [
115 'price' => $product->get_price(),
116 ] ) );
117
118 if ( PriceManager::getPricingType( $product->get_id() ) === 'percentage' ) {
119
120 $lowest_html = wc_price(
121 wc_get_price_to_display( $product, [
122 'price' => PriceManager::getPriceByPercentDiscount( $product->get_price(), $lowest ),
123 ] )
124 );
125
126 return $lowest_html . ' - ' . $highest_html;
127 }
128
129 $lowest_html = wc_price(
130 wc_get_price_to_display( $product, [
131 'price' => $lowest,
132 ] )
133 );
134
135 return $lowest_html . ' - ' . $highest_html;
136 }
137
138 /**
139 * Get lowest price from price rules
140 *
141 * @param array $rules
142 * @param WC_Product $product
143 *
144 * @param bool $html
145 *
146 * @return string|float
147 */
148 protected function getLowestPrice( $rules, $product, $html = true ) {
149 if ( PriceManager::getPricingType( $product->get_id() ) === 'percentage' ) {
150 $lowest = PriceManager::getPriceByPercentDiscount( $product->get_price(),
151 array_pop( $rules ) );
152 } else {
153 $lowest = array_pop( $rules );
154 }
155
156 if ( ! $html ) {
157 return wc_get_price_to_display( $product, [
158 'price' => $lowest
159 ] );
160 }
161
162 return $this->getLowestPrefix() . ' ' . wc_price( wc_get_price_to_display( $product, [
163 'price' => $lowest
164 ] ) );
165 }
166
167 public function getLowestPrefix() {
168 return $this->settings->get( 'lowest_prefix', __( 'From', 'tier-pricing-table' ) );
169 }
170
171 public function isEnable() {
172 return $this->settings->get( 'tiered_price_at_catalog', 'yes' ) === 'yes';
173 }
174
175 public function getDisplayType() {
176 return $this->settings->get( 'tiered_price_at_catalog_type', 'range' );
177 }
178
179 public function useForVariable() {
180 return $this->settings->get( 'tiered_price_at_catalog_for_variable', 'yes' );
181 }
182
183 }