Last active
January 7, 2025 09:50
-
-
Save Acephalia/5c714ce558d297d14d29825e2f92f5b3 to your computer and use it in GitHub Desktop.
WooCommerce Dynamic Shop Wide Global Discount
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* @snippet WooCommerce Shope Wide Global Discount | |
* @author https://gist.github.com/Acephalia | |
* @caffeinate https://buymeacoffee.com/acephaliax | |
* @Usage Activate via code snippets and change discount amount at line 11. | |
*/ | |
class WC_Dynamic_Global_Discount { | |
// CHANGE DISCOUNT PERCENTAGE HERE | |
private static $discount_percentage = 10; // Set your desired discount percentage (e.g., 10 for 10%) | |
public static function init() { | |
add_action('woocommerce_before_calculate_totals', [__CLASS__, 'apply_dynamic_discount']); | |
add_filter('woocommerce_product_get_sale_price', [__CLASS__, 'modify_sale_price'], 10, 2); | |
add_filter('woocommerce_product_get_price', [__CLASS__, 'modify_price'], 10, 2); | |
} | |
//Calculate discounted price with proper type handling | |
private static function calculate_discount($price) { | |
if (!is_numeric($price)) { | |
return $price; | |
} | |
$price = floatval($price); | |
if ($price <= 0) { | |
return $price; | |
} | |
return $price * (1 - (self::$discount_percentage / 100)); | |
} | |
//Dynamically modify sale price for all products | |
public static function modify_sale_price($sale_price, $product) { | |
$regular_price = $product->get_regular_price(); | |
if (empty($regular_price)) { | |
return $sale_price; | |
} | |
return self::calculate_discount($regular_price); | |
} | |
//Dynamically modify price for all products | |
public static function modify_price($price, $product) { | |
$regular_price = $product->get_regular_price(); | |
if (empty($regular_price)) { | |
return $price; | |
} | |
return self::calculate_discount($regular_price); | |
} | |
//Apply dynamic discount during cart calculation | |
public static function apply_dynamic_discount($cart) { | |
if (is_admin() && !defined('DOING_AJAX')) return; | |
foreach ($cart->get_cart() as $cart_item_key => $cart_item) { | |
$product = $cart_item['data']; | |
$original_price = $product->get_regular_price(); | |
if (!empty($original_price)) { | |
$discounted_price = self::calculate_discount($original_price); | |
$cart_item['data']->set_price($discounted_price); | |
} | |
} | |
} | |
} | |
// Initialize the dynamic discount | |
add_action('init', ['WC_Dynamic_Global_Discount', 'init']); | |
//Add admin notice | |
add_action('admin_notices', function() { | |
$discount = WC_Dynamic_Global_Discount::$discount_percentage; | |
echo '<div class="notice notice-warning"> | |
<p>' . esc_html($discount) . '% Discount is currently active for all products</p> | |
</div>'; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code
Support: https://buymeacoffee.com/acephaliax