| 1 |
<?php use TierPricingTable\CalculationLogic; |
| 2 |
use TierPricingTable\PriceManager; |
| 3 |
use TierPricingTable\PricingRule; |
| 4 |
|
| 5 |
if ( ! defined( 'WPINC' ) ) { |
| 6 |
die; |
| 7 |
} |
| 8 |
/** |
| 9 |
* Available variables |
| 10 |
* |
| 11 |
* @var array $price_rules |
| 12 |
* @var PricingRule $pricing_rule |
| 13 |
* @var string $real_price |
| 14 |
* @var string $product_name |
| 15 |
* @var string $pricing_type |
| 16 |
* @var WC_Product $product |
| 17 |
* @var string $id |
| 18 |
* @var int $product_id |
| 19 |
* @var int $minimum |
| 20 |
* @var array $settings |
| 21 |
*/ |
| 22 |
|
| 23 |
$sale_price = $product->get_sale_price(); |
| 24 |
|
| 25 |
if ( $sale_price ) { |
| 26 |
$sale_price = wc_get_price_to_display( $product, array( |
| 27 |
'price' => $sale_price, |
| 28 |
) ); |
| 29 |
} |
| 30 |
|
| 31 |
$regular_price = wc_get_price_to_display( $product, array( |
| 32 |
'price' => $product->get_regular_price(), |
| 33 |
) ); |
| 34 |
|
| 35 |
$price = wc_get_price_to_display( $product, array( |
| 36 |
'price' => $product->get_price(), |
| 37 |
) ); |
| 38 |
|
| 39 |
?> |
| 40 |
|
| 41 |
<?php if ( ! empty( $price_rules ) ) : ?> |
| 42 |
|
| 43 |
<div class="tiered-pricing-wrapper"> |
| 44 |
<?php if ( ! empty( $settings['title'] ) ) : ?> |
| 45 |
<h3 style="clear:both; margin: 20px 0;"><?php echo esc_attr( $settings['title'] ); ?></h3> |
| 46 |
<?php endif; ?> |
| 47 |
|
| 48 |
<div class="tiered-pricing-blocks <?php echo ( isset( $settings['compact_layout'] ) && $settings['compact_layout'] === 'yes' ) ? 'tiered-pricing-blocks--slim' : ''; ?>" |
| 49 |
id="<?php echo esc_attr( $id ); ?>" |
| 50 |
data-product-id="<?php echo esc_attr( $product_id ); ?>" |
| 51 |
data-price-rules="<?php echo esc_attr( htmlspecialchars( json_encode( $price_rules ), ENT_QUOTES ) ); ?>" |
| 52 |
data-minimum="<?php echo esc_attr( $minimum ); ?>" |
| 53 |
data-product-name="<?php echo esc_attr( $product_name ); ?>" |
| 54 |
data-regular-price="<?php echo esc_attr( $regular_price ); ?>" |
| 55 |
data-sale-price="<?php echo esc_attr( $sale_price ); ?>" |
| 56 |
data-price="<?php echo esc_attr( $price ); ?>" |
| 57 |
data-product-price-suffix="<?php echo esc_attr( $product->get_price_suffix() ); ?>" |
| 58 |
> |
| 59 |
|
| 60 |
<div class="tiered-pricing-block tiered-pricing--active" |
| 61 |
data-tiered-quantity="<?php echo esc_attr( $minimum ); ?>" |
| 62 |
data-tiered-price=" |
| 63 |
<?php |
| 64 |
echo esc_attr( wc_get_price_to_display( wc_get_product( $product_id ), array( |
| 65 |
'price' => $real_price, |
| 66 |
) ) ); |
| 67 |
?> |
| 68 |
" |
| 69 |
data-tiered-price-exclude-taxes=" |
| 70 |
<?php |
| 71 |
echo esc_attr( wc_get_price_excluding_tax( wc_get_product( $product_id ), array( |
| 72 |
'price' => $real_price, |
| 73 |
) ) ); |
| 74 |
?> |
| 75 |
" |
| 76 |
data-tiered-price-include-taxes=" |
| 77 |
<?php |
| 78 |
echo esc_attr( wc_get_price_including_tax( wc_get_product( $product_id ), array( |
| 79 |
'price' => $real_price, |
| 80 |
) ) ); |
| 81 |
?> |
| 82 |
"> |
| 83 |
|
| 84 |
<?php |
| 85 |
do_action( 'tiered_pricing_table/blocks/label', $pricing_rule, $minimum, array( |
| 86 |
'id' => $id, |
| 87 |
'style' => 'default', |
| 88 |
) ); |
| 89 |
?> |
| 90 |
|
| 91 |
<div class="tiered-pricing-block__price"> |
| 92 |
<?php |
| 93 |
echo wp_kses_post( wc_price( wc_get_price_to_display( wc_get_product( $product_id ), array( |
| 94 |
'price' => $real_price, |
| 95 |
) ) ) ); |
| 96 |
?> |
| 97 |
|
| 98 |
<?php if ( $settings['show_discount_column'] ) : ?> |
| 99 |
<?php |
| 100 |
$discountAmount = 0; |
| 101 |
if ( CalculationLogic::calculateDiscountBasedOnRegularPrice() && $product->is_on_sale() ) { |
| 102 |
$discountAmount = PriceManager::calculateDiscount( $product->get_regular_price(), |
| 103 |
$product->get_sale_price() ); |
| 104 |
} |
| 105 |
?> |
| 106 |
<?php if ( $discountAmount > 0 ) : ?> |
| 107 |
<span class="tiered-pricing-block__price-discount"> |
| 108 |
<?php |
| 109 |
// translators: %d: discount amount |
| 110 |
echo esc_html( sprintf( __( '(%d%% off)', 'tier-pricing-table' ), |
| 111 |
round( $discountAmount, 2 ) ) ); |
| 112 |
?> |
| 113 |
</span> |
| 114 |
<?php endif; ?> |
| 115 |
<?php endif; ?> |
| 116 |
</div> |
| 117 |
|
| 118 |
<span class="tiered-pricing-block__quantity"> |
| 119 |
<?php if ( 1 >= array_keys( $price_rules )[0] - $minimum || 'static' === $settings['quantity_type'] ) : ?> |
| 120 |
<span><?php echo esc_attr( number_format_i18n( $minimum ) ); ?></span> |
| 121 |
<?php if ( $minimum > 1 ) : ?> |
| 122 |
<?php echo esc_html( $settings['quantity_measurement_plural'] ); ?> |
| 123 |
<?php else : ?> |
| 124 |
<?php echo esc_html( $settings['quantity_measurement_singular'] ); ?> |
| 125 |
<?php endif; ?> |
| 126 |
<?php else : ?> |
| 127 |
<span><?php echo esc_attr( number_format_i18n( $minimum ) ); ?> - <?php echo esc_attr( number_format_i18n( array_keys( $price_rules )[0] - 1 ) ); ?></span> |
| 128 |
<?php echo esc_html( $settings['quantity_measurement_plural'] ); ?> |
| 129 |
<?php endif; ?> |
| 130 |
</span> |
| 131 |
</div> |
| 132 |
|
| 133 |
<?php $iterator = new ArrayIterator( $price_rules ); ?> |
| 134 |
|
| 135 |
<?php while ( $iterator->valid() ) : ?> |
| 136 |
<?php |
| 137 |
$currentPrice = $iterator->current(); |
| 138 |
$currentQuantity = $iterator->key(); |
| 139 |
|
| 140 |
if ( 'percentage' === $pricing_type ) { |
| 141 |
$discountAmount = $currentPrice; |
| 142 |
} else { |
| 143 |
$discountAmount = PriceManager::calculateDiscount( CalculationLogic::calculateDiscountBasedOnRegularPrice() ? $product->get_regular_price() : $product->get_price(), |
| 144 |
$pricing_rule->getTierPrice( $currentQuantity, false ) ); |
| 145 |
} |
| 146 |
|
| 147 |
$iterator->next(); |
| 148 |
|
| 149 |
if ( $iterator->valid() ) { |
| 150 |
$quantity = $currentQuantity; |
| 151 |
|
| 152 |
if ( intval( $iterator->key() - 1 != $currentQuantity ) ) { |
| 153 |
|
| 154 |
$quantity = number_format_i18n( $quantity ); |
| 155 |
|
| 156 |
if ( 'range' === $settings['quantity_type'] ) { |
| 157 |
$quantity .= ' - ' . number_format_i18n( intval( $iterator->key() - 1 ) ); |
| 158 |
} |
| 159 |
} |
| 160 |
} else { |
| 161 |
$quantity = number_format_i18n( $currentQuantity ); |
| 162 |
|
| 163 |
$quantity .= apply_filters( 'tiered_pricing_table/tiered_pricing/last_tier_postfix', '+', |
| 164 |
$currentQuantity, $pricing_rule, 'blocks' ); |
| 165 |
} |
| 166 |
|
| 167 |
$quantity = $quantity . ' ' . $settings['quantity_measurement_plural']; |
| 168 |
|
| 169 |
$currentProductPrice = PriceManager::getPriceByRules( $currentQuantity, $product_id ); |
| 170 |
|
| 171 |
$currentProductPriceExcludeTaxes = wc_get_price_excluding_tax( wc_get_product( $product_id ), array( |
| 172 |
'price' => PriceManager::getPriceByRules( $currentQuantity, $product_id, null, null, false ), |
| 173 |
) ); |
| 174 |
|
| 175 |
$currentProductPriceIncludeTaxes = wc_get_price_including_tax( wc_get_product( $product_id ), array( |
| 176 |
'price' => PriceManager::getPriceByRules( $currentQuantity, $product_id, null, null, false ), |
| 177 |
) ); |
| 178 |
|
| 179 |
?> |
| 180 |
|
| 181 |
<div class="tiered-pricing-block" |
| 182 |
data-tiered-quantity="<?php echo esc_attr( $currentQuantity ); ?>" |
| 183 |
data-tiered-price="<?php echo esc_attr( $currentProductPrice ); ?>" |
| 184 |
data-tiered-price-exclude-taxes="<?php echo esc_attr( $currentProductPriceExcludeTaxes ); ?>" |
| 185 |
data-tiered-price-include-taxes="<?php echo esc_attr( $currentProductPriceIncludeTaxes ); ?>"> |
| 186 |
|
| 187 |
<?php |
| 188 |
do_action( 'tiered_pricing_table/blocks/label', $pricing_rule, $currentQuantity, array( |
| 189 |
'id' => $id, |
| 190 |
'style' => 'default', |
| 191 |
) ); |
| 192 |
?> |
| 193 |
|
| 194 |
<div class="tiered-pricing-block__price"> |
| 195 |
<span> |
| 196 |
<?php |
| 197 |
echo wp_kses_post( wc_price( PriceManager::getPriceByRules( $currentQuantity, |
| 198 |
$product_id ) ) ); |
| 199 |
?> |
| 200 |
</span> |
| 201 |
|
| 202 |
<?php if ( $settings['show_discount_column'] ) : ?> |
| 203 |
<span class="tiered-pricing-block__price-discount"> |
| 204 |
<?php |
| 205 |
// translators: %d: discount amount |
| 206 |
echo esc_html( sprintf( __( '(%d%% off)', 'tier-pricing-table' ), |
| 207 |
round( $discountAmount, 2 ) ) ); |
| 208 |
?> |
| 209 |
</span> |
| 210 |
<?php endif; ?> |
| 211 |
</div> |
| 212 |
<span class="tiered-pricing-block__quantity"><?php echo esc_html( $quantity ); ?></span> |
| 213 |
</div> |
| 214 |
<?php endwhile; ?> |
| 215 |
|
| 216 |
<?php do_action( 'tiered_pricing_table/blocks/blocks', $pricing_rule, $settings ); ?> |
| 217 |
</div> |
| 218 |
|
| 219 |
<?php do_action( 'tiered_pricing_table/blocks/after_blocks', $pricing_rule ); ?> |
| 220 |
</div> |
| 221 |
|
| 222 |
<style> |
| 223 |
<?php |
| 224 |
if ( $settings['clickable_rows']) { |
| 225 |
echo esc_attr('#' . $id) . ' .tiered-pricing-block {cursor: pointer; }'; |
| 226 |
} |
| 227 |
?> |
| 228 |
|
| 229 |
<?php echo esc_attr('#' . $id); ?> |
| 230 |
.tiered-pricing--active { |
| 231 |
border-color: <?php echo esc_attr($settings['active_tier_color']); ?> !important; |
| 232 |
} |
| 233 |
</style> |
| 234 |
<?php endif; ?> |