PluginProbe
Tiered Pricing Table for WooCommerce / 7.1.5
Tiered Pricing Table for WooCommerce v7.1.5
8.0.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 All 91 releases
tier-pricing-table / views / frontend / tiered-pricing-dropdown.php

tiered-pricing-dropdown.php in Tiered Pricing Table for WooCommerce 7.1.5, at views/frontend/tiered-pricing-dropdown.php

374 lines 12.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use TierPricingTable\CalculationLogic;
4 use TierPricingTable\PriceManager;
5 use TierPricingTable\PricingRule;
6 use TierPricingTable\Settings\Settings;
7
8 if ( ! defined( 'WPINC' ) ) {
9 die;
10 }
11
12 /**
13 * Available variables
14 *
15 * @var array $price_rules
16 * @var PricingRule $pricing_rule
17 * @var string $real_price
18 * @var string $product_name
19 * @var string $pricing_type
20 * @var WC_Product $product
21 * @var string $id
22 * @var int $product_id
23 * @var int $minimum
24 * @var array $settings
25 */
26
27 if ( ! function_exists( 'tptParseOptionText' ) ) {
28 function tptParseOptionText( $text, $quantity, $discount = null, $base_unit_name = null ) {
29 return strtr( $text, array(
30 '{tp_quantity}' => $quantity,
31 '{tp_discount}' => $discount,
32 '{tp_rounded_discount}' => ! is_null( $discount ) ? round( $discount ) : 0,
33 '{tp_base_unit_name}' => $base_unit_name,
34 ) );
35 }
36 }
37
38 $sale_price = $product->get_sale_price();
39
40 if ( $sale_price ) {
41 $sale_price = wc_get_price_to_display( $product, array(
42 'price' => $sale_price,
43 ) );
44 }
45
46 $regular_price = wc_get_price_to_display( $product, array(
47 'price' => $product->get_regular_price(),
48 ) );
49
50 $price = wc_get_price_to_display( $product, array(
51 'price' => $product->get_price(),
52 ) );
53
54 $price_incl_taxes = wc_get_price_including_tax( wc_get_product( $product_id ), array(
55 'price' => $real_price,
56 ) );
57
58 $price_excl_taxes = wc_get_price_excluding_tax( wc_get_product( $product_id ), array(
59 'price' => $real_price,
60 ) );
61
62
63 ?>
64 <?php if ( ! empty( $price_rules ) ) : ?>
65
66 <div class="tiered-pricing-wrapper">
67 <?php if ( ! empty( $settings['title'] ) ) : ?>
68 <h3 style="clear:both; margin: 20px 0;"><?php echo esc_attr( $settings['title'] ); ?></h3>
69 <?php endif; ?>
70
71 <div class="tiered-pricing-dropdown <?php echo ( isset( $settings['compact_layout'] ) && $settings['compact_layout'] === 'yes' ) ? 'tiered-pricing-dropdown--slim' : ''; ?>"
72 id="<?php echo esc_attr( $id ); ?>"
73 data-product-id="<?php echo esc_attr( $product_id ); ?>"
74 data-price-rules="<?php echo esc_attr( htmlspecialchars( json_encode( $price_rules ) ) ); ?>"
75 data-minimum="<?php echo esc_attr( $minimum ); ?>"
76 data-product-name="<?php echo esc_attr( $product_name ); ?>"
77 data-regular-price="<?php echo esc_attr( $regular_price ); ?>"
78 data-sale-price="<?php echo esc_attr( $sale_price ); ?>"
79 data-price="<?php echo esc_attr( $price ); ?>"
80 data-product-price-suffix="<?php echo esc_attr( $product->get_price_suffix() ); ?>"
81 >
82 <?php
83 $discountAmount = 0;
84 if ( CalculationLogic::calculateDiscountBasedOnRegularPrice() && $product->is_on_sale() ) {
85 $discountAmount = PriceManager::calculateDiscount( $product->get_regular_price(),
86 $product->get_sale_price() );
87 }
88 ?>
89
90 <div class="tiered-pricing-dropdown__select-box" tabindex="0" role="combobox" aria-haspopup="listbox" aria-expanded="false" aria-controls="tiered-pricing-dropdown-list-<?php echo esc_attr( $id ); ?>">
91 <div class="tiered-pricing-dropdown-option">
92 <div class="tiered-pricing-dropdown-option__quantity">
93
94 <?php if ( 1 >= array_keys( $price_rules )[0] - $minimum || 'static' === $settings['quantity_type'] ) : ?>
95 <?php
96 $quantity = esc_attr( number_format_i18n( $minimum ) . ' ' );
97 $baseUnitName = $settings['quantity_measurement_singular'];
98 ?>
99 <?php else : ?>
100 <?php
101 $quantity = esc_attr( number_format_i18n( $minimum ) . ' - ' . number_format_i18n( array_keys( $price_rules )[0] - 1 ) . ' ' );
102 $baseUnitName = $settings['quantity_measurement_plural'];
103 ?>
104 <?php endif; ?>
105
106 <?php if ( $discountAmount > 0 ) : ?>
107 <?php
108 echo wp_kses_post( tptParseOptionText( $settings['options_option_text'], $quantity,
109 $discountAmount, $baseUnitName ) );
110 ?>
111 <?php else : ?>
112 <?php
113 echo wp_kses_post( tptParseOptionText( $settings['options_default_option_text'], $quantity,
114 null, $baseUnitName ) );
115 ?>
116 <?php endif; ?>
117 </div>
118
119 <?php
120 do_action( 'tiered_pricing_table/dropdown/label', $pricing_rule, $minimum, array(
121 'id' => $id,
122 'style' => 'default',
123 ) );
124 ?>
125
126 <div class="tiered-pricing-dropdown-option__pricing">
127 <div class="tiered-pricing-option-price">
128
129 <?php if ( $discountAmount > 0 ) : ?>
130 <div class="tiered-pricing-dropdown-option-price__original">
131 <del>
132 <?php
133 echo wp_kses_post( wc_price( wc_get_price_to_display( $product, array(
134 'price' => $regular_price,
135 ) ) ) );
136 ?>
137 </del>
138 </div>
139 <?php endif; ?>
140
141 <div class="tiered-pricing-dropdown-option-price__discounted">
142 <?php
143 echo wp_kses_post( wc_price( wc_get_price_to_display( wc_get_product( $product_id ),
144 array(
145 'price' => $real_price,
146 ) ) ) );
147 ?>
148 </div>
149 </div>
150 </div>
151 </div>
152 <div class="tiered-pricing-dropdown__select-box-arrow">
153 <svg height="24" viewBox="0 0 48 48" width="24" xmlns="http://www.w3.org/2000/svg">
154 <path d="M14.83 16.42l9.17 9.17 9.17-9.17 2.83 2.83-12 12-12-12z"/>
155 <path d="M0-.75h48v48h-48z" fill="none"/>
156 </svg>
157 </div>
158 </div>
159
160 <div class="tiered-pricing-dropdown__list">
161 <ul role="listbox" id="tiered-pricing-dropdown-list-<?php echo esc_attr( $id ); ?>">
162 <li class="tiered-pricing-dropdown-option tiered-pricing-dropdown-option--active tiered-pricing-dropdown-option--default"
163 role="option"
164 aria-selected="true"
165 data-tiered-quantity="<?php echo esc_attr( $minimum ); ?>"
166 data-tiered-price="<?php echo esc_attr( $price ); ?>"
167 data-tiered-price-exclude-taxes="<?php echo esc_attr( $price_excl_taxes ); ?>"
168 data-tiered-price-include-taxes="<?php echo esc_attr( $price_incl_taxes ); ?>">
169
170 <div class="tiered-pricing-dropdown-option__quantity">
171 <?php if ( 1 >= array_keys( $price_rules )[0] - $minimum || 'static' === $settings['quantity_type'] ) : ?>
172 <?php
173 $quantity = esc_attr( number_format_i18n( $minimum ) . ' ' );
174 $baseUnitName = $settings['quantity_measurement_singular'];
175 ?>
176 <?php else : ?>
177 <?php
178 $quantity = esc_attr( number_format_i18n( $minimum ) . ' - ' . number_format_i18n( array_keys( $price_rules )[0] - 1 ) . ' ' );
179 $baseUnitName = $settings['quantity_measurement_plural'];
180 ?>
181 <?php endif; ?>
182
183 <?php if ( $discountAmount > 0 ) : ?>
184 <?php
185 echo wp_kses_post( tptParseOptionText( $settings['options_option_text'], $quantity,
186 $discountAmount, $baseUnitName ) );
187 ?>
188 <?php else : ?>
189 <?php
190 echo wp_kses_post( tptParseOptionText( $settings['options_default_option_text'],
191 $quantity, null, $baseUnitName ) );
192 ?>
193 <?php endif; ?>
194 </div>
195
196 <?php
197 do_action( 'tiered_pricing_table/dropdown/label', $pricing_rule, $minimum, array(
198 'id' => $id,
199 'style' => 'default',
200 ) );
201 ?>
202
203 <div class="tiered-pricing-dropdown-option__pricing">
204 <div class="tiered-pricing-option-price">
205
206 <?php if ( $discountAmount > 0 ) : ?>
207 <div class="tiered-pricing-dropdown-option-price__original">
208 <del>
209 <?php
210 echo wp_kses_post( wc_price( wc_get_price_to_display( $product, array(
211 'price' => $regular_price,
212 ) ) ) );
213 ?>
214 </del>
215 </div>
216 <?php endif; ?>
217
218 <div class="tiered-pricing-dropdown-option-price__discounted">
219 <?php
220 echo wp_kses_post( wc_price( wc_get_price_to_display( wc_get_product( $product_id ),
221 array(
222 'price' => $real_price,
223 ) ) ) );
224 ?>
225 </div>
226 </div>
227 </div>
228 </li>
229 <?php $iterator = new ArrayIterator( $price_rules ); ?>
230
231 <?php while ( $iterator->valid() ) : ?>
232 <?php
233 $currentPrice = $iterator->current();
234 $currentQuantity = $iterator->key();
235
236 if ( 'percentage' === $pricing_type ) {
237 $discountAmount = $currentPrice;
238 } else {
239 $discountAmount = PriceManager::calculateDiscount( CalculationLogic::calculateDiscountBasedOnRegularPrice() ? $product->get_regular_price() : $product->get_price(),
240 $pricing_rule->getTierPrice( $currentQuantity, false ) );
241 }
242
243 $iterator->next();
244
245 if ( $iterator->valid() ) {
246 $quantity = $currentQuantity;
247
248 if ( intval( $iterator->key() - 1 != $currentQuantity ) ) {
249
250 $quantity = number_format_i18n( $quantity );
251
252 if ( 'range' === $settings['quantity_type'] ) {
253 $quantity .= ' - ' . number_format_i18n( intval( $iterator->key() - 1 ) );
254 }
255 }
256 } else {
257 $quantity = number_format_i18n( $currentQuantity );
258
259 $quantity .= apply_filters( 'tiered_pricing_table/tiered_pricing/last_tier_postfix', '+',
260 $currentQuantity, $pricing_rule, 'blocks' );
261 }
262
263 $currentProductPrice = PriceManager::getPriceByRules( $currentQuantity, $product_id );
264
265 $currentProductPriceExcludeTaxes = wc_get_price_excluding_tax( wc_get_product( $product_id ),
266 array(
267 'price' => PriceManager::getPriceByRules( $currentQuantity, $product_id, null, null,
268 false ),
269 ) );
270
271 $currentProductPriceIncludeTaxes = wc_get_price_including_tax( wc_get_product( $product_id ),
272 array(
273 'price' => PriceManager::getPriceByRules( $currentQuantity, $product_id, null, null,
274 false ),
275 ) );
276 ?>
277
278 <li class="tiered-pricing-dropdown-option"
279 role="option"
280 aria-selected="false"
281 data-tiered-quantity="<?php echo esc_attr( $currentQuantity ); ?>"
282 data-tiered-price="<?php echo esc_attr( $currentProductPrice ); ?>"
283 data-tiered-price-exclude-taxes="<?php echo esc_attr( $currentProductPriceExcludeTaxes ); ?>"
284 data-tiered-price-include-taxes="<?php echo esc_attr( $currentProductPriceIncludeTaxes ); ?>">
285
286 <div class="tiered-pricing-dropdown-option__quantity">
287 <?php
288 echo wp_kses_post( tptParseOptionText( $settings['options_option_text'], $quantity,
289 round( $discountAmount, 2 ), $settings['quantity_measurement_plural'] ) );
290 ?>
291 </div>
292
293 <?php
294 do_action( 'tiered_pricing_table/dropdown/label', $pricing_rule, $currentQuantity, array(
295 'id' => $id,
296 'style' => 'default',
297 ) );
298 ?>
299
300 <div class="tiered-pricing-dropdown-option__pricing">
301 <div class="tiered-pricing-option-price">
302 <div class="tiered-pricing-dropdown-option-price__original">
303 <del>
304 <?php
305 echo wp_kses_post( wc_price( wc_get_price_to_display( $product, array(
306 'price' => CalculationLogic::calculateDiscountBasedOnRegularPrice() ? $regular_price : $real_price,
307 ) ) ) );
308 ?>
309 </del>
310 </div>
311 <div class="tiered-pricing-dropdown-option-price__discounted">
312 <?php
313 echo wp_kses_post( wc_price( PriceManager::getPriceByRules( $currentQuantity,
314 $product_id ) ) );
315 ?>
316 </div>
317 </div>
318 </div>
319 </li>
320 <?php endwhile; ?>
321 <?php do_action( 'tiered_pricing_table/dropdown/options', $pricing_rule ); ?>
322 </ul>
323
324 <?php do_action( 'tiered_pricing_table/dropdown/after_options', $pricing_rule ); ?>
325 </div>
326 </div>
327 </div>
328
329 <style>
330 <?php
331 $backgroundColor = Settings::hex2rgba($settings['active_tier_color'], 0.05);
332
333 $clickableCSS = <<<EOD
334 .tiered-pricing-dropdown__list .tiered-pricing-dropdown-option:hover,
335 .tiered-pricing-dropdown__list .tiered-pricing-dropdown-option:focus {
336 background: $backgroundColor;
337 cursor: pointer;
338 }
339 EOD;
340 $CSS = <<<EOD
341 #$id .tiered-pricing-dropdown__list, #$id .tiered-pricing-dropdown__select-box {
342 border-color: {$settings['active_tier_color']};
343 }
344 #$id .tiered-pricing-dropdown__select-box-arrow svg {
345 fill: {$settings['active_tier_color']};
346 }
347 #$id .tiered-pricing--active .tiered-pricing-option-checkbox::after {
348 background: {$settings['active_tier_color']};
349 }
350 #{$id} .tiered-pricing-dropdown__list .tiered-pricing--active {
351 background: $backgroundColor;
352 }
353 EOD;
354
355 $hideOriginalPriceCSS = <<<EOD
356 #$id .tiered-pricing-dropdown-option-price__original {
357 display: none
358 }
359 EOD;
360
361 echo esc_html($CSS);
362
363 if (tpt_fs()->is__premium_only() && $settings['clickable_rows']) {
364 echo esc_html($clickableCSS);
365 }
366
367 if (! $settings['options_show_original_product_price']) {
368 echo esc_html($hideOriginalPriceCSS);
369 }
370 ?>
371 </style>
372 <?php endif; ?>
373
374