PluginProbe
Tiered Pricing Table for WooCommerce / 8.0.2
Tiered Pricing Table for WooCommerce v8.0.2
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-style-1.php

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

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