PluginProbe
Tiered Pricing Table for WooCommerce / 8.1.0
Tiered Pricing Table for WooCommerce v8.1.0
8.1.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 All 92 releases
tier-pricing-table / views / frontend / tiered-pricing-options-style-5.php

tiered-pricing-options-style-5.php in Tiered Pricing Table for WooCommerce 8.1.0, at views/frontend/tiered-pricing-options-style-5.php

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