Install
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.zipA 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_rulestable 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
- Go to WordPress Dashboard
- Navigate to WooCommerce → Smart Discounts
- Click "Add New Discount"
- Fill in the rule details:
- Rule Name: Give it a descriptive name
- Description: Optional note about the rule
- Add conditions:
- Click "Add Condition"
- Select condition type
- Select operator
- Enter value
- Repeat for multiple conditions (all must match)
- Configure discount:
- Select discount type (percentage or fixed)
- Enter discount value
- Set priority
- Optional scheduling:
- Set start date if rule should activate in the future
- Set end date if rule should expire
- Set status (Active/Inactive)
- 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
- Create a product (Price: $100)
- Create rule:
- Condition: Product = Test Product
- Discount: 20% Percentage
- Expected: Cart shows $80
Test 2: Fixed Amount Discount
- Create a product (Price: $100)
- Create rule:
- Condition: Product = Test Product
- Discount: $15 Fixed
- Expected: Cart shows $85
Test 3: Category Discount
- Create a product in "Shoes" category (Price: $100)
- Create rule:
- Condition: Category = Shoes
- Discount: 20% Percentage
- Expected: Product shows discount
Test 4: Category Exclusion
- Create a product NOT in "Shoes" category
- Use the rule from Test 3
- Expected: No discount applied
Test 5: Quantity Discount
- Add 5+ items to cart
- Create rule:
- Condition: Quantity >= 5
- Discount: 10% Percentage
- Expected: Discount applies when quantity >= 5
Test 6: Inactive Rule
- Create a rule and set to Inactive
- Expected: Discount does not apply
Test 7: Scheduled Rule
- Create rule with start date in future
- Expected: Discount does not apply until start date
- Create rule with end date in past
- Expected: Discount does not apply
Test 8: Conflict Resolution
- Create two rules matching same product:
- Rule A: 20% discount, Priority 5
- Rule B: 10% discount, Priority 10
- 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
- No Discount Stacking: Only one discount per rule applies
- No BOGO: Buy X Get Y not implemented
- No Bundle Discounts: Multi-product discounts not in Phase 1
- No Customer Loyalty: Loyalty-based discounts not implemented
- No Shipping Discounts: Cart subtotal doesn't include shipping
- No Coupon Integration: Works independently of WooCommerce coupons
- Percentage Comparison: When comparing discounts for "highest discount", percentage and fixed amounts aren't converted
Phase 2 Recommendations
-
Buy X Get Y Discounts
- Add BOGO rule type
- Condition: Product quantity threshold
- Action: Free/discounted secondary product
-
Tiered Pricing
- Multiple discount brackets by quantity
- Progressive discount increase
-
Bundle Discounts
- Multi-product conditions
- Discount on bundle total
-
Customer Loyalty
- Loyalty tier conditions
- Loyalty points-based discounts
-
VIP/Membership Pricing
- Membership level conditions
- Exclusive pricing
-
Discount Stacking
- Allow multiple rules to apply
- Cumulative or combined discount calculation
-
Advanced Analytics
- Track discount usage
- Revenue impact analysis
- Discount effectiveness reports
-
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.