Skip to content

Instantly share code, notes, and snippets.

@Acephalia
Last active January 7, 2025 09:50
Show Gist options
  • Save Acephalia/5c714ce558d297d14d29825e2f92f5b3 to your computer and use it in GitHub Desktop.
Save Acephalia/5c714ce558d297d14d29825e2f92f5b3 to your computer and use it in GitHub Desktop.
WooCommerce Dynamic Shop Wide Global Discount
/*
* @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>';
});
@Acephalia
Copy link
Author

Acephalia commented Jan 6, 2025

This code

  • Creates a global discount system that applies the specified percentage off to all products in your store
  • Modifies products' sale price and regular price to show the discount both on product pages and in the cart
  • Automatically applies the discount during cart calculations
  • Shows amount saved in cart
  • Includes Admin notice in the WordPress dashboard reminding admins that the discount is active

Support: https://buymeacoffee.com/acephaliax

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment