WP Manifestindependent plugin directory
manifest / ecommerce / smart-discount-manager

WooCommerce Smart Discount Manager

A visual rule-based discount manager for WooCommerce with flexible conditions and actions.

★ 0stars

Install

Download source zip No release zip yet

This is the repository archive. It installs, but the folder name will carry the branch suffix and updates will not flow.

Install straight from GitHub with WP-CLI:

wp plugin install https://github.com/kamal-core/smart-discount-manager/archive/refs/heads/main.zip
Updates itself. It ships its own updater (Plugin Update Checker), so new versions appear under Dashboard → Updates.

A production-quality WordPress plugin for managing flexible, rule-based discounts in WooCommerce.

Features

Phase 1 Implementation

1. Plugin Activation

  • Automatic activation check
  • WooCommerce dependency verification
  • Database table creation on activation
  • Admin notice if WooCommerce is not active

2. Admin Menu

Located under WooCommerce menu:

  • Dashboard
  • Discount Rules
  • Add New Rule
  • Settings

3. Dashboard

Quick overview showing:

  • Total Rules count
  • Active Rules count
  • Inactive Rules count
  • Scheduled Rules count
  • Quick action buttons
  • Getting started guide
  • Recent rules list

4. Discount Rules Management

Table view with columns:

  • Rule Name
  • Discount Type
  • Discount Value
  • Priority
  • Status
  • Date Range
  • Actions (Edit, Duplicate, Enable/Disable, Delete)

5. Visual Rule Builder

Easy-to-use interface for creating discount rules:

  • Rule name and description
  • Dynamic condition builder
  • Discount type selection (percentage/fixed)
  • Discount value input
  • Priority configuration
  • Scheduling (start/end dates)
  • Status control

6. Conditions System

Fully implemented condition types:

  • Product: Is / Is Not
  • Product Category: Is / Is Not
  • Product Tag: Is / Is Not
  • Quantity: Greater than, Greater than or equal, Less than, Less than or equal, Equal
  • Cart Subtotal: Greater than, Greater than or equal, Less than, Less than or equal, Equal
  • Customer Role: Is / Is Not
  • Logged-in Status: Is (logged in/logged out)

AND Logic: All conditions must match for the rule to apply

7. Discount Types

  • Percentage Discount: Apply percentage off (e.g., 20%)
  • Fixed Amount Discount: Apply fixed currency amount (e.g., $10)

8. Rule Priority System

  • Configurable priority number (lower = higher priority)
  • Used in conflict resolution strategy

9. Rule Scheduling

  • Optional start date
  • Optional end date
  • WordPress timezone aware
  • Rules outside scheduled dates don't apply

10. Conflict Resolution Settings

Three strategies for handling multiple matching rules:

  • Highest Discount: Apply the rule giving the best discount
  • Highest Priority: Apply the rule with lowest priority number
  • First Matching Rule: Apply the first matching rule

11. WooCommerce Integration

  • Hooks into cart item pricing
  • Applies discounts to cart totals
  • Works with checkout
  • No recursive calculations
  • Preserves WooCommerce pricing when no rules match

12. Database Architecture

Custom table structure:

  • wp_sdm_discount_rules table with fields:
    • id (primary key)
    • name
    • description
    • conditions (JSON)
    • discount_type
    • discount_value
    • priority
    • status
    • start_date
    • end_date
    • created_at
    • updated_at

Uses WordPress dbDelta() for safe schema updates.

13. Security

  • Capability checks (manage_woocommerce)
  • Nonce verification
  • Input sanitization
  • Output escaping
  • Prepared SQL queries
  • Data validation

14. Data Management

  • Uninstall hook respects user preference
  • Option to delete data on uninstall
  • Data preserved during deactivation

File Structure

smart-discount-manager/
│
├── smart-discount-manager.php      # Main plugin file
├── uninstall.php                   # Uninstall handler
│
├── includes/
│   ├── class-autoloader.php        # PSR-4 autoloader
│   ├── class-plugin.php            # Main orchestrator
│   ├── class-database.php          # Database operations
│   ├── class-rule-manager.php      # Rule CRUD and logic
│   ├── class-condition-manager.php # Condition evaluation
│   ├── class-discount-engine.php   # Discount calculations
│   └── class-woocommerce-integration.php # WC integration
│
├── admin/
│   ├── class-admin.php             # Admin handler
│   │
│   ├── views/
│   │   ├── dashboard.php           # Dashboard page
│   │   ├── rules.php               # Rules list page
│   │   ├── rule-editor.php         # Rule editor page
│   │   └── settings.php            # Settings page
│   │
│   ├── css/
│   │   └── admin.css               # Admin styles
│   │
│   └── js/
│       └── admin.js                # Admin interactivity
│
├── i18n/
│   └── languages/                  # Translation files
│
└── assets/
    ├── css/
    ├── images/
    └── js/

Usage Instructions

Creating a Discount Rule

  1. Go to WordPress Dashboard
  2. Navigate to WooCommerce → Smart Discounts
  3. Click "Add New Discount"
  4. Fill in the rule details:
    • Rule Name: Give it a descriptive name
    • Description: Optional note about the rule
  5. Add conditions:
    • Click "Add Condition"
    • Select condition type
    • Select operator
    • Enter value
    • Repeat for multiple conditions (all must match)
  6. Configure discount:
    • Select discount type (percentage or fixed)
    • Enter discount value
    • Set priority
  7. Optional scheduling:
    • Set start date if rule should activate in the future
    • Set end date if rule should expire
  8. Set status (Active/Inactive)
  9. Click "Create Rule" or "Update Rule"

