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