| @@ -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 ); ?>" |
| @@ -86,9 +87,9 @@ | ||
| 86 | 87 | $product->get_sale_price() ); |
| 87 | 88 | } |
| 88 | 89 | ?> |
| 89 | 90 | |
| 90 | - <div class="tiered-pricing-dropdown__select-box" tabindex="0"> | |
| 91 | + <div class="tiered-pricing-dropdown__select-box" tabindex="0" role="combobox" aria-haspopup="listbox" aria-expanded="false" aria-controls="tiered-pricing-dropdown-list-<?php echo esc_attr( $id ); ?>"> | |
| 91 | 92 | <div class="tiered-pricing-dropdown-option"> |
| 92 | 93 | <div class="tiered-pricing-dropdown-option__quantity"> |
| 93 | 94 | |
| 94 | 95 | <?php if ( 1 >= array_keys( $price_rules )[0] - $minimum || 'static' === $settings['quantity_type'] ) : ?> |
| @@ -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, |
| @@ -157,10 +158,13 @@ | ||
| 157 | 158 | </div> |
| 158 | 159 | </div> |
| 159 | 160 | |
| 160 | 161 | <div class="tiered-pricing-dropdown__list"> |
| 161 | - <ul> | |
| 162 | - <li class="tiered-pricing-dropdown-option tiered-pricing-dropdown-option--active tiered-pricing-dropdown-option--default" | |
| 162 | + <ul role="listbox" id="tiered-pricing-dropdown-list-<?php echo esc_attr( $id ); ?>"> | |
| 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" | |
| 165 | + role="option" | |
| 166 | + aria-selected="true" | |
| 163 | 167 | data-tiered-quantity="<?php echo esc_attr( $minimum ); ?>" |
| 164 | 168 | data-tiered-price="<?php echo esc_attr( $price ); ?>" |
| 165 | 169 | data-tiered-price-exclude-taxes="<?php echo esc_attr( $price_excl_taxes ); ?>" |
| 166 | 170 | data-tiered-price-include-taxes="<?php echo esc_attr( $price_incl_taxes ); ?>"> |
| @@ -168,9 +172,9 @@ | ||
| 168 | 172 | <div class="tiered-pricing-dropdown-option__quantity"> |
| 169 | 173 | <?php if ( 1 >= array_keys( $price_rules )[0] - $minimum || 'static' === $settings['quantity_type'] ) : ?> |
| 170 | 174 | <?php |
| 171 | 175 | $quantity = esc_attr( number_format_i18n( $minimum ) . ' ' ); |
| 172 | - $baseUnitName = $settings['quantity_measurement_singular']; | |
| 176 | + $baseUnitName = $minimum > 1 ? $settings['quantity_measurement_plural'] : $settings['quantity_measurement_singular']; | |
| 173 | 177 | ?> |
| 174 | 178 | <?php else : ?> |
| 175 | 179 | <?php |
| 176 | 180 | $quantity = esc_attr( number_format_i18n( $minimum ) . ' - ' . number_format_i18n( array_keys( $price_rules )[0] - 1 ) . ' ' ); |
| @@ -180,9 +184,9 @@ | ||
| 180 | 184 | |
| 181 | 185 | <?php if ( $discountAmount > 0 ) : ?> |
| 182 | 186 | <?php |
| 183 | 187 | echo wp_kses_post( tptParseOptionText( $settings['options_option_text'], $quantity, |
| 184 | - $discountAmount, $baseUnitName ) ); | |
| 188 | + round( $discountAmount, 2 ), $baseUnitName ) ); | |
| 185 | 189 | ?> |
| 186 | 190 | <?php else : ?> |
| 187 | 191 | <?php |
| 188 | 192 | echo wp_kses_post( tptParseOptionText( $settings['options_default_option_text'], |
| @@ -223,11 +227,12 @@ | ||
| 223 | 227 | </div> |
| 224 | 228 | </div> |
| 225 | 229 | </div> |
| 226 | 230 | </li> |
| 231 | + <?php $tptTierRows[] = ob_get_clean(); ?> | |
| 227 | 232 | <?php $iterator = new ArrayIterator( $price_rules ); ?> |
| 228 | 233 | |
| 229 | - <?php while ( $iterator->valid() ) : ?> | |
| 234 | + <?php while ( $iterator->valid() ) : ob_start(); ?> | |
| 230 | 235 | <?php |
| 231 | 236 | $currentPrice = $iterator->current(); |
| 232 | 237 | $currentQuantity = $iterator->key(); |
| 233 | 238 | |
| @@ -240,17 +245,12 @@ | ||
| 240 | 245 | |
| 241 | 246 | $iterator->next(); |
| 242 | 247 | |
| 243 | 248 | if ( $iterator->valid() ) { |
| 244 | - $quantity = $currentQuantity; | |
| 245 | - | |
| 246 | - if ( intval( $iterator->key() - 1 != $currentQuantity ) ) { | |
| 247 | - | |
| 248 | - $quantity = number_format_i18n( $quantity ); | |
| 249 | - | |
| 250 | - if ( 'range' === $settings['quantity_type'] ) { | |
| 251 | - $quantity .= ' - ' . number_format_i18n( intval( $iterator->key() - 1 ) ); | |
| 252 | - } | |
| 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 ) ); | |
| 253 | 253 | } |
| 254 | 254 | } else { |
| 255 | 255 | $quantity = number_format_i18n( $currentQuantity ); |
| 256 | 256 | |
| @@ -273,8 +273,10 @@ | ||
| 273 | 273 | ) ); |
| 274 | 274 | ?> |
| 275 | 275 | |
| 276 | 276 | <li class="tiered-pricing-dropdown-option" |
| 277 | + role="option" | |
| 278 | + aria-selected="false" | |
| 277 | 279 | data-tiered-quantity="<?php echo esc_attr( $currentQuantity ); ?>" |
| 278 | 280 | data-tiered-price="<?php echo esc_attr( $currentProductPrice ); ?>" |
| 279 | 281 | data-tiered-price-exclude-taxes="<?php echo esc_attr( $currentProductPriceExcludeTaxes ); ?>" |
| 280 | 282 | data-tiered-price-include-taxes="<?php echo esc_attr( $currentProductPriceIncludeTaxes ); ?>"> |
| @@ -312,14 +314,15 @@ | ||
| 312 | 314 | </div> |
| 313 | 315 | </div> |
| 314 | 316 | </div> |
| 315 | 317 | </li> |
| 316 | - <?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 ?> | |
| 317 | 320 | <?php do_action( 'tiered_pricing_table/dropdown/options', $pricing_rule ); ?> |
| 318 | 321 | </ul> |
| 319 | - | |
| 320 | - <?php do_action( 'tiered_pricing_table/dropdown/after_options', $pricing_rule ); ?> | |
| 321 | 322 | </div> |
| 323 | + | |
| 324 | + <?php do_action( 'tiered_pricing_table/dropdown/after_options', $pricing_rule ); ?> | |
| 322 | 325 | </div> |
| 323 | 326 | </div> |
| 324 | 327 | |
| 325 | 328 | <style> |
| @@ -326,9 +329,10 @@ | ||
| 326 | 329 | <?php |
| 327 | 330 | $backgroundColor = Settings::hex2rgba($settings['active_tier_color'], 0.05); |
| 328 | 331 | |
| 329 | 332 | $clickableCSS = <<<EOD |
| 330 | - .tiered-pricing-dropdown__list .tiered-pricing-dropdown-option:hover { | |
| 333 | + .tiered-pricing-dropdown__list .tiered-pricing-dropdown-option:hover, | |
| 334 | + .tiered-pricing-dropdown__list .tiered-pricing-dropdown-option:focus { | |
| 331 | 335 | background: $backgroundColor; |
| 332 | 336 | cursor: pointer; |
| 333 | 337 | } |
| 334 | 338 | EOD; |