| 1 |
<?php |
| 2 |
/** |
| 3 |
* Available variables |
| 4 |
* |
| 5 |
* @var PricingRule $pricing_rule |
| 6 |
* @var array $price_rules |
| 7 |
* @var string $pricing_type |
| 8 |
* @var string $real_price |
| 9 |
* @var string $product_name |
| 10 |
* @var WC_Product $product |
| 11 |
* @var string $id |
| 12 |
* @var int $product_id |
| 13 |
* @var int $minimum |
| 14 |
* @var array $settings |
| 15 |
*/ |
| 16 |
|
| 17 |
use TierPricingTable\CalculationLogic; |
| 18 |
use TierPricingTable\PriceManager; |
| 19 |
use TierPricingTable\PricingTable; |
| 20 |
use TierPricingTable\PricingRule; |
| 21 |
|
| 22 |
if ( ! defined( 'WPINC' ) ) { |
| 23 |
die; |
| 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 |
$price_incl_taxes = wc_get_price_including_tax( wc_get_product( $product_id ), array( |
| 43 |
'price' => $real_price, |
| 44 |
) ); |
| 45 |
|
| 46 |
$price_excl_taxes = wc_get_price_excluding_tax( wc_get_product( $product_id ), array( |
| 47 |
'price' => $real_price, |
| 48 |
) ); |
| 49 |
|
| 50 |
?> |
| 51 |
|
| 52 |
<?php if ( ! empty( $pricing_rule->getRules() ) ) : ?> |
| 53 |
<div class="clear"></div> |
| 54 |
|
| 55 |
<div class="tiered-pricing-wrapper"> |
| 56 |
<?php if ( ! empty( $settings['title'] ) ) : ?> |
| 57 |
<h3 style="clear:both; margin: 20px 0; font-weight: 600;"><?php echo esc_attr( $settings['title'] ); ?></h3> |
| 58 |
<?php endif; ?> |
| 59 |
|
| 60 |
<?php do_action( 'tiered_pricing_table/tiered_pricing/before', $pricing_rule ); ?> |
| 61 |
|
| 62 |
<?php |
| 63 |
$columns = 0; |
| 64 |
if ( $settings['quantity_column_title'] ) $columns++; |
| 65 |
if ( $settings['discount_column_title'] ) $columns++; |
| 66 |
if ( $settings['price_column_title'] ) $columns++; |
| 67 |
|
| 68 |
// Detect custom columns via output buffering |
| 69 |
ob_start(); |
| 70 |
do_action( 'tiered_pricing_table/tiered_pricing/header_columns', $pricing_rule ); |
| 71 |
$custom_head = ob_get_clean(); |
| 72 |
$custom_columns_count = substr_count($custom_head, '<th'); |
| 73 |
$columns += $custom_columns_count; |
| 74 |
?> |
| 75 |
|
| 76 |
<div class="tiered-pricing-table tiered-pricing-table--styled tiered-pricing-table--style-3 <?php echo (isset($settings['compact_layout']) && $settings['compact_layout'] === 'yes') ? 'tiered-pricing-table--slim' : ''; ?>" |
| 77 |
|
| 78 |
<?php echo PricingTable::layoutStyleAttribute( $settings ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped in the helper ?> |
| 79 |
id="<?php echo esc_attr( $id ); ?>" |
| 80 |
data-tiered-pricing-table |
| 81 |
data-product-id="<?php echo esc_attr( $product_id ); ?>" |
| 82 |
data-price-rules="<?php echo esc_attr( json_encode( $pricing_rule->getRules() ) ); ?>" |
| 83 |
data-minimum="<?php echo esc_attr( $minimum ); ?>" |
| 84 |
data-product-name="<?php echo esc_attr( $product_name ); ?>" |
| 85 |
data-regular-price="<?php echo esc_attr( $regular_price ); ?>" |
| 86 |
data-sale-price="<?php echo esc_attr( $sale_price ); ?>" |
| 87 |
data-price="<?php echo esc_attr( $price ); ?>" |
| 88 |
data-product-price-suffix="<?php echo esc_attr( $product->get_price_suffix() ); ?>" |
| 89 |
> |
| 90 |
<div class="tiered-pricing-table-header" style="grid-template-columns: repeat(<?php echo (int) $columns; ?>, 1fr);"> |
| 91 |
<?php if ( $settings['quantity_column_title'] ) : ?> |
| 92 |
<div><?php echo esc_attr( $settings['quantity_column_title'] ); ?></div> |
| 93 |
<?php endif; ?> |
| 94 |
|
| 95 |
<?php if ( $settings['discount_column_title'] ) : ?> |
| 96 |
<div><?php echo esc_attr( ( $settings['discount_column_title'] ) ); ?></div> |
| 97 |
<?php endif; ?> |
| 98 |
|
| 99 |
<?php if ( $settings['price_column_title'] ) : ?> |
| 100 |
<div><?php echo esc_attr( $settings['price_column_title'] ); ?></div> |
| 101 |
<?php endif; ?> |
| 102 |
|
| 103 |
<?php echo wp_kses_post( str_replace(['<th', '</th>'], ['<div', '</div>'], $custom_head) ); ?> |
| 104 |
</div> |
| 105 |
|
| 106 |
<div class="tiered-pricing-table-body"> |
| 107 |
<?php $tptTierRows = array(); ob_start(); ?> |
| 108 |
<div class="tiered-pricing-table-row tiered-pricing--active" |
| 109 |
data-tiered-quantity="<?php echo esc_attr( $minimum ); ?>" |
| 110 |
data-tiered-price="<?php echo esc_attr( $price ); ?>" |
| 111 |
data-tiered-price-exclude-taxes="<?php echo esc_attr( $price_excl_taxes ); ?>" |
| 112 |
data-tiered-price-include-taxes="<?php echo esc_attr( $price_incl_taxes ); ?>" |
| 113 |
style="grid-template-columns: repeat(<?php echo (int) $columns; ?>, 1fr);"> |
| 114 |
|
| 115 |
<?php if ( $settings['quantity_column_title'] ) : ?> |
| 116 |
<div> |
| 117 |
<?php if ( 1 >= array_keys( $pricing_rule->getRules() )[0] - $minimum || 'static' === $settings['quantity_type'] ) : ?> |
| 118 |
<span> |
| 119 |
<span class="tiered-pricing-table-row-qty"> |
| 120 |
<?php echo esc_attr( number_format_i18n( $minimum ) ); ?> |
| 121 |
</span> |
| 122 |
<span class="tiered-pricing-table-row-qty-unit"> |
| 123 |
<?php echo esc_attr( ' ' . ( $minimum > 1 ? $settings['quantity_measurement_plural'] : $settings['quantity_measurement_singular'] ) ); ?> |
| 124 |
</span> |
| 125 |
</span> |
| 126 |
<?php else : ?> |
| 127 |
<span> |
| 128 |
<span class="tiered-pricing-table-row-qty"> |
| 129 |
<?php echo esc_attr( number_format_i18n( $minimum ) ); ?> - <?php echo esc_attr( number_format_i18n( array_keys( $pricing_rule->getRules() )[0] - 1 ) ); ?> |
| 130 |
</span> |
| 131 |
<span class="tiered-pricing-table-row-qty-unit"> |
| 132 |
<?php echo esc_attr( ' ' . $settings['quantity_measurement_plural'] ); ?> |
| 133 |
</span> |
| 134 |
</span> |
| 135 |
<?php endif; ?> |
| 136 |
|
| 137 |
<?php do_action( 'tiered_pricing_table/table/label', $pricing_rule, $minimum, array( |
| 138 |
'id' => $id, |
| 139 |
'style' => 'default', |
| 140 |
) ); ?> |
| 141 |
</div> |
| 142 |
<?php endif; ?> |
| 143 |
|
| 144 |
<?php if ( $settings['discount_column_title'] ) : ?> |
| 145 |
|
| 146 |
<?php |
| 147 |
$discountAmount = 0; |
| 148 |
if ( CalculationLogic::calculateDiscountBasedOnRegularPrice() && $product->is_on_sale() ) { |
| 149 |
$discountAmount = PriceManager::calculateDiscount( $product->get_regular_price(), |
| 150 |
$product->get_sale_price() ); |
| 151 |
} |
| 152 |
?> |
| 153 |
|
| 154 |
<div> |
| 155 |
<?php if ( $discountAmount > 0 ) : ?> |
| 156 |
<span class="tiered-pricing-table-row-discount"><?php echo wp_kses_post( PricingTable::formatDiscount( $discountAmount, $pricing_rule, $product, null, $settings ) ); ?></span> |
| 157 |
<?php else : ?> |
| 158 |
<span class="tiered-pricing-table-row-discount--empty">—</span> |
| 159 |
<?php endif; ?> |
| 160 |
</div> |
| 161 |
<?php endif; ?> |
| 162 |
|
| 163 |
<?php if ( $settings['price_column_title'] ) : ?> |
| 164 |
<div class="tiered-pricing-table-row-price"> |
| 165 |
<?php |
| 166 |
echo wp_kses_post( wc_price( wc_get_price_to_display( wc_get_product( $product_id ), |
| 167 |
array( 'price' => $real_price, ) ) ) ); |
| 168 |
?> |
| 169 |
</div> |
| 170 |
<?php endif; ?> |
| 171 |
|
| 172 |
<?php |
| 173 |
ob_start(); |
| 174 |
do_action( 'tiered_pricing_table/tiered_pricing/row_columns', $pricing_rule, null ); |
| 175 |
$custom_cols = ob_get_clean(); |
| 176 |
echo wp_kses_post( str_replace(['<td', '</td>'], ['<div', '</div>'], $custom_cols) ); |
| 177 |
?> |
| 178 |
</div> |
| 179 |
<?php $tptTierRows[] = ob_get_clean(); ?> |
| 180 |
|
| 181 |
<?php $iterator = new ArrayIterator( $pricing_rule->getRules() ); ?> |
| 182 |
|
| 183 |
<?php while ( $iterator->valid() ) : ob_start(); ?> |
| 184 |
<?php |
| 185 |
$currentPrice = $iterator->current(); |
| 186 |
$currentQuantity = $iterator->key(); |
| 187 |
|
| 188 |
$iterator->next(); |
| 189 |
|
| 190 |
if ( $pricing_rule->getType() === 'percentage' ) { |
| 191 |
$discountAmount = $currentPrice; |
| 192 |
} else { |
| 193 |
$discountAmount = PriceManager::calculateDiscount( CalculationLogic::calculateDiscountBasedOnRegularPrice() ? $product->get_regular_price() : $product->get_price(), |
| 194 |
$pricing_rule->getTierPrice( $currentQuantity, false ) ); |
| 195 |
} |
| 196 |
|
| 197 |
$quantity = number_format_i18n( $currentQuantity ); |
| 198 |
|
| 199 |
if ( $iterator->valid() ) { |
| 200 |
|
| 201 |
if ( intval( $iterator->key() - 1 != $currentQuantity ) && 'range' === $settings['quantity_type'] ) { |
| 202 |
$quantity .= ' - ' . number_format_i18n( intval( $iterator->key() - 1 ) ); |
| 203 |
} |
| 204 |
|
| 205 |
} else { |
| 206 |
$quantity .= apply_filters( 'tiered_pricing_table/tiered_pricing/last_tier_postfix', '+', |
| 207 |
$currentQuantity, $pricing_rule, 'table' ); |
| 208 |
} |
| 209 |
|
| 210 |
$quantityUnit = $settings['quantity_measurement_plural']; |
| 211 |
|
| 212 |
$currentProductPrice = $pricing_rule->getTierPrice( $currentQuantity ); |
| 213 |
|
| 214 |
$currentProductPriceExcludeTaxes = wc_get_price_excluding_tax( wc_get_product( $product_id ), array( |
| 215 |
'price' => $pricing_rule->getTierPrice( $currentQuantity, false ), |
| 216 |
) ); |
| 217 |
|
| 218 |
$currentProductPriceIncludeTaxes = wc_get_price_including_tax( wc_get_product( $product_id ), array( |
| 219 |
'price' => $pricing_rule->getTierPrice( $currentQuantity, false ), |
| 220 |
) ); |
| 221 |
|
| 222 |
?> |
| 223 |
<div class="tiered-pricing-table-row" data-tiered-quantity="<?php echo esc_attr( $currentQuantity ); ?>" |
| 224 |
data-tiered-price="<?php echo esc_attr( $currentProductPrice ); ?>" |
| 225 |
data-tiered-price-exclude-taxes="<?php echo esc_attr( $currentProductPriceExcludeTaxes ); ?>" |
| 226 |
data-tiered-price-include-taxes="<?php echo esc_attr( $currentProductPriceIncludeTaxes ); ?>" |
| 227 |
style="grid-template-columns: repeat(<?php echo (int) $columns; ?>, 1fr);"> |
| 228 |
|
| 229 |
<?php if ( $settings['quantity_column_title'] ) : ?> |
| 230 |
<div> |
| 231 |
<span class="tiered-pricing-table-row-qty"><?php echo esc_attr( $quantity ); ?></span> |
| 232 |
<span class="tiered-pricing-table-row-qty-unit"> <?php echo esc_attr( $quantityUnit ); ?></span> |
| 233 |
<?php do_action( 'tiered_pricing_table/table/label', $pricing_rule, $currentQuantity, |
| 234 |
array( |
| 235 |
'id' => $id, |
| 236 |
'style' => 'default', |
| 237 |
) ); ?> |
| 238 |
</div> |
| 239 |
<?php endif; ?> |
| 240 |
|
| 241 |
<?php if ( $settings['discount_column_title'] ) : ?> |
| 242 |
<div> |
| 243 |
<?php if ( $discountAmount > 0 ) : ?> |
| 244 |
<span class="tiered-pricing-table-row-discount"><?php echo wp_kses_post( PricingTable::formatDiscount( $discountAmount, $pricing_rule, $product, $currentQuantity, $settings ) ); ?></span> |
| 245 |
<?php else : ?> |
| 246 |
<span class="tiered-pricing-table-row-discount--empty">—</span> |
| 247 |
<?php endif; ?> |
| 248 |
</div> |
| 249 |
<?php endif; ?> |
| 250 |
|
| 251 |
<?php if ( $settings['price_column_title'] ) : ?> |
| 252 |
<div class="tiered-pricing-table-row-price"> |
| 253 |
<?php |
| 254 |
echo wp_kses_post( wc_price( $pricing_rule->getTierPrice( $currentQuantity ) ) ); |
| 255 |
?> |
| 256 |
</div> |
| 257 |
<?php endif; ?> |
| 258 |
|
| 259 |
<?php |
| 260 |
ob_start(); |
| 261 |
do_action( 'tiered_pricing_table/tiered_pricing/row_columns', $pricing_rule, $currentQuantity ); |
| 262 |
$custom_cols = ob_get_clean(); |
| 263 |
echo wp_kses_post( str_replace(['<td', '</td>'], ['<div', '</div>'], $custom_cols) ); |
| 264 |
?> |
| 265 |
</div> |
| 266 |
|
| 267 |
<?php $tptTierRows[] = ob_get_clean(); endwhile; ?> |
| 268 |
<?php echo PricingTable::orderTiers( $tptTierRows, $settings ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- rows escaped when rendered above ?> |
| 269 |
<?php do_action( 'tiered_pricing_table/tiered_pricing/rows', $pricing_rule, $settings, 'tiered-pricing-table-style-3' ); ?> |
| 270 |
<?php do_action( 'tiered_pricing_table/table/tfoot', $pricing_rule, $settings, 'tiered-pricing-table-style-3' ); ?> |
| 271 |
</div> |
| 272 |
</div> |
| 273 |
|
| 274 |
<?php do_action( 'tiered_pricing_table/tiered_pricing/after', $pricing_rule, $product_id ); ?> |
| 275 |
</div> |
| 276 |
|
| 277 |
<style> |
| 278 |
<?php |
| 279 |
if ( $settings['clickable_rows'] && tpt_fs()->can_use_premium_code()) { |
| 280 |
echo esc_attr('#' . $id) . ' .tiered-pricing-table-row { cursor: pointer; }'; |
| 281 |
echo esc_attr('#' . $id) . ' .tiered-pricing-table-row:not(.tiered-pricing--active):hover { border-color: #d1d5db; background-color: #f9fafb; transform: scale(1.01); }'; |
| 282 |
} |
| 283 |
?> |
| 284 |
|
| 285 |
<?php echo esc_attr('#' . $id); ?> .tiered-pricing--active { |
| 286 |
background-color: #f3f4f6; |
| 287 |
border-color: <?php echo esc_attr($settings['active_tier_color']); ?> !important; |
| 288 |
} |
| 289 |
|
| 290 |
<?php echo esc_attr('#' . $id); ?> .tiered-pricing-table-row-discount { |
| 291 |
background-color: <?php echo esc_attr( ! empty( $settings['discount_badge_color'] ) ? $settings['discount_badge_color'] : $settings['active_tier_color'] ); ?> !important; |
| 292 |
} |
| 293 |
</style> |
| 294 |
<?php endif; ?> |
| 295 |
|