| 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 <?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' => 'default', |
| 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 |
</div> |
| 197 |
|
| 198 |
<?php |
| 199 |
do_action( 'tiered_pricing_table/dropdown/label', $pricing_rule, $minimum, array( |
| 200 |
'id' => $id, |
| 201 |
'style' => 'default', |
| 202 |
) ); |
| 203 |
?> |
| 204 |
|
| 205 |
<div class="tiered-pricing-dropdown-option__pricing"> |
| 206 |
<div class="tiered-pricing-option-price"> |
| 207 |
|
| 208 |
<?php if ( $discountAmount > 0 ) : ?> |
| 209 |
<div class="tiered-pricing-dropdown-option-price__original"> |
| 210 |
<del> |
| 211 |
<?php |
| 212 |
echo wp_kses_post( wc_price( wc_get_price_to_display( $product, array( |
| 213 |
'price' => $regular_price, |
| 214 |
) ) ) ); |
| 215 |
?> |
| 216 |
</del> |
| 217 |
</div> |
| 218 |
<?php endif; ?> |
| 219 |
|
| 220 |
<div class="tiered-pricing-dropdown-option-price__discounted"> |
| 221 |
<?php |
| 222 |
echo wp_kses_post( wc_price( wc_get_price_to_display( wc_get_product( $product_id ), |
| 223 |
array( |
| 224 |
'price' => $real_price, |
| 225 |
) ) ) ); |
| 226 |
?> |
| 227 |
</div> |
| 228 |
</div> |
| 229 |
</div> |
| 230 |
</li> |
| 231 |
<?php $tptTierRows[] = ob_get_clean(); ?> |
| 232 |
<?php $iterator = new ArrayIterator( $price_rules ); ?> |
| 233 |
|
| 234 |
<?php while ( $iterator->valid() ) : ob_start(); ?> |
| 235 |
<?php |
| 236 |
$currentPrice = $iterator->current(); |
| 237 |
$currentQuantity = $iterator->key(); |
| 238 |
|
| 239 |
if ( 'percentage' === $pricing_type ) { |
| 240 |
$discountAmount = $currentPrice; |
| 241 |
} else { |
| 242 |
$discountAmount = PriceManager::calculateDiscount( CalculationLogic::calculateDiscountBasedOnRegularPrice() ? $product->get_regular_price() : $product->get_price(), |
| 243 |
$pricing_rule->getTierPrice( $currentQuantity, false ) ); |
| 244 |
} |
| 245 |
|
| 246 |
$iterator->next(); |
| 247 |
|
| 248 |
if ( $iterator->valid() ) { |
| 249 |
$quantity = number_format_i18n( $currentQuantity ); |
| 250 |
|
| 251 |
if ( (int) $iterator->key() - 1 !== (int) $currentQuantity && 'range' === $settings['quantity_type'] ) { |
| 252 |
$quantity .= ' - ' . number_format_i18n( intval( $iterator->key() - 1 ) ); |
| 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 |
role="option" |
| 278 |
aria-selected="false" |
| 279 |
data-tiered-quantity="<?php echo esc_attr( $currentQuantity ); ?>" |
| 280 |
data-tiered-price="<?php echo esc_attr( $currentProductPrice ); ?>" |
| 281 |
data-tiered-price-exclude-taxes="<?php echo esc_attr( $currentProductPriceExcludeTaxes ); ?>" |
| 282 |
data-tiered-price-include-taxes="<?php echo esc_attr( $currentProductPriceIncludeTaxes ); ?>"> |
| 283 |
|
| 284 |
<div class="tiered-pricing-dropdown-option__quantity"> |
| 285 |
<?php |
| 286 |
echo wp_kses_post( tptParseOptionText( $settings['options_option_text'], $quantity, |
| 287 |
round( $discountAmount, 2 ), $settings['quantity_measurement_plural'] ) ); |
| 288 |
?> |
| 289 |
</div> |
| 290 |
|
| 291 |
<?php |
| 292 |
do_action( 'tiered_pricing_table/dropdown/label', $pricing_rule, $currentQuantity, array( |
| 293 |
'id' => $id, |
| 294 |
'style' => 'default', |
| 295 |
) ); |
| 296 |
?> |
| 297 |
|
| 298 |
<div class="tiered-pricing-dropdown-option__pricing"> |
| 299 |
<div class="tiered-pricing-option-price"> |
| 300 |
<div class="tiered-pricing-dropdown-option-price__original"> |
| 301 |
<del> |
| 302 |
<?php |
| 303 |
echo wp_kses_post( wc_price( wc_get_price_to_display( $product, array( |
| 304 |
'price' => CalculationLogic::calculateDiscountBasedOnRegularPrice() ? $regular_price : $real_price, |
| 305 |
) ) ) ); |
| 306 |
?> |
| 307 |
</del> |
| 308 |
</div> |
| 309 |
<div class="tiered-pricing-dropdown-option-price__discounted"> |
| 310 |
<?php |
| 311 |
echo wp_kses_post( wc_price( PriceManager::getPriceByRules( $currentQuantity, |
| 312 |
$product_id ) ) ); |
| 313 |
?> |
| 314 |
</div> |
| 315 |
</div> |
| 316 |
</div> |
| 317 |
</li> |
| 318 |
<?php $tptTierRows[] = ob_get_clean(); endwhile; ?> |
| 319 |
<?php echo PricingTable::orderTiers( $tptTierRows, $settings ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- rows escaped when rendered above ?> |
| 320 |
<?php do_action( 'tiered_pricing_table/dropdown/options', $pricing_rule ); ?> |
| 321 |
</ul> |
| 322 |
</div> |
| 323 |
|
| 324 |
<?php do_action( 'tiered_pricing_table/dropdown/after_options', $pricing_rule ); ?> |
| 325 |
</div> |
| 326 |
</div> |
| 327 |
|
| 328 |
<style> |
| 329 |
<?php |
| 330 |
$backgroundColor = Settings::hex2rgba($settings['active_tier_color'], 0.05); |
| 331 |
|
| 332 |
$clickableCSS = <<<EOD |
| 333 |
.tiered-pricing-dropdown__list .tiered-pricing-dropdown-option:hover, |
| 334 |
.tiered-pricing-dropdown__list .tiered-pricing-dropdown-option:focus { |
| 335 |
background: $backgroundColor; |
| 336 |
cursor: pointer; |
| 337 |
} |
| 338 |
EOD; |
| 339 |
$CSS = <<<EOD |
| 340 |
#$id .tiered-pricing-dropdown__list, #$id .tiered-pricing-dropdown__select-box { |
| 341 |
border-color: {$settings['active_tier_color']}; |
| 342 |
} |
| 343 |
#$id .tiered-pricing-dropdown__select-box-arrow svg { |
| 344 |
fill: {$settings['active_tier_color']}; |
| 345 |
} |
| 346 |
#$id .tiered-pricing--active .tiered-pricing-option-checkbox::after { |
| 347 |
background: {$settings['active_tier_color']}; |
| 348 |
} |
| 349 |
#{$id} .tiered-pricing-dropdown__list .tiered-pricing--active { |
| 350 |
background: $backgroundColor; |
| 351 |
} |
| 352 |
EOD; |
| 353 |
|
| 354 |
$hideOriginalPriceCSS = <<<EOD |
| 355 |
#$id .tiered-pricing-dropdown-option-price__original { |
| 356 |
display: none |
| 357 |
} |
| 358 |
EOD; |
| 359 |
|
| 360 |
echo esc_html($CSS); |
| 361 |
|
| 362 |
if (tpt_fs()->is__premium_only() && $settings['clickable_rows']) { |
| 363 |
echo esc_html($clickableCSS); |
| 364 |
} |
| 365 |
|
| 366 |
if (! $settings['options_show_original_product_price']) { |
| 367 |
echo esc_html($hideOriginalPriceCSS); |
| 368 |
} |
| 369 |
?> |
| 370 |
</style> |
| 371 |
<?php endif; ?> |
| 372 |
|
| 373 |
|