badge.php
35 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Modules\Badge; |
| 4 | |
| 5 | use ShopEngine\Traits\Singleton; |
| 6 | |
| 7 | class Badge |
| 8 | { |
| 9 | use Singleton; |
| 10 | |
| 11 | public function init() { |
| 12 | |
| 13 | add_filter('woocommerce_format_sale_price', [$this, 'format_price_badge'], 9999, 3); |
| 14 | |
| 15 | add_action('wp_enqueue_scripts', [$this, 'enqueue']); |
| 16 | } |
| 17 | |
| 18 | public function enqueue() { |
| 19 | |
| 20 | wp_enqueue_style('lib-badge', plugin_dir_url(__FILE__) . '/assets/css/badge.css'); |
| 21 | } |
| 22 | |
| 23 | public function format_price_badge($price, $regular_price, $sale_price) { |
| 24 | |
| 25 | $pct = ($regular_price - $sale_price) * 100 / $regular_price; |
| 26 | $pct = ceil($pct); |
| 27 | |
| 28 | $price = '<del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del> '. |
| 29 | '<ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price ) . '</ins>'. |
| 30 | '<span class="shopengine-badge shopengine-discount-badge">' . $pct . '% '.__('OFF', 'shopengine').'</span>'; |
| 31 | |
| 32 | return $price; |
| 33 | } |
| 34 | } |
| 35 |