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-4.php

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

385 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\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-4 <?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-inner">
156 <div class="tiered-pricing-option__head">
157 <div class="tiered-pricing-option__checkbox">
158 <div class="tiered-pricing-option-checkbox tiered-pricing-option-checkbox--active"></div>
159 </div>
160 <?php
161 if ( $settings['show_discount_column'] && $discountAmount > 0 ) {
162 ?>
163 <div class="tiered-pricing-option__discount-label"><?php
164 echo wp_kses_post( PricingTable::formatDiscount(
165 $discountAmount,
166 $pricing_rule,
167 $product,
168 null,
169 $settings
170 ) );
171 ?></div>
172 <?php
173 }
174 ?>
175 </div>
176 <div class="tiered-pricing-option__quantity"><?php
177 echo wp_kses_post( $optionText );
178 ?></div>
179 <?php
180 do_action(
181 'tiered_pricing_table/options/label',
182 $pricing_rule,
183 $minimum,
184 array(
185 'id' => $id,
186 'style' => '4',
187 )
188 );
189 ?>
190 <div class="tiered-pricing-option__pricing">
191 <div class="tiered-pricing-option-price">
192 <div class="tiered-pricing-option-price__discounted"><?php
193 echo wp_kses_post( wc_price( wc_get_price_to_display( wc_get_product( $product_id ), array(
194 'price' => $real_price,
195 ) ) ) );
196 ?></div>
197 <?php
198 if ( $discountAmount > 0 ) {
199 ?>
200 <div class="tiered-pricing-option-price__original"><del><?php
201 echo wp_kses_post( wc_price( $regular_price ) );
202 ?></del></div>
203 <?php
204 }
205 ?>
206 </div>
207
208 <?php
209 ?>
210
211 </div>
212 </div>
213 </div>
214 <?php
215 $tptTierRows[] = ob_get_clean();
216 ?>
217
218 <?php
219 $iterator = new ArrayIterator($price_rules);
220 ?>
221
222 <?php
223 while ( $iterator->valid() ) {
224 ob_start();
225 ?>
226 <?php
227 $currentPrice = $iterator->current();
228 $currentQuantity = $iterator->key();
229 $discountAmount = $tierDiscounts[$currentQuantity];
230 $iterator->next();
231 if ( $iterator->valid() ) {
232 $quantity = number_format_i18n( $currentQuantity );
233 if ( (int) $iterator->key() - 1 !== (int) $currentQuantity && 'range' === $settings['quantity_type'] ) {
234 $quantity .= ' - ' . number_format_i18n( intval( $iterator->key() - 1 ) );
235 }
236 } else {
237 $quantity = number_format_i18n( $currentQuantity );
238 $quantity .= apply_filters(
239 'tiered_pricing_table/tiered_pricing/last_tier_postfix',
240 '+',
241 $currentQuantity,
242 $pricing_rule,
243 'blocks'
244 );
245 }
246 $currentProductPrice = PriceManager::getPriceByRules( $currentQuantity, $product_id );
247 $currentProductPriceExcludeTaxes = wc_get_price_excluding_tax( wc_get_product( $product_id ), array(
248 'price' => PriceManager::getPriceByRules(
249 $currentQuantity,
250 $product_id,
251 null,
252 null,
253 false
254 ),
255 ) );
256 $currentProductPriceIncludeTaxes = wc_get_price_including_tax( wc_get_product( $product_id ), array(
257 'price' => PriceManager::getPriceByRules(
258 $currentQuantity,
259 $product_id,
260 null,
261 null,
262 false
263 ),
264 ) );
265 $optionText = tptParseOptionText(
266 $settings['options_option_text'],
267 $quantity,
268 round( $discountAmount, 2 ),
269 $settings['quantity_measurement_plural']
270 );
271 ?>
272
273 <div class="tiered-pricing-option"
274 data-tiered-quantity="<?php
275 echo esc_attr( $currentQuantity );
276 ?>"
277 data-tiered-price="<?php
278 echo esc_attr( $currentProductPrice );
279 ?>"
280 data-tiered-price-exclude-taxes="<?php
281 echo esc_attr( $currentProductPriceExcludeTaxes );
282 ?>"
283 data-tiered-price-include-taxes="<?php
284 echo esc_attr( $currentProductPriceIncludeTaxes );
285 ?>">
286 <div class="tiered-pricing-option-inner">
287 <div class="tiered-pricing-option__head">
288 <div class="tiered-pricing-option__checkbox">
289 <div class="tiered-pricing-option-checkbox"></div>
290 </div>
291 <?php
292 if ( $settings['show_discount_column'] && $discountAmount > 0 ) {
293 ?>
294 <div class="tiered-pricing-option__discount-label"><?php
295 echo wp_kses_post( PricingTable::formatDiscount(
296 $discountAmount,
297 $pricing_rule,
298 $product,
299 $currentQuantity,
300 $settings
301 ) );
302 ?></div>
303 <?php
304 }
305 ?>
306 </div>
307 <div class="tiered-pricing-option__quantity"><?php
308 echo wp_kses_post( $optionText );
309 ?></div>
310 <?php
311 do_action(
312 'tiered_pricing_table/options/label',
313 $pricing_rule,
314 $currentQuantity,
315 array(
316 'id' => $id,
317 'style' => '4',
318 )
319 );
320 ?>
321 <div class="tiered-pricing-option__pricing">
322 <div class="tiered-pricing-option-price">
323 <div class="tiered-pricing-option-price__discounted"><?php
324 echo wp_kses_post( wc_price( PriceManager::getPriceByRules( $currentQuantity, $product_id ) ) );
325 ?></div>
326 <?php
327 if ( $discountAmount > 0 ) {
328 ?>
329 <div class="tiered-pricing-option-price__original"><del><?php
330 echo wp_kses_post( wc_price( wc_get_price_to_display( $product, array(
331 'price' => ( CalculationLogic::calculateDiscountBasedOnRegularPrice() ? $regular_price : $real_price ),
332 ) ) ) );
333 ?></del></div>
334 <?php
335 }
336 ?>
337 </div>
338
339 <?php
340 ?>
341
342 </div>
343 </div>
344 </div>
345 <?php
346 $tptTierRows[] = ob_get_clean();
347 }
348 ?>
349 <?php
350 echo PricingTable::orderTiers( $tptTierRows, $settings );
351 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- rows escaped when rendered above
352 ?>
353
354 <?php
355 do_action( 'tiered_pricing_table/options/options', $pricing_rule, $settings );
356 ?>
357 </div>
358
359 <?php
360 do_action( 'tiered_pricing_table/options/after_options', $pricing_rule );
361 ?>
362 </div>
363
364 <style>
365 <?php
366 if ( !$settings['options_show_default_option'] ) {
367 echo esc_html( "#{$id} .tiered-pricing-option--default { display: none }" );
368 }
369 if ( !$settings['options_show_original_product_price'] ) {
370 echo esc_html( "#{$id} .tiered-pricing-option-price__original { display: none }" );
371 echo esc_html( "#{$id} .tiered-pricing-option-total__original_total { display: none }" );
372 }
373 $tint = Settings::hex2rgba( $settings['active_tier_color'], 0.06 );
374 $border = Settings::hex2rgba( $settings['active_tier_color'], 0.35 );
375 $color = $settings['active_tier_color'];
376 echo esc_html( "#{$id} .tiered-pricing-option { border-color: #e5e7eb; }" );
377 echo esc_html( "#{$id} .tiered-pricing--active { border-color: {$color}; background: {$tint}; box-shadow: 0 0 0 1px {$color}; }" );
378 echo esc_html( "#{$id} .tiered-pricing--active .tiered-pricing-option-checkbox { border-color: {$color}; }" );
379 echo esc_html( "#{$id} .tiered-pricing--active .tiered-pricing-option-checkbox::after { background: {$color}; }" );
380 echo esc_html( "#{$id} .tiered-pricing-option__discount-label { background: {$tint}; color: {$color}; }" );
381 echo esc_html( "#{$id} .tiered-pricing--active .tiered-pricing-option__discount-label { background: {$color}; color: #fff; }" );
382 ?>
383 </style>
384 <?php
385 }