Onsale badge percentage compatibility for Satine Theme

Implement on-sale badge percentage compatibility for the Satine Theme.

add_action('plugins_loaded', function() {
    if(!function_exists('satine_elated_woocommerce_sale_percentage')) {
        /**
         * Function that sale badge percentage
         * Return string
         */
        function satine_elated_woocommerce_sale_percentage($price, $sale_price) {
            global $post, $product;
            if (is_object($product) && method_exists($product, 'get_type')) {
                $percentage = 0;
                if ($product->get_type() == 'simple' || $product->get_type() == 'external' || $product->get_type() == 'variation') {
                    $discount = apply_filters('advanced_woo_discount_rules_get_product_discount_price_from_custom_price', false, $product, 1, 0, 'all', true, false);
                    if ($discount !== false && is_array($discount) && isset($discount['initial_price']) && isset($discount['discounted_price'])) {
                        $percentage = round((($discount['initial_price'] - $discount['discounted_price']) / $discount['initial_price'])  * 100);
                    } elseif ($product->get_sale_price() != '') {
                        $percentage = round((($product->get_regular_price() - $product->get_sale_price()) / $product->get_regular_price()) * 100);
                    }
                }
                if ($percentage > 0) {
                    $badge = '-' . $percentage . '%';
                }else{
                    $badge = esc_html__('SALE', 'satine');
                }
                if (class_exists('\Wdr\App\Router')) {
                    $badge = sanitize_text_field(\Wdr\App\Router::$manage_discount->replaceSaleTagText($badge, $post, $product));
                }
                return $badge;
            } else {
                // default code
                if($price > 0) {
                    return '-' . (100 - round(($sale_price * 100) / $price)) . '%';
                }else{
                    return esc_html__('SALE', 'satine');
                }
            }
        }
    }
});

Last updated