PluginProbe
Tiered Pricing Table for WooCommerce / 6.4.0
Tiered Pricing Table for WooCommerce v6.4.0
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 6.4.0, at views/frontend/tiered-pricing-dropdown.php

369 lines 12.1 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"
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">
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>
162 <li class="tiered-pricing-dropdown-option tiered-pricing-dropdown-option--active tiered-pricing-dropdown-option--default"
163 data-tiered-quantity="<?php echo esc_attr( $minimum ); ?>"
164 data-tiered-price="<?php echo esc_attr( $price ); ?>"
165 data-tiered-price-exclude-taxes="<?php echo esc_attr( $price_excl_taxes ); ?>"
166 data-tiered-price-include-taxes="<?php echo esc_attr( $price_incl_taxes ); ?>">
167
168 <div class="tiered-pricing-dropdown-option__quantity">
169 <?php if ( 1 >= array_keys( $price_rules )[0] - $minimum || 'static' === $settings['quantity_type'] ) : ?>
170 <?php
171 $quantity = esc_attr( number_format_i18n( $minimum ) . ' ' );
172 $baseUnitName = $settings['quantity_measurement_singular'];
173 ?>
174 <?php else : ?>
175 <?php
176 $quantity = esc_attr( number_format_i18n( $minimum ) . ' - ' . number_format_i18n( array_keys( $price_rules )[0] - 1 ) . ' ' );
177 $baseUnitName = $settings['quantity_measurement_plural'];
178 ?>
179 <?php endif; ?>
180
181 <?php if ( $discountAmount > 0 ) : ?>
182 <?php
183 echo wp_kses_post( tptParseOptionText( $settings['options_option_text'], $quantity,
184 $discountAmount, $baseUnitName ) );
185 ?>
186 <?php else : ?>
187 <?php
188 echo wp_kses_post( tptParseOptionText( $settings['options_default_option_text'],
189 $quantity, null, $baseUnitName ) );
190 ?>
191 <?php endif; ?>
192 </div>
193
194 <?php
195 do_action( 'tiered_pricing_table/dropdown/label', $pricing_rule, $minimum, array(
196 'id' => $id,
197 'style' => 'default',
198 ) );
199 ?>
200
201 <div class="tiered-pricing-dropdown-option__pricing">
202 <div class="tiered-pricing-option-price">
203
204 <?php if ( $discountAmount > 0 ) : ?>
205 <div class="tiered-pricing-dropdown-option-price__original">
206 <del>
207 <?php
208 echo wp_kses_post( wc_price( wc_get_price_to_display( $product, array(
209 'price' => $regular_price,
210 ) ) ) );
211 ?>
212 </del>
213 </div>
214 <?php endif; ?>
215
216 <div class="tiered-pricing-dropdown-option-price__discounted">
217 <?php
218 echo wp_kses_post( wc_price( wc_get_price_to_display( wc_get_product( $product_id ),
219 array(
220 'price' => $real_price,
221 ) ) ) );
222 ?>
223 </div>
224 </div>
225 </div>
226 </li>
227 <?php $iterator = new ArrayIterator( $price_rules ); ?>
228
229 <?php while ( $iterator->valid() ) : ?>
230 <?php
231 $currentPrice = $iterator->current();
232 $currentQuantity = $iterator->key();
233
234 if ( 'percentage' === $pricing_type ) {
235 $discountAmount = $currentPrice;
236 } else {
237 $discountAmount = PriceManager::calculateDiscount( CalculationLogic::calculateDiscountBasedOnRegularPrice() ? $product->get_regular_price() : $product->get_price(),
238 $pricing_rule->getTierPrice( $currentQuantity, false ) );
239 }
240
241 $iterator->next();
242
243 if ( $iterator->valid() ) {
244 $quantity = $currentQuantity;
245
246 if ( intval( $iterator->key() - 1 != $currentQuantity ) ) {
247
248 $quantity = number_format_i18n( $quantity );
249
250 if ( 'range' === $settings['quantity_type'] ) {
251 $quantity .= ' - ' . number_format_i18n( intval( $iterator->key() - 1 ) );
252 }
253 }
254 } else {
255 $quantity = number_format_i18n( $currentQuantity );
256
257 $quantity .= apply_filters( 'tiered_pricing_table/tiered_pricing/last_tier_postfix', '+',
258 $currentQuantity, $pricing_rule, 'blocks' );
259 }
260
261 $currentProductPrice = PriceManager::getPriceByRules( $currentQuantity, $product_id );
262
263 $currentProductPriceExcludeTaxes = wc_get_price_excluding_tax( wc_get_product( $product_id ),
264 array(
265 'price' => PriceManager::getPriceByRules( $currentQuantity, $product_id, null, null,
266 false ),
267 ) );
268
269 $currentProductPriceIncludeTaxes = wc_get_price_including_tax( wc_get_product( $product_id ),
270 array(
271 'price' => PriceManager::getPriceByRules( $currentQuantity, $product_id, null, null,
272 false ),
273 ) );
274 ?>
275
276 <li class="tiered-pricing-dropdown-option"
277 data-tiered-quantity="<?php echo esc_attr( $currentQuantity ); ?>"
278 data-tiered-price="<?php echo esc_attr( $currentProductPrice ); ?>"
279 data-tiered-price-exclude-taxes="<?php echo esc_attr( $currentProductPriceExcludeTaxes ); ?>"
280 data-tiered-price-include-taxes="<?php echo esc_attr( $currentProductPriceIncludeTaxes ); ?>">
281
282 <div class="tiered-pricing-dropdown-option__quantity">
283 <?php
284 echo wp_kses_post( tptParseOptionText( $settings['options_option_text'], $quantity,
285 round( $discountAmount, 2 ), $settings['quantity_measurement_plural'] ) );
286 ?>
287 </div>
288
289 <?php
290 do_action( 'tiered_pricing_table/dropdown/label', $pricing_rule, $currentQuantity, array(
291 'id' => $id,
292 'style' => 'default',
293 ) );
294 ?>
295
296 <div class="tiered-pricing-dropdown-option__pricing">
297 <div class="tiered-pricing-option-price">
298 <div class="tiered-pricing-dropdown-option-price__original">
299 <del>
300 <?php
301 echo wp_kses_post( wc_price( wc_get_price_to_display( $product, array(
302 'price' => CalculationLogic::calculateDiscountBasedOnRegularPrice() ? $regular_price : $real_price,
303 ) ) ) );
304 ?>
305 </del>
306 </div>
307 <div class="tiered-pricing-dropdown-option-price__discounted">
308 <?php
309 echo wp_kses_post( wc_price( PriceManager::getPriceByRules( $currentQuantity,
310 $product_id ) ) );
311 ?>
312 </div>
313 </div>
314 </div>
315 </li>
316 <?php endwhile; ?>
317 <?php do_action( 'tiered_pricing_table/dropdown/options', $pricing_rule ); ?>
318 </ul>
319
320 <?php do_action( 'tiered_pricing_table/dropdown/after_options', $pricing_rule ); ?>
321 </div>
322 </div>
323 </div>
324
325 <style>
326 <?php
327 $backgroundColor = Settings::hex2rgba($settings['active_tier_color'], 0.05);
328
329 $clickableCSS = <<<EOD
330 .tiered-pricing-dropdown__list .tiered-pricing-dropdown-option:hover {
331 background: $backgroundColor;
332 cursor: pointer;
333 }
334 EOD;
335 $CSS = <<<EOD
336 #$id .tiered-pricing-dropdown__list, #$id .tiered-pricing-dropdown__select-box {
337 border-color: {$settings['active_tier_color']};
338 }
339 #$id .tiered-pricing-dropdown__select-box-arrow svg {
340 fill: {$settings['active_tier_color']};
341 }
342 #$id .tiered-pricing--active .tiered-pricing-option-checkbox::after {
343 background: {$settings['active_tier_color']};
344 }
345 #{$id} .tiered-pricing-dropdown__list .tiered-pricing--active {
346 background: $backgroundColor;
347 }
348 EOD;
349
350 $hideOriginalPriceCSS = <<<EOD
351 #$id .tiered-pricing-dropdown-option-price__original {
352 display: none
353 }
354 EOD;
355
356 echo esc_html($CSS);
357
358 if (tpt_fs()->is__premium_only() && $settings['clickable_rows']) {
359 echo esc_html($clickableCSS);
360 }
361
362 if (! $settings['options_show_original_product_price']) {
363 echo esc_html($hideOriginalPriceCSS);
364 }
365 ?>
366 </style>
367 <?php endif; ?>
368
369