Managing Rules

  • View All: Go to Discount Rules to see all rules
  • Edit: Click rule name to edit
  • Duplicate: Clone a rule to quickly create similar rules
  • Enable/Disable: Toggle rule status with one click
  • Delete: Remove a rule (confirm required)

Settings

Access from WooCommerce → Smart Discounts → Settings

  • Discount Conflict Resolution: Choose strategy when multiple rules match
  • Data Management: Choose whether to delete data on uninstall

Database Schema

wp_sdm_discount_rules Table

CREATE TABLE wp_sdm_discount_rules (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  description LONGTEXT,
  conditions LONGTEXT NOT NULL COMMENT 'JSON encoded conditions',
  discount_type VARCHAR(50) NOT NULL,
  discount_value DECIMAL(10, 2) NOT NULL,
  priority INT NOT NULL DEFAULT 10,
  status VARCHAR(20) NOT NULL DEFAULT 'active',
  start_date DATETIME,
  end_date DATETIME,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  INDEX idx_status (status),
  INDEX idx_priority (priority),
  INDEX idx_dates (start_date, end_date)
);

Testing Guide

Test 1: Basic Percentage Discount

  1. Create a product (Price: $100)
  2. Create rule:
    • Condition: Product = Test Product
    • Discount: 20% Percentage
  3. Expected: Cart shows $80

Test 2: Fixed Amount Discount

  1. Create a product (Price: $100)
  2. Create rule:
    • Condition: Product = Test Product
    • Discount: $15 Fixed
  3. Expected: Cart shows $85

Test 3: Category Discount

  1. Create a product in "Shoes" category (Price: $100)
  2. Create rule:
    • Condition: Category = Shoes
    • Discount: 20% Percentage
  3. Expected: Product shows discount

Test 4: Category Exclusion

  1. Create a product NOT in "Shoes" category
  2. Use the rule from Test 3
  3. Expected: No discount applied

Test 5: Quantity Discount

  1. Add 5+ items to cart
  2. Create rule:
    • Condition: Quantity >= 5
    • Discount: 10% Percentage
  3. Expected: Discount applies when quantity >= 5

Test 6: Inactive Rule

  1. Create a rule and set to Inactive
  2. Expected: Discount does not apply

Test 7: Scheduled Rule

  1. Create rule with start date in future
  2. Expected: Discount does not apply until start date
  3. Create rule with end date in past
  4. Expected: Discount does not apply

Test 8: Conflict Resolution

  1. Create two rules matching same product:
    • Rule A: 20% discount, Priority 5
    • Rule B: 10% discount, Priority 10
  2. Test each strategy:
    • Highest Discount: 20% applies
    • Highest Priority: Rule A (priority 5) applies
    • First Matching: Depends on order

Architecture Highlights

Modular Design

  • Separated concerns (Database, Rules, Conditions, Discounts, WooCommerce)
  • Easy to extend with new condition types
  • Easy to add new discount types

Security First

  • WordPress nonces on all AJAX
  • Capability checks on all admin functions
  • Sanitized input, escaped output
  • Prepared SQL statements

Performance Optimized

  • Discount cache to avoid recalculation
  • Indexed database queries
  • Efficient condition evaluation

Extensibility

Ready for Phase 2 features:

  • Condition manager supports adding new types
  • Discount engine designed for complex calculations
  • Database structure supports additional fields

Known Limitations

  1. No Discount Stacking: Only one discount per rule applies
  2. No BOGO: Buy X Get Y not implemented
  3. No Bundle Discounts: Multi-product discounts not in Phase 1
  4. No Customer Loyalty: Loyalty-based discounts not implemented
  5. No Shipping Discounts: Cart subtotal doesn't include shipping
  6. No Coupon Integration: Works independently of WooCommerce coupons
  7. Percentage Comparison: When comparing discounts for "highest discount", percentage and fixed amounts aren't converted

Phase 2 Recommendations

  1. Buy X Get Y Discounts

    • Add BOGO rule type
    • Condition: Product quantity threshold
    • Action: Free/discounted secondary product
  2. Tiered Pricing

    • Multiple discount brackets by quantity
    • Progressive discount increase
  3. Bundle Discounts

    • Multi-product conditions
    • Discount on bundle total
  4. Customer Loyalty

    • Loyalty tier conditions
    • Loyalty points-based discounts
  5. VIP/Membership Pricing

    • Membership level conditions
    • Exclusive pricing
  6. Discount Stacking

    • Allow multiple rules to apply
    • Cumulative or combined discount calculation
  7. Advanced Analytics

    • Track discount usage
    • Revenue impact analysis
    • Discount effectiveness reports
  8. Import/Export

    • Backup and restore rules
    • Share rules between stores

Code Standards

  • Follows WordPress coding standards
  • Uses PSR-4 autoloading with namespace
  • Consistent naming conventions
  • Comprehensive comments for complex logic
  • No external dependencies

Support

For issues or questions, refer to the code documentation and inline comments throughout the plugin files.