| 1 |
<?php |
| 2 |
|
| 3 |
use TierPricingTable\CalculationLogic; |
| 4 |
use TierPricingTable\PriceManager; |
| 5 |
use TierPricingTable\PricingTable; |
| 6 |
use TierPricingTable\PricingRule; |
| 7 |
|
| 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 |
|
| 26 |
$sale_price = $product->get_sale_price(); |
| 27 |
|
| 28 |
if ( $sale_price ) { |
| 29 |
$sale_price = wc_get_price_to_display( $product, array( |
| 30 |
'price' => $sale_price, |
| 31 |
) ); |
| 32 |
} |
| 33 |
|
| 34 |
$regular_price = wc_get_price_to_display( $product, array( |
| 35 |
'price' => $product->get_regular_price(), |
| 36 |
) ); |
| 37 |
|
| 38 |
$price = wc_get_price_to_display( $product, array( |
| 39 |
'price' => $product->get_price(), |
| 40 |
) ); |
| 41 |
|
| 42 |
if ( ! function_exists( 'tptParsePlainText' ) ) { |
| 43 |
function tptParsePlainText( $text, $quantity, $discount = null, $price = null, $base_unit_name = null ) { |
| 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_price}' => $price ? wc_price( $price ) : '', |
| 49 |
'{tp_base_unit_name}' => $base_unit_name, |
| 50 |
) ); |
| 51 |
} |
| 52 |
} |
| 53 |
?> |
| 54 |
<?php if ( ! empty( $price_rules ) ) : ?> |
| 55 |
|
| 56 |
<div class="tiered-pricing-wrapper"> |
| 57 |
<?php if ( ! empty( $settings['title'] ) ) : ?> |
| 58 |
<h3 style="clear:both; margin: 20px 0;"><?php echo esc_attr( $settings['title'] ); ?></h3> |
| 59 |
<?php endif; ?> |
| 60 |
|
| 61 |
<ul class="tiered-pricing-plain-texts" |
| 62 |
id="<?php echo esc_attr( $id ); ?>" |
| 63 |
data-product-id="<?php echo esc_attr( $product_id ); ?>" |
| 64 |
data-price-rules="<?php echo esc_attr( htmlspecialchars( json_encode( $price_rules ), ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ) ); ?>" |
| 65 |
data-minimum="<?php echo esc_attr( $minimum ); ?>" |
| 66 |
data-product-name="<?php echo esc_attr( $product_name ); ?>" |
| 67 |
data-regular-price="<?php echo esc_attr( $regular_price ); ?>" |
| 68 |
data-sale-price="<?php echo esc_attr( $sale_price ); ?>" |
| 69 |
data-price="<?php echo esc_attr( $price ); ?>" |
| 70 |
data-product-price-suffix="<?php echo esc_attr( $product->get_price_suffix() ); ?>" |
| 71 |
> |
| 72 |
<?php $tptTierRows = array(); ob_start(); ?> |
| 73 |
<li class="tiered-pricing-plain-text tiered-pricing--active tiered-pricing-plain-text--default" |
| 74 |
data-tiered-quantity="<?php echo esc_attr( $minimum ); ?>" |
| 75 |
data-tiered-price="<?php echo esc_attr( $price ); ?>" |
| 76 |
data-tiered-price-exclude-taxes=" |
| 77 |
<?php |
| 78 |
echo esc_attr( wc_get_price_excluding_tax( wc_get_product( $product_id ), array( |
| 79 |
'price' => $real_price, |
| 80 |
) ) ); |
| 81 |
?> |
| 82 |
" |
| 83 |
data-tiered-price-include-taxes=" |
| 84 |
<?php |
| 85 |
echo esc_attr( wc_get_price_including_tax( wc_get_product( $product_id ), array( |
| 86 |
'price' => $real_price, |
| 87 |
) ) ); |
| 88 |
?> |
| 89 |
" |
| 90 |
> |
| 91 |
<?php |
| 92 |
$discountAmount = 0; |
| 93 |
if ( CalculationLogic::calculateDiscountBasedOnRegularPrice() && $product->is_on_sale() ) { |
| 94 |
$discountAmount = PriceManager::calculateDiscount( $product->get_regular_price(), |
| 95 |
$product->get_sale_price() ); |
| 96 |
} |
| 97 |
?> |
| 98 |
|
| 99 |
<?php if ( 1 >= array_keys( $price_rules )[0] - $minimum || 'static' === $settings['quantity_type'] ) : ?> |
| 100 |
<?php |
| 101 |
$quantity = esc_attr( number_format_i18n( $minimum ) . ' ' ); |
| 102 |
$baseUnitName = $minimum > 1 ? $settings['quantity_measurement_plural'] : $settings['quantity_measurement_singular']; |
| 103 |
?> |
| 104 |
<?php else : ?> |
| 105 |
<?php |
| 106 |
$quantity = esc_attr( number_format_i18n( $minimum ) . ' - ' . number_format_i18n( array_keys( $price_rules )[0] - 1 ) . ' ' ); |
| 107 |
$baseUnitName = $settings['quantity_measurement_plural']; |
| 108 |
?> |
| 109 |
<?php endif; ?> |
| 110 |
|
| 111 |
<?php if ( $discountAmount > 0 ) : ?> |
| 112 |
<?php |
| 113 |
echo wp_kses_post( tptParsePlainText( $settings['plain_text_option_text'], $quantity, |
| 114 |
round( $discountAmount, 2 ), $price, $baseUnitName ) ); |
| 115 |
?> |
| 116 |
<?php else : ?> |
| 117 |
<?php |
| 118 |
echo wp_kses_post( tptParsePlainText( $settings['plain_text_default_option_text'], $quantity, null, |
| 119 |
$price, $baseUnitName ) ); |
| 120 |
?> |
| 121 |
<?php endif; ?> |
| 122 |
|
| 123 |
<?php |
| 124 |
do_action( 'tiered_pricing_table/plain-text/label', $pricing_rule, $minimum, array( |
| 125 |
'id' => $id, |
| 126 |
'style' => 'default', |
| 127 |
) ); |
| 128 |
?> |
| 129 |
</li> |
| 130 |
<?php $tptTierRows[] = ob_get_clean(); ?> |
| 131 |
|
| 132 |
<?php $iterator = new ArrayIterator( $price_rules ); ?> |
| 133 |
|
| 134 |
<?php while ( $iterator->valid() ) : ob_start(); ?> |
| 135 |
<?php |
| 136 |
$currentPrice = $iterator->current(); |
| 137 |
$currentQuantity = $iterator->key(); |
| 138 |
|
| 139 |
if ( 'percentage' === $pricing_type ) { |
| 140 |
$discountAmount = $currentPrice; |
| 141 |
} else { |
| 142 |
$discountAmount = PriceManager::calculateDiscount( CalculationLogic::calculateDiscountBasedOnRegularPrice() ? $product->get_regular_price() : $product->get_price(), |
| 143 |
$pricing_rule->getTierPrice( $currentQuantity, false ) ); |
| 144 |
} |
| 145 |
|
| 146 |
$iterator->next(); |
| 147 |
|
| 148 |
if ( $iterator->valid() ) { |
| 149 |
$quantity = number_format_i18n( $currentQuantity ); |
| 150 |
|
| 151 |
if ( (int) $iterator->key() - 1 !== (int) $currentQuantity && 'range' === $settings['quantity_type'] ) { |
| 152 |
$quantity .= ' - ' . number_format_i18n( intval( $iterator->key() - 1 ) ); |
| 153 |
} |
| 154 |
} else { |
| 155 |
$quantity = number_format_i18n( $currentQuantity ); |
| 156 |
|
| 157 |
$quantity .= apply_filters( 'tiered_pricing_table/tiered_pricing/last_tier_postfix', '+', |
| 158 |
$currentQuantity, $pricing_rule, 'blocks' ); |
| 159 |
} |
| 160 |
|
| 161 |
$currentProductPrice = PriceManager::getPriceByRules( $currentQuantity, $product_id ); |
| 162 |
|
| 163 |
$currentProductPriceExcludeTaxes = wc_get_price_excluding_tax( wc_get_product( $product_id ), array( |
| 164 |
'price' => PriceManager::getPriceByRules( $currentQuantity, $product_id, null, null, false ), |
| 165 |
) ); |
| 166 |
|
| 167 |
$currentProductPriceIncludeTaxes = wc_get_price_including_tax( wc_get_product( $product_id ), array( |
| 168 |
'price' => PriceManager::getPriceByRules( $currentQuantity, $product_id, null, null, false ), |
| 169 |
) ); |
| 170 |
|
| 171 |
?> |
| 172 |
|
| 173 |
<li class="tiered-pricing-plain-text" |
| 174 |
data-tiered-quantity="<?php echo esc_attr( $currentQuantity ); ?>" |
| 175 |
data-tiered-price="<?php echo esc_attr( $currentProductPrice ); ?>" |
| 176 |
data-tiered-price-exclude-taxes="<?php echo esc_attr( $currentProductPriceExcludeTaxes ); ?>" |
| 177 |
data-tiered-price-include-taxes="<?php echo esc_attr( $currentProductPriceIncludeTaxes ); ?>"> |
| 178 |
|
| 179 |
<?php |
| 180 |
|
| 181 |
echo wp_kses_post( tptParsePlainText( $settings['plain_text_option_text'], $quantity, |
| 182 |
round( $discountAmount, 2 ), $currentProductPrice, |
| 183 |
$settings['quantity_measurement_plural'] ) ); |
| 184 |
?> |
| 185 |
|
| 186 |
<?php |
| 187 |
do_action( 'tiered_pricing_table/plain-text/label', $pricing_rule, $currentQuantity, array( |
| 188 |
'id' => $id, |
| 189 |
'style' => 'default', |
| 190 |
) ); |
| 191 |
?> |
| 192 |
</li> |
| 193 |
<?php $tptTierRows[] = ob_get_clean(); endwhile; ?> |
| 194 |
<?php echo PricingTable::orderTiers( $tptTierRows, $settings ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- rows escaped when rendered above ?> |
| 195 |
|
| 196 |
<?php do_action( 'tiered_pricing_table/plain-text/line', $pricing_rule, $settings ); ?> |
| 197 |
</ul> |
| 198 |
|
| 199 |
<?php do_action( 'tiered_pricing_table/plain-text/after_lines', $pricing_rule, $settings ); ?> |
| 200 |
</div> |
| 201 |
|
| 202 |
<style> |
| 203 |
<?php |
| 204 |
if ( $settings['clickable_rows'] && tpt_fs()->can_use_premium_code() ) { |
| 205 |
echo esc_html("#{$id} .tiered-pricing-plain-text {cursor: pointer; }"); |
| 206 |
} |
| 207 |
|
| 208 |
if ( ! $settings['plain_text_show_default_option'] ) { |
| 209 |
echo esc_html("#{$id} .tiered-pricing-plain-text--default { display: none }"); |
| 210 |
} |
| 211 |
|
| 212 |
echo esc_html( "#{$id} .tiered-pricing--active { |
| 213 |
color: {$settings['active_tier_color']}; |
| 214 |
}"); |
| 215 |
|
| 216 |
?> |
| 217 |
|
| 218 |
</style> |
| 219 |
<?php endif; ?> |