| @@ -110,8 +110,16 @@ | ||
| 110 | 110 | <?php endforeach; ?> |
| 111 | 111 | </div> |
| 112 | 112 | <?php endif; ?> |
| 113 | 113 | <?php |
| 114 | + /** | |
| 115 | + * After the pricing layout and its wrapper. The savings badge uses it for its "after the table" position. | |
| 116 | + * | |
| 117 | + * @param WC_Product $parentProduct | |
| 118 | + * @param int|null $variationID | |
| 119 | + * @param array $settings | |
| 120 | + */ | |
| 121 | + do_action( 'tiered_pricing_table/after_rendering_tiered_pricing', $parentProduct, $variationID, $settings ); | |
| 114 | 122 | } |
| 115 | 123 | |
| 116 | 124 | /** |
| 117 | 125 | * Render pricing table without wrapper |
| @@ -268,8 +276,31 @@ | ||
| 268 | 276 | * @param string $style 'plain' ("10%", "$4.50", "10% ($4.50)") or 'off' ("10% off", "$4.50 off", "10% off, save $4.50"). |
| 269 | 277 | * |
| 270 | 278 | * @return string HTML |
| 271 | 279 | */ |
| 280 | + /** | |
| 281 | + * Decimals shown on a percentage discount ("10.14%" with 2, "10%" with 0). | |
| 282 | + * | |
| 283 | + * @param float $percent The discount percentage. | |
| 284 | + * @param WC_Product|null $product | |
| 285 | + * @param PricingRule|null $pricingRule | |
| 286 | + * | |
| 287 | + * @return int | |
| 288 | + */ | |
| 289 | + public static function discountDecimals( float $percent, ?WC_Product $product = null, ?PricingRule $pricingRule = null ): int { | |
| 290 | + /** | |
| 291 | + * Filter the number of decimals of displayed percentage discounts. | |
| 292 | + * | |
| 293 | + * @param int $decimals Default 2. Return 0 to show whole numbers. | |
| 294 | + * @param float $percent The discount percentage before rounding. | |
| 295 | + * @param WC_Product|null $product | |
| 296 | + * @param PricingRule|null $pricingRule | |
| 297 | + */ | |
| 298 | + $decimals = apply_filters( 'tiered_pricing_table/discount/decimals', 2, $percent, $product, $pricingRule ); | |
| 299 | + | |
| 300 | + return max( 0, (int) $decimals ); | |
| 301 | + } | |
| 302 | + | |
| 272 | 303 | public static function formatDiscount( |
| 273 | 304 | float $percent, |
| 274 | 305 | PricingRule $pricingRule, |
| 275 | 306 | WC_Product $product, |
| @@ -279,9 +310,9 @@ | ||
| 279 | 310 | ): string { |
| 280 | 311 | $format = $settings['discount_format'] ?? 'percentage'; |
| 281 | 312 | $format = in_array( $format, array( 'percentage', 'amount', 'both' ), true ) ? $format : 'percentage'; |
| 282 | 313 | |
| 283 | - $percentLabel = round( $percent, 2 ) . '%'; | |
| 314 | + $percentLabel = round( $percent, self::discountDecimals( $percent, $product, $pricingRule ) ) . '%'; | |
| 284 | 315 | $amountLabel = ''; |
| 285 | 316 | |
| 286 | 317 | if ( 'percentage' !== $format ) { |
| 287 | 318 | $saving = 0; |