| 1 |
<?php use TierPricingTable\CalculationLogic; |
| 2 |
use TierPricingTable\PriceManager; |
| 3 |
use TierPricingTable\PricingTable; |
| 4 |
use TierPricingTable\PricingRule; |
| 5 |
use TierPricingTable\Settings\Settings; |
| 6 |
|
| 7 |
if ( ! defined( 'WPINC' ) ) { |
| 8 |
die; |
| 9 |
} |
| 10 |
/** |
| 11 |
* Available variables |
| 12 |
* |
| 13 |
* @var array $price_rules |
| 14 |
* @var PricingRule $pricing_rule |
| 15 |
* @var string $real_price |
| 16 |
* @var string $product_name |
| 17 |
* @var string $pricing_type |
| 18 |
* @var WC_Product $product |
| 19 |
* @var string $id |
| 20 |
* @var int $product_id |
| 21 |
* @var int $minimum |
| 22 |
* @var array $settings |
| 23 |
*/ |
| 24 |
|
| 25 |
$sale_price = $product->get_sale_price(); |
| 26 |
|
| 27 |
if ( $sale_price ) { |
| 28 |
$sale_price = wc_get_price_to_display( $product, array( |
| 29 |
'price' => $sale_price, |
| 30 |
) ); |
| 31 |
} |
| 32 |
|
| 33 |
$regular_price = wc_get_price_to_display( $product, array( |
| 34 |
'price' => $product->get_regular_price(), |
| 35 |
) ); |
| 36 |
|
| 37 |
$price = wc_get_price_to_display( $product, array( |
| 38 |
'price' => $product->get_price(), |
| 39 |
) ); |
| 40 |
|
| 41 |
// discount of every tier (percent), for the biggest discount |
| 42 |
$tierDiscounts = array(); |
| 43 |
foreach ( $price_rules as $tierQuantity => $tierPrice ) { |
| 44 |
$tierDiscounts[ $tierQuantity ] = 'percentage' === $pricing_type |
| 45 |
? (float) $tierPrice |
| 46 |
: (float) PriceManager::calculateDiscount( CalculationLogic::calculateDiscountBasedOnRegularPrice() ? $product->get_regular_price() : $product->get_price(), |
| 47 |
$pricing_rule->getTierPrice( $tierQuantity, false ) ); |
| 48 |
} |
| 49 |
$maxDiscount = $tierDiscounts ? max( $tierDiscounts ) : 0; |
| 50 |
|
| 51 |
?> |
| 52 |
|
| 53 |
<?php if ( ! empty( $price_rules ) ) : ?> |
| 54 |
|
| 55 |
<div class="tiered-pricing-wrapper"> |
| 56 |
<?php if ( ! empty( $settings['title'] ) ) : ?> |
| 57 |
<h3 style="clear:both; margin: 20px 0;"><?php echo esc_attr( $settings['title'] ); ?></h3> |
| 58 |
<?php endif; ?> |
| 59 |
|
| 60 |
<div class="tiered-pricing-blocks tiered-pricing-blocks--styled tiered-pricing-blocks--style-8 <?php echo ( isset( $settings['compact_layout'] ) && $settings['compact_layout'] === 'yes' ) ? 'tiered-pricing-blocks--slim' : ''; ?>" |
| 61 |
|
| 62 |
<?php echo PricingTable::layoutStyleAttribute( $settings ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped in the helper ?> |
| 63 |
id="<?php echo esc_attr( $id ); ?>" |
| 64 |
data-product-id="<?php echo esc_attr( $product_id ); ?>" |
| 65 |
data-price-rules="<?php echo esc_attr( htmlspecialchars( json_encode( $price_rules ), ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ) ); ?>" |
| 66 |
data-minimum="<?php echo esc_attr( $minimum ); ?>" |
| 67 |
data-product-name="<?php echo esc_attr( $product_name ); ?>" |
| 68 |
data-regular-price="<?php echo esc_attr( $regular_price ); ?>" |
| 69 |
data-sale-price="<?php echo esc_attr( $sale_price ); ?>" |
| 70 |
data-price="<?php echo esc_attr( $price ); ?>" |
| 71 |
data-product-price-suffix="<?php echo esc_attr( $product->get_price_suffix() ); ?>" |
| 72 |
> |
| 73 |
|
| 74 |
<?php $tptTierRows = array(); ob_start(); ?> |
| 75 |
<?php |
| 76 |
$discountAmount = 0; |
| 77 |
if ( CalculationLogic::calculateDiscountBasedOnRegularPrice() && $product->is_on_sale() ) { |
| 78 |
$discountAmount = PriceManager::calculateDiscount( $product->get_regular_price(), $product->get_sale_price() ); |
| 79 |
} |
| 80 |
$baseQuantityLabel = ( 1 >= array_keys( $price_rules )[0] - $minimum || 'static' === $settings['quantity_type'] ) |
| 81 |
? number_format_i18n( $minimum ) . ' ' . ( $minimum > 1 ? $settings['quantity_measurement_plural'] : $settings['quantity_measurement_singular'] ) |
| 82 |
: number_format_i18n( $minimum ) . ' - ' . number_format_i18n( array_keys( $price_rules )[0] - 1 ) . ' ' . $settings['quantity_measurement_plural']; |
| 83 |
?> |
| 84 |
<div class="tiered-pricing-block tiered-pricing--active" |
| 85 |
data-tiered-quantity="<?php echo esc_attr( $minimum ); ?>" |
| 86 |
data-tiered-price="<?php echo esc_attr( wc_get_price_to_display( wc_get_product( $product_id ), array( 'price' => $real_price ) ) ); ?>" |
| 87 |
data-tiered-price-exclude-taxes="<?php echo esc_attr( wc_get_price_excluding_tax( wc_get_product( $product_id ), array( 'price' => $real_price ) ) ); ?>" |
| 88 |
data-tiered-price-include-taxes="<?php echo esc_attr( wc_get_price_including_tax( wc_get_product( $product_id ), array( 'price' => $real_price ) ) ); ?>"> |
| 89 |
|
| 90 |
<?php |
| 91 |
do_action( 'tiered_pricing_table/blocks/label', $pricing_rule, $minimum, array( |
| 92 |
'id' => $id, |
| 93 |
'style' => '8', |
| 94 |
) ); |
| 95 |
?> |
| 96 |
<div class="tiered-pricing-block__price"> |
| 97 |
<span><?php echo wp_kses_post( wc_price( wc_get_price_to_display( wc_get_product( $product_id ), array( 'price' => $real_price ) ) ) ); ?></span> |
| 98 |
<?php if ( $settings['show_discount_column'] && $discountAmount > 0 ) : ?> |
| 99 |
<span class="tiered-pricing-block__price-discount"><?php echo wp_kses_post( PricingTable::formatDiscount( $discountAmount, $pricing_rule, $product, null, $settings, 'off' ) ); ?></span> |
| 100 |
<?php endif; ?> |
| 101 |
</div> |
| 102 |
<div class="tiered-pricing-block__quantity"><?php echo esc_html( $baseQuantityLabel ); ?></div> |
| 103 |
</div> |
| 104 |
<?php $tptTierRows[] = ob_get_clean(); ?> |
| 105 |
|
| 106 |
<?php $iterator = new ArrayIterator( $price_rules ); ?> |
| 107 |
|
| 108 |
<?php while ( $iterator->valid() ) : ob_start(); ?> |
| 109 |
<?php |
| 110 |
$currentPrice = $iterator->current(); |
| 111 |
$currentQuantity = $iterator->key(); |
| 112 |
$discountAmount = $tierDiscounts[ $currentQuantity ]; |
| 113 |
|
| 114 |
$iterator->next(); |
| 115 |
|
| 116 |
if ( $iterator->valid() ) { |
| 117 |
$quantity = number_format_i18n( $currentQuantity ); |
| 118 |
|
| 119 |
if ( (int) $iterator->key() - 1 !== (int) $currentQuantity && 'range' === $settings['quantity_type'] ) { |
| 120 |
$quantity .= ' - ' . number_format_i18n( intval( $iterator->key() - 1 ) ); |
| 121 |
} |
| 122 |
} else { |
| 123 |
$quantity = number_format_i18n( $currentQuantity ); |
| 124 |
|
| 125 |
$quantity .= apply_filters( 'tiered_pricing_table/tiered_pricing/last_tier_postfix', '+', |
| 126 |
$currentQuantity, $pricing_rule, 'blocks' ); |
| 127 |
} |
| 128 |
|
| 129 |
$quantity = $quantity . ' ' . $settings['quantity_measurement_plural']; |
| 130 |
|
| 131 |
$currentProductPrice = PriceManager::getPriceByRules( $currentQuantity, $product_id ); |
| 132 |
|
| 133 |
$currentProductPriceExcludeTaxes = wc_get_price_excluding_tax( wc_get_product( $product_id ), array( |
| 134 |
'price' => PriceManager::getPriceByRules( $currentQuantity, $product_id, null, null, false ), |
| 135 |
) ); |
| 136 |
|
| 137 |
$currentProductPriceIncludeTaxes = wc_get_price_including_tax( wc_get_product( $product_id ), array( |
| 138 |
'price' => PriceManager::getPriceByRules( $currentQuantity, $product_id, null, null, false ), |
| 139 |
) ); |
| 140 |
|
| 141 |
$isBestValue = $maxDiscount > 0 && abs( $discountAmount - $maxDiscount ) < 0.0001; |
| 142 |
?> |
| 143 |
|
| 144 |
<div class="tiered-pricing-block<?php echo $isBestValue ? ' tiered-pricing-block--best' : ''; ?>" |
| 145 |
data-tiered-quantity="<?php echo esc_attr( $currentQuantity ); ?>" |
| 146 |
data-tiered-price="<?php echo esc_attr( $currentProductPrice ); ?>" |
| 147 |
data-tiered-price-exclude-taxes="<?php echo esc_attr( $currentProductPriceExcludeTaxes ); ?>" |
| 148 |
data-tiered-price-include-taxes="<?php echo esc_attr( $currentProductPriceIncludeTaxes ); ?>"> |
| 149 |
|
| 150 |
<?php |
| 151 |
do_action( 'tiered_pricing_table/blocks/label', $pricing_rule, $currentQuantity, array( |
| 152 |
'id' => $id, |
| 153 |
'style' => '8', |
| 154 |
) ); |
| 155 |
?> |
| 156 |
<div class="tiered-pricing-block__price"> |
| 157 |
<span><?php echo wp_kses_post( wc_price( PriceManager::getPriceByRules( $currentQuantity, $product_id ) ) ); ?></span> |
| 158 |
<?php if ( $settings['show_discount_column'] && $discountAmount > 0 ) : ?> |
| 159 |
<span class="tiered-pricing-block__price-discount"><?php echo wp_kses_post( PricingTable::formatDiscount( $discountAmount, $pricing_rule, $product, $currentQuantity, $settings, 'off' ) ); ?></span> |
| 160 |
<?php endif; ?> |
| 161 |
</div> |
| 162 |
<div class="tiered-pricing-block__quantity"><?php echo esc_html( $quantity ); ?></div> |
| 163 |
</div> |
| 164 |
<?php $tptTierRows[] = ob_get_clean(); endwhile; ?> |
| 165 |
<?php echo PricingTable::orderTiers( $tptTierRows, $settings ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- rows escaped when rendered above ?> |
| 166 |
|
| 167 |
<?php do_action( 'tiered_pricing_table/blocks/blocks', $pricing_rule, $settings ); ?> |
| 168 |
</div> |
| 169 |
|
| 170 |
<?php do_action( 'tiered_pricing_table/blocks/after_blocks', $pricing_rule ); ?> |
| 171 |
</div> |
| 172 |
|
| 173 |
<style> |
| 174 |
<?php |
| 175 |
if ( $settings['clickable_rows'] && tpt_fs()->can_use_premium_code() ) { |
| 176 |
echo esc_attr( '#' . $id ) . ' .tiered-pricing-block { cursor: pointer; }'; |
| 177 |
} |
| 178 |
?> |
| 179 |
|
| 180 |
<?php echo esc_attr( '#' . $id ); ?> .tiered-pricing--active { |
| 181 |
background-color: <?php echo esc_attr( $settings['active_tier_color'] ); ?> !important; |
| 182 |
} |
| 183 |
|
| 184 |
</style> |
| 185 |
<?php endif; ?> |
| 186 |
|