This simple snippet automatically applies a 10% discount to all products from a specific category when they’re added to the cart. No coupon codes needed – the discount is applied automatically and customers can see it clearly.
The snippet monitors the shopping cart and automatically reduces prices by 10% for any products that belong to category ID 33. The discount is visible in the cart, on product pages, and as badges in the shop.
/**
* Automatic 10% discount for products from specific category
* Apply discount automatically when products added to cart
* Prefix: tpsc_ (TP Snippet Collection)
*/
// ============================================
// CONFIGURATION
// ============================================
// Set your category IDs here (can add multiple categories)
define('TPSC_DISCOUNT_CATEGORIES', array(33, 45, 67, 89));
// Set discount percentage (10 = 10%)
define('TPSC_DISCOUNT_PERCENTAGE', 10);
// Set discount name (appears in cart)
define('TPSC_DISCOUNT_NAME', 'Category Discount (10%)');
// ============================================
// APPLY AUTOMATIC DISCOUNT
// ============================================
// Apply discount when cart is updated
add_action('woocommerce_before_calculate_totals', 'tpsc_apply_category_discount');
function tpsc_apply_category_discount($cart) {
// Exit if we're in admin or during AJAX requests (except cart updates)
if (is_admin() && !defined('DOING_AJAX')) {
return;
}
// Avoid infinite loops during calculations
if (did_action('woocommerce_before_calculate_totals') >= 2) {
return;
}
// Check if cart is empty
if ($cart->is_empty()) {
return;
}
// Loop through cart items
foreach ($cart->get_cart() as $cart_item) {
$product = $cart_item['data'];
$product_id = $cart_item['product_id'];
// Check if product is in the discount category
if (tpsc_product_in_discount_category($product_id)) {
// Get original price
$original_price = $product->get_regular_price();
if (empty($original_price)) {
$original_price = $product->get_price();
}
// Calculate discounted price
$discount_amount = ($original_price * TPSC_DISCOUNT_PERCENTAGE) / 100;
$new_price = $original_price - $discount_amount;
// Set new price
$product->set_price($new_price);
}
}
}
// ============================================
// HELPER FUNCTIONS
// ============================================
// Check if product belongs to discount category
function tpsc_product_in_discount_category($product_id) {
$product_categories = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'ids'));
if (is_wp_error($product_categories) || empty($product_categories)) {
return false;
}
// Check if product is in any of the discount categories
$discount_categories = TPSC_DISCOUNT_CATEGORIES;
return array_intersect($discount_categories, $product_categories);
}
// ============================================
// DISPLAY DISCOUNT INFO
// ============================================
// Add discount notice in cart
add_action('woocommerce_before_cart_table', 'tpsc_display_cart_discount_notice');
function tpsc_display_cart_discount_notice() {
$has_discount_products = false;
// Check if cart has products from discount category
foreach (WC()->cart->get_cart() as $cart_item) {
if (tpsc_product_in_discount_category($cart_item['product_id'])) {
$has_discount_products = true;
break;
}
}
if ($has_discount_products) {
echo '<div class="woocommerce-info tpsc-discount-notice">';
echo '<strong>🎉 ' . TPSC_DISCOUNT_NAME . ' Applied!</strong><br>';
echo 'Selected products have automatically received a ' . TPSC_DISCOUNT_PERCENTAGE . '% discount.';
echo '</div>';
}
}
// ============================================
// PRODUCT PAGE DISCOUNT INFO
// ============================================
// Show discount info on product page
add_action('woocommerce_single_product_summary', 'tpsc_display_product_discount_info', 25);
function tpsc_display_product_discount_info() {
global $product;
if (!$product) {
return;
}
// Check if current product is in discount category
if (tpsc_product_in_discount_category($product->get_id())) {
echo '<div class="tpsc-product-discount-info">';
echo '<p style="color: #27ae60; font-weight: bold; margin: 10px 0;">';
echo '🎉 This product gets an automatic ' . TPSC_DISCOUNT_PERCENTAGE . '% discount in cart!';
echo '</p>';
echo '</div>';
}
}
// ============================================
// SHOP PAGE DISCOUNT BADGES
// ============================================
// Add discount badge to products in shop/category pages
add_action('woocommerce_before_shop_loop_item_title', 'tpsc_add_discount_badge', 15);
function tpsc_add_discount_badge() {
global $product;
if (!$product) {
return;
}
// Check if product is in discount category
if (tpsc_product_in_discount_category($product->get_id())) {
echo '<div class="tpsc-discount-badge">';
echo TPSC_DISCOUNT_PERCENTAGE . '% OFF';
echo '</div>';
}
}
// ============================================
// STYLING
// ============================================
// Add CSS for discount elements
add_action('wp_head', 'tpsc_discount_styles');
function tpsc_discount_styles() {
echo '<style>
.tpsc-discount-notice {
background-color: #d4edda !important;
border-color: #c3e6cb !important;
color: #155724 !important;
padding: 15px !important;
border-radius: 5px !important;
margin-bottom: 20px !important;
}
.tpsc-product-discount-info {
background-color: #f8f9fa;
border: 2px dashed #27ae60;
border-radius: 5px;
padding: 15px;
margin: 15px 0;
text-align: center;
}
.tpsc-discount-badge {
position: absolute;
top: 10px;
left: 10px;
background-color: #e74c3c;
color: white;
padding: 5px 10px;
font-size: 12px;
font-weight: bold;
border-radius: 3px;
z-index: 1;
text-transform: uppercase;
}
.woocommerce ul.products li.product {
position: relative;
}
/* Mobile responsive */
@media (max-width: 768px) {
.tpsc-discount-badge {
font-size: 10px;
padding: 3px 6px;
}
}
</style>';
}
// ============================================
// ADMIN TOOLS
// ============================================
// Add admin notice to show current settings
add_action('admin_notices', 'tpsc_admin_discount_notice');
function tpsc_admin_discount_notice() {
$screen = get_current_screen();
if ($screen && ($screen->id === 'edit-product' || $screen->id === 'product')) {
$category_names = array();
foreach (TPSC_DISCOUNT_CATEGORIES as $cat_id) {
$category = get_term($cat_id, 'product_cat');
$category_names[] = $category ? $category->name : 'ID ' . $cat_id;
}
echo '<div class="notice notice-info">';
echo '<p><strong>Category Discount Active:</strong> ';
echo TPSC_DISCOUNT_PERCENTAGE . '% automatic discount for products in: ' . implode(', ', $category_names) . '</p>';
echo '</div>';
}
}
Something not working as expected? Need help customizing it for your specific needs?
Have an idea for functionality you're missing on your site? Tell us what you're looking for!
Share it with us! Our community loves learning and growing together.
No comments yet.