| 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 |
$num_rows = 0; |
| 51 |
if ( $settings['quantity_column_title'] ) $num_rows++; |
| 52 |
if ( $settings['discount_column_title'] ) $num_rows++; |
| 53 |
if ( $settings['price_column_title'] ) $num_rows++; |
| 54 |
|
| 55 |
// Detect custom columns via output buffering |
| 56 |
ob_start(); |
| 57 |
do_action( 'tiered_pricing_table/tiered_pricing/header_columns', $pricing_rule ); |
| 58 |
$custom_head = ob_get_clean(); |
| 59 |
$custom_columns_count = substr_count($custom_head, '<th'); |
| 60 |
$num_rows += $custom_columns_count; |
| 61 |
?> |
| 62 |
|
| 63 |
<?php if ( ! empty( $pricing_rule->getRules() ) ) : ?> |
| 64 |
<div class="clear"></div> |
| 65 |
|
| 66 |
<div class="tiered-pricing-wrapper"> |
| 67 |
<?php if ( ! empty( $settings['title'] ) ) : ?> |
| 68 |
<h3 style="clear:both; margin: 20px 0;"><?php echo esc_attr( $settings['title'] ); ?></h3> |
| 69 |
<?php endif; ?> |
| 70 |
|
| 71 |
<?php do_action( 'tiered_pricing_table/tiered_pricing/before', $pricing_rule ); ?> |
| 72 |
|
| 73 |
<div class="tiered-pricing-horizontal-table <?php echo ( isset( $settings['compact_layout'] ) && $settings['compact_layout'] === 'yes' ) ? 'tiered-pricing-horizontal-table--slim' : ''; ?>" |
| 74 |
id="<?php echo esc_attr( $id ); ?>" |
| 75 |
<?php echo \TierPricingTable\PricingTable::layoutStyleAttribute( $settings ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped in the helper ?> |
| 76 |
data-tiered-pricing-table |
| 77 |
data-product-id="<?php echo esc_attr( $product_id ); ?>" |
| 78 |
data-price-rules=" |
| 79 |
<?php |
| 80 |
echo esc_attr( htmlspecialchars( json_encode( $pricing_rule->getRules() ), ENT_QUOTES ) ); |
| 81 |
?> |
| 82 |
" |
| 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-horizontal-table-column tiered-pricing-horizontal-table__labels"> |
| 91 |
|
| 92 |
<?php if ( $settings['quantity_column_title'] ) : ?> |
| 93 |
<div class="tiered-pricing-horizontal-table-cell tiered-pricing-horizontal-table-cell--quantity"> |
| 94 |
<strong> |
| 95 |
<?php echo esc_attr( $settings['quantity_column_title'] ); ?> |
| 96 |
</strong> |
| 97 |
</div> |
| 98 |
<?php endif; ?> |
| 99 |
|
| 100 |
<?php if ( $settings['discount_column_title'] ) : ?> |
| 101 |
<div class="tiered-pricing-horizontal-table-cell tiered-pricing-horizontal-table-cell--discount"> |
| 102 |
<strong> |
| 103 |
<?php echo esc_attr( ( $settings['discount_column_title'] ) ); ?> |
| 104 |
</strong> |
| 105 |
</div> |
| 106 |
<?php endif; ?> |
| 107 |
|
| 108 |
<?php if ( $settings['price_column_title'] ) : ?> |
| 109 |
<div class="tiered-pricing-horizontal-table-cell tiered-pricing-horizontal-table-cell--price"> |
| 110 |
<strong> |
| 111 |
<?php echo esc_attr( $settings['price_column_title'] ); ?> |
| 112 |
</strong> |
| 113 |
</div> |
| 114 |
<?php endif; ?> |
| 115 |
|
| 116 |
<?php echo wp_kses_post( str_replace(['<th', '</th>'], ['<div class="tiered-pricing-horizontal-table-cell"', '</div>'], $custom_head) ); ?> |
| 117 |
</div> |
| 118 |
<?php $tptTierRows = array(); ob_start(); ?> |
| 119 |
<div class="tiered-pricing-horizontal-table-column tiered-pricing-horizontal-table__values tiered-pricing--active" |
| 120 |
data-tiered-quantity="<?php echo esc_attr( $minimum ); ?>" |
| 121 |
data-tiered-price="<?php echo esc_attr( $price ); ?>" |
| 122 |
data-tiered-price-exclude-taxes="<?php echo esc_attr( $price_excl_taxes ); ?>" |
| 123 |
data-tiered-price-include-taxes="<?php echo esc_attr( $price_incl_taxes ); ?>"> |
| 124 |
|
| 125 |
<?php if ( $settings['quantity_column_title'] ) : ?> |
| 126 |
<div class="tiered-pricing-horizontal-table-cell tiered-pricing-horizontal-table-cell--quantity"> |
| 127 |
<?php if ( 1 >= array_keys( $pricing_rule->getRules() )[0] - $minimum || 'static' === $settings['quantity_type'] ) : ?> |
| 128 |
<span> |
| 129 |
<span> |
| 130 |
<?php echo esc_attr( number_format_i18n( $minimum ) ); ?> |
| 131 |
</span> |
| 132 |
<span> |
| 133 |
<?php echo esc_attr( ' ' . ( $minimum > 1 ? $settings['quantity_measurement_plural'] : $settings['quantity_measurement_singular'] ) ); ?> |
| 134 |
</span> |
| 135 |
</span> |
| 136 |
<?php else : ?> |
| 137 |
<span> |
| 138 |
<span> |
| 139 |
<?php echo esc_attr( number_format_i18n( $minimum ) ); ?> - <?php echo esc_attr( number_format_i18n( array_keys( $pricing_rule->getRules() )[0] - 1 ) ); ?> |
| 140 |
</span> |
| 141 |
<span> |
| 142 |
<?php echo esc_attr( ' ' . $settings['quantity_measurement_plural'] ); ?> |
| 143 |
</span> |
| 144 |
</span> |
| 145 |
<?php endif; ?> |
| 146 |
|
| 147 |
<?php |
| 148 |
do_action( 'tiered_pricing_table/horizontal-table/label', $pricing_rule, $minimum, array( |
| 149 |
'id' => $id, |
| 150 |
'style' => 'default', |
| 151 |
) ); |
| 152 |
?> |
| 153 |
|
| 154 |
</div> |
| 155 |
<?php endif; ?> |
| 156 |
|
| 157 |
<?php if ( $settings['discount_column_title'] ) : ?> |
| 158 |
|
| 159 |
<?php |
| 160 |
$discountAmount = 0; |
| 161 |
if ( CalculationLogic::calculateDiscountBasedOnRegularPrice() && $product->is_on_sale() ) { |
| 162 |
$discountAmount = PriceManager::calculateDiscount( $product->get_regular_price(), |
| 163 |
$product->get_sale_price() ); |
| 164 |
} |
| 165 |
?> |
| 166 |
|
| 167 |
<div class="tiered-pricing-horizontal-table-cell tiered-pricing-horizontal-table-cell--discount"> |
| 168 |
<?php if ( $discountAmount > 0 ) : ?> |
| 169 |
<span><?php echo wp_kses_post( PricingTable::formatDiscount( $discountAmount, $pricing_rule, $product, null, $settings ) ); ?></span> |
| 170 |
<?php else : ?> |
| 171 |
<span>—</span> |
| 172 |
<?php endif; ?> |
| 173 |
</div> |
| 174 |
<?php endif; ?> |
| 175 |
|
| 176 |
<?php if ( $settings['price_column_title'] ) : ?> |
| 177 |
<div class="tiered-pricing-horizontal-table-cell tiered-pricing-horizontal-table-cell--price"> |
| 178 |
<?php |
| 179 |
echo wp_kses_post( wc_price( wc_get_price_to_display( wc_get_product( $product_id ), |
| 180 |
array( 'price' => $real_price, ) ) ) ); |
| 181 |
?> |
| 182 |
</div> |
| 183 |
<?php endif; ?> |
| 184 |
|
| 185 |
<?php |
| 186 |
ob_start(); |
| 187 |
do_action( 'tiered_pricing_table/tiered_pricing/row_columns', $pricing_rule, null ); |
| 188 |
$custom_cols = ob_get_clean(); |
| 189 |
echo wp_kses_post( str_replace(['<td', '</td>'], ['<div class="tiered-pricing-horizontal-table-cell"', '</div>'], $custom_cols) ); |
| 190 |
?> |
| 191 |
</div> |
| 192 |
<?php $tptTierRows[] = ob_get_clean(); ?> |
| 193 |
|
| 194 |
<?php $iterator = new ArrayIterator( $pricing_rule->getRules() ); ?> |
| 195 |
|
| 196 |
<?php while ( $iterator->valid() ) : ob_start(); ?> |
| 197 |
<?php |
| 198 |
$currentPrice = $iterator->current(); |
| 199 |
$currentQuantity = $iterator->key(); |
| 200 |
|
| 201 |
$iterator->next(); |
| 202 |
|
| 203 |
if ( $pricing_rule->getType() === 'percentage' ) { |
| 204 |
$discountAmount = $currentPrice; |
| 205 |
} else { |
| 206 |
$discountAmount = PriceManager::calculateDiscount( CalculationLogic::calculateDiscountBasedOnRegularPrice() ? $product->get_regular_price() : $product->get_price(), |
| 207 |
$pricing_rule->getTierPrice( $currentQuantity, false ) ); |
| 208 |
} |
| 209 |
|
| 210 |
$quantity = number_format_i18n( $currentQuantity ); |
| 211 |
|
| 212 |
if ( $iterator->valid() ) { |
| 213 |
|
| 214 |
if ( intval( $iterator->key() - 1 != $currentQuantity ) && 'range' === $settings['quantity_type'] ) { |
| 215 |
$quantity .= ' - ' . number_format_i18n( intval( $iterator->key() - 1 ) ); |
| 216 |
} |
| 217 |
|
| 218 |
} else { |
| 219 |
$quantity .= apply_filters( 'tiered_pricing_table/tiered_pricing/last_tier_postfix', '+', |
| 220 |
$currentQuantity, $pricing_rule, 'table' ); |
| 221 |
} |
| 222 |
|
| 223 |
$quantity .= ' ' . $settings['quantity_measurement_plural']; |
| 224 |
|
| 225 |
$currentProductPrice = $pricing_rule->getTierPrice( $currentQuantity ); |
| 226 |
|
| 227 |
$currentProductPriceExcludeTaxes = wc_get_price_excluding_tax( wc_get_product( $product_id ), array( |
| 228 |
'price' => $pricing_rule->getTierPrice( $currentQuantity, false ), |
| 229 |
) ); |
| 230 |
|
| 231 |
$currentProductPriceIncludeTaxes = wc_get_price_including_tax( wc_get_product( $product_id ), array( |
| 232 |
'price' => $pricing_rule->getTierPrice( $currentQuantity, false ), |
| 233 |
) ); |
| 234 |
|
| 235 |
?> |
| 236 |
<div class="tiered-pricing-horizontal-table-column tiered-pricing-horizontal-table__values" |
| 237 |
data-tiered-quantity="<?php echo esc_attr( $currentQuantity ); ?>" |
| 238 |
data-tiered-price="<?php echo esc_attr( $currentProductPrice ); ?>" |
| 239 |
data-tiered-price-exclude-taxes="<?php echo esc_attr( $currentProductPriceExcludeTaxes ); ?>" |
| 240 |
data-tiered-price-include-taxes="<?php echo esc_attr( $currentProductPriceIncludeTaxes ); ?>"> |
| 241 |
|
| 242 |
<?php if ( $settings['quantity_column_title'] ) : ?> |
| 243 |
<div class="tiered-pricing-horizontal-table-cell tiered-pricing-horizontal-table-cell--quantity"> |
| 244 |
<span><?php echo esc_attr( $quantity ); ?></span> |
| 245 |
|
| 246 |
<?php |
| 247 |
do_action( 'tiered_pricing_table/horizontal-table/label', $pricing_rule, |
| 248 |
$currentQuantity, array( |
| 249 |
'id' => $id, |
| 250 |
'style' => 'default', |
| 251 |
) ); |
| 252 |
?> |
| 253 |
</div> |
| 254 |
<?php endif; ?> |
| 255 |
|
| 256 |
<?php if ( $settings['discount_column_title'] ) : ?> |
| 257 |
<div |
| 258 |
class="tiered-pricing-horizontal-table-cell tiered-pricing-horizontal-table-cell--discount"> |
| 259 |
<?php if ( $discountAmount > 0 ) : ?> |
| 260 |
<span><?php echo wp_kses_post( PricingTable::formatDiscount( $discountAmount, $pricing_rule, $product, $currentQuantity, $settings ) ); ?></span> |
| 261 |
<?php else : ?> |
| 262 |
<span>—</span> |
| 263 |
<?php endif; ?> |
| 264 |
</div> |
| 265 |
<?php endif; ?> |
| 266 |
|
| 267 |
<?php if ( $settings['price_column_title'] ) : ?> |
| 268 |
<div class="tiered-pricing-horizontal-table-cell tiered-pricing-horizontal-table-cell--price"> |
| 269 |
<?php |
| 270 |
echo wp_kses_post( wc_price( $pricing_rule->getTierPrice( $currentQuantity ) ) ); |
| 271 |
?> |
| 272 |
</div> |
| 273 |
<?php endif; ?> |
| 274 |
|
| 275 |
<?php |
| 276 |
ob_start(); |
| 277 |
do_action( 'tiered_pricing_table/tiered_pricing/row_columns', $pricing_rule, $currentQuantity ); |
| 278 |
$custom_cols = ob_get_clean(); |
| 279 |
echo wp_kses_post( str_replace(['<td', '</td>'], ['<div class="tiered-pricing-horizontal-table-cell"', '</div>'], $custom_cols) ); |
| 280 |
?> |
| 281 |
</div> |
| 282 |
<?php $tptTierRows[] = ob_get_clean(); endwhile; ?> |
| 283 |
<?php echo PricingTable::orderTiers( $tptTierRows, $settings ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- rows escaped when rendered above ?> |
| 284 |
|
| 285 |
<?php do_action( 'tiered_pricing_table/horizontal-table/after_columns', $pricing_rule, $settings ); ?> |
| 286 |
</div> |
| 287 |
|
| 288 |
<?php do_action( 'tiered_pricing_table/tiered_pricing/after', $pricing_rule ); ?> |
| 289 |
</div> |
| 290 |
|
| 291 |
<style> |
| 292 |
<?php echo esc_attr('#' . $id); ?>[data-tiered-pricing-table] { |
| 293 |
display: grid; |
| 294 |
grid-auto-flow: column; |
| 295 |
grid-template-rows: repeat(<?php echo (int) $num_rows; ?>, auto); |
| 296 |
} |
| 297 |
|
| 298 |
<?php echo esc_attr('#' . $id); ?> .tiered-pricing-horizontal-table-column { |
| 299 |
display: contents; |
| 300 |
} |
| 301 |
|
| 302 |
<?php |
| 303 |
if ( $settings['clickable_rows']) { |
| 304 |
echo esc_attr('#' . $id) . ' .tiered-pricing-horizontal-table__values { cursor: pointer; }'; |
| 305 |
echo esc_attr('#' . $id) . ' .tiered-pricing-horizontal-table__values:hover .tiered-pricing-horizontal-table-cell { background: #f5f5f5; }'; |
| 306 |
} |
| 307 |
?> |
| 308 |
|
| 309 |
<?php echo esc_attr('#' . $id); ?> .tiered-pricing--active .tiered-pricing-horizontal-table-cell, |
| 310 |
<?php echo esc_attr('#' . $id); ?> .tiered-pricing--active td { |
| 311 |
background-color: <?php echo esc_attr($settings['active_tier_color']); ?> !important; |
| 312 |
color: #fff; |
| 313 |
} |
| 314 |
</style> |
| 315 |
<?php endif; ?> |