| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | use TierPricingTable\CalculationLogic; |
| 4 | 4 | use TierPricingTable\PriceManager; |
| 5 | + use TierPricingTable\PricingTable; | |
| 5 | 6 | use TierPricingTable\PricingRule; |
| 6 | 7 | use TierPricingTable\Settings\Settings; |
| 7 | 8 | |
| 8 | 9 | if ( ! defined( 'WPINC' ) ) { |
| @@ -27,9 +28,9 @@ | ||
| 27 | 28 | if ( ! function_exists( 'tptParseOptionText' ) ) { |
| 28 | 29 | function tptParseOptionText( $text, $quantity, $discount = null, $base_unit_name = null ) { |
| 29 | 30 | return strtr( $text, array( |
| 30 | 31 | '{tp_quantity}' => $quantity, |
| 31 | - '{tp_discount}' => $discount, | |
| 32 | + '{tp_discount}' => ! is_null( $discount ) ? round( (float) $discount, \TierPricingTable\PricingTable::discountDecimals( (float) $discount ) ) : $discount, | |
| 32 | 33 | '{tp_rounded_discount}' => ! is_null( $discount ) ? round( $discount ) : 0, |
| 33 | 34 | '{tp_base_unit_name}' => $base_unit_name, |
| 34 | 35 | ) ); |
| 35 | 36 | } |
| @@ -70,9 +71,9 @@ | ||
| 70 | 71 | |
| 71 | 72 | <div class="tiered-pricing-dropdown <?php echo ( isset( $settings['compact_layout'] ) && $settings['compact_layout'] === 'yes' ) ? 'tiered-pricing-dropdown--slim' : ''; ?>" |
| 72 | 73 | id="<?php echo esc_attr( $id ); ?>" |
| 73 | 74 | data-product-id="<?php echo esc_attr( $product_id ); ?>" |
| 74 | - data-price-rules="<?php echo esc_attr( htmlspecialchars( json_encode( $price_rules ) ) ); ?>" | |
| 75 | + data-price-rules="<?php echo esc_attr( htmlspecialchars( json_encode( $price_rules ), ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ) ); ?>" | |
| 75 | 76 | data-minimum="<?php echo esc_attr( $minimum ); ?>" |
| 76 | 77 | data-product-name="<?php echo esc_attr( $product_name ); ?>" |
| 77 | 78 | data-regular-price="<?php echo esc_attr( $regular_price ); ?>" |
| 78 | 79 | data-sale-price="<?php echo esc_attr( $sale_price ); ?>" |
| @@ -93,9 +94,9 @@ | ||
| 93 | 94 | |
| 94 | 95 | <?php if ( 1 >= array_keys( $price_rules )[0] - $minimum || 'static' === $settings['quantity_type'] ) : ?> |
| 95 | 96 | <?php |
| 96 | 97 | $quantity = esc_attr( number_format_i18n( $minimum ) . ' ' ); |
| 97 | - $baseUnitName = $settings['quantity_measurement_singular']; | |
| 98 | + $baseUnitName = $minimum > 1 ? $settings['quantity_measurement_plural'] : $settings['quantity_measurement_singular']; | |
| 98 | 99 | ?> |
| 99 | 100 | <?php else : ?> |
| 100 | 101 | <?php |
| 101 | 102 | $quantity = esc_attr( number_format_i18n( $minimum ) . ' - ' . number_format_i18n( array_keys( $price_rules )[0] - 1 ) . ' ' ); |
| @@ -105,9 +106,9 @@ | ||
| 105 | 106 | |
| 106 | 107 | <?php if ( $discountAmount > 0 ) : ?> |
| 107 | 108 | <?php |
| 108 | 109 | echo wp_kses_post( tptParseOptionText( $settings['options_option_text'], $quantity, |
| 109 | - $discountAmount, $baseUnitName ) ); | |
| 110 | + round( $discountAmount, 2 ), $baseUnitName ) ); | |
| 110 | 111 | ?> |
| 111 | 112 | <?php else : ?> |
| 112 | 113 | <?php |
| 113 | 114 | echo wp_kses_post( tptParseOptionText( $settings['options_default_option_text'], $quantity, |
| @@ -158,9 +159,10 @@ | ||
| 158 | 159 | </div> |
| 159 | 160 | |
| 160 | 161 | <div class="tiered-pricing-dropdown__list"> |
| 161 | 162 | <ul role="listbox" id="tiered-pricing-dropdown-list-<?php echo esc_attr( $id ); ?>"> |
| 162 | - <li class="tiered-pricing-dropdown-option tiered-pricing-dropdown-option--active tiered-pricing-dropdown-option--default" | |
| 163 | + <?php $tptTierRows = array(); ob_start(); ?> | |
| 164 | + <li class="tiered-pricing-dropdown-option tiered-pricing--active tiered-pricing-dropdown-option--selected tiered-pricing-dropdown-option--default" | |
| 163 | 165 | role="option" |
| 164 | 166 | aria-selected="true" |
| 165 | 167 | data-tiered-quantity="<?php echo esc_attr( $minimum ); ?>" |
| 166 | 168 | data-tiered-price="<?php echo esc_attr( $price ); ?>" |
| @@ -170,9 +172,9 @@ | ||
| 170 | 172 | <div class="tiered-pricing-dropdown-option__quantity"> |
| 171 | 173 | <?php if ( 1 >= array_keys( $price_rules )[0] - $minimum || 'static' === $settings['quantity_type'] ) : ?> |
| 172 | 174 | <?php |
| 173 | 175 | $quantity = esc_attr( number_format_i18n( $minimum ) . ' ' ); |
| 174 | - $baseUnitName = $settings['quantity_measurement_singular']; | |
| 176 | + $baseUnitName = $minimum > 1 ? $settings['quantity_measurement_plural'] : $settings['quantity_measurement_singular']; | |
| 175 | 177 | ?> |
| 176 | 178 | <?php else : ?> |
| 177 | 179 | <?php |
| 178 | 180 | $quantity = esc_attr( number_format_i18n( $minimum ) . ' - ' . number_format_i18n( array_keys( $price_rules )[0] - 1 ) . ' ' ); |
| @@ -182,9 +184,9 @@ | ||
| 182 | 184 | |
| 183 | 185 | <?php if ( $discountAmount > 0 ) : ?> |
| 184 | 186 | <?php |
| 185 | 187 | echo wp_kses_post( tptParseOptionText( $settings['options_option_text'], $quantity, |
| 186 | - $discountAmount, $baseUnitName ) ); | |
| 188 | + round( $discountAmount, 2 ), $baseUnitName ) ); | |
| 187 | 189 | ?> |
| 188 | 190 | <?php else : ?> |
| 189 | 191 | <?php |
| 190 | 192 | echo wp_kses_post( tptParseOptionText( $settings['options_default_option_text'], |
| @@ -225,11 +227,12 @@ | ||
| 225 | 227 | </div> |
| 226 | 228 | </div> |
| 227 | 229 | </div> |
| 228 | 230 | </li> |
| 231 | + <?php $tptTierRows[] = ob_get_clean(); ?> | |
| 229 | 232 | <?php $iterator = new ArrayIterator( $price_rules ); ?> |
| 230 | 233 | |
| 231 | - <?php while ( $iterator->valid() ) : ?> | |
| 234 | + <?php while ( $iterator->valid() ) : ob_start(); ?> | |
| 232 | 235 | <?php |
| 233 | 236 | $currentPrice = $iterator->current(); |
| 234 | 237 | $currentQuantity = $iterator->key(); |
| 235 | 238 | |
| @@ -242,17 +245,12 @@ | ||
| 242 | 245 | |
| 243 | 246 | $iterator->next(); |
| 244 | 247 | |
| 245 | 248 | if ( $iterator->valid() ) { |
| 246 | - $quantity = $currentQuantity; | |
| 247 | - | |
| 248 | - if ( intval( $iterator->key() - 1 != $currentQuantity ) ) { | |
| 249 | - | |
| 250 | - $quantity = number_format_i18n( $quantity ); | |
| 251 | - | |
| 252 | - if ( 'range' === $settings['quantity_type'] ) { | |
| 253 | - $quantity .= ' - ' . number_format_i18n( intval( $iterator->key() - 1 ) ); | |
| 254 | - } | |
| 249 | + $quantity = number_format_i18n( $currentQuantity ); | |
| 250 | + | |
| 251 | + if ( (int) $iterator->key() - 1 !== (int) $currentQuantity && 'range' === $settings['quantity_type'] ) { | |
| 252 | + $quantity .= ' - ' . number_format_i18n( intval( $iterator->key() - 1 ) ); | |
| 255 | 253 | } |
| 256 | 254 | } else { |
| 257 | 255 | $quantity = number_format_i18n( $currentQuantity ); |
| 258 | 256 | |
| @@ -316,14 +314,15 @@ | ||
| 316 | 314 | </div> |
| 317 | 315 | </div> |
| 318 | 316 | </div> |
| 319 | 317 | </li> |
| 320 | - <?php endwhile; ?> | |
| 318 | + <?php $tptTierRows[] = ob_get_clean(); endwhile; ?> | |
| 319 | + <?php echo PricingTable::orderTiers( $tptTierRows, $settings ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- rows escaped when rendered above ?> | |
| 321 | 320 | <?php do_action( 'tiered_pricing_table/dropdown/options', $pricing_rule ); ?> |
| 322 | 321 | </ul> |
| 323 | - | |
| 324 | - <?php do_action( 'tiered_pricing_table/dropdown/after_options', $pricing_rule ); ?> | |
| 325 | 322 | </div> |
| 323 | + | |
| 324 | + <?php do_action( 'tiered_pricing_table/dropdown/after_options', $pricing_rule ); ?> | |
| 326 | 325 | </div> |
| 327 | 326 | </div> |
| 328 | 327 | |
| 329 | 328 | <style> |