Auto Discounts for WooCommerce - Automatic Discounts Plugin
Key Results
Challenges
- Managing discounts based on product age
- Performance with large databases
- Intuitive admin interface
- Compatibility with other WooCommerce plugins
Solutions
- Flexible and extensible rule system
- SQL query optimization
- React interface for administration
- WordPress hooks for seamless integration
Auto Discounts for WooCommerce - Automatic Discounts Plugin
Project Context
Auto Discounts for WooCommerce is a WordPress plugin I developed to address a recurring need in e-commerce stores: automatically applying discounts based on product age. This open-source plugin enables merchants to easily manage automatic promotions without manual intervention.
The Problem to Solve
Many e-merchants want to:
- Apply progressive discounts on products that have been in stock for a long time
- Automatically manage promotions without manual intervention
- Exclude certain products or categories from discounts
- Preview discounts before applying them
- Schedule discounts via cron tasks
Plugin Architecture
Technical Structure
The plugin is developed according to WordPress and WooCommerce standards:
- PHP 8.4 with strict typing
- WordPress Coding Standards: Respect for WordPress conventions
- WooCommerce Hooks: Native integration with WooCommerce
- REST API: Endpoints for administration
- JavaScript (ES6+): Modern administration interface
Flexible Rule System
The core of the plugin relies on an extensible rule system:
class Discount_Rule {
private $age_threshold; // Minimum age in days
private $discount_percentage; // Discount percentage
private $excluded_products; // Excluded products
private $excluded_categories; // Excluded categories
private $max_discount; // Maximum discount
public function apply($product_id) {
// Discount application logic
}
}
Performance Optimization
With catalogs of several thousand products, performance is crucial:
Optimized SQL Queries
// Optimized query to calculate product age
$query = "
SELECT p.ID,
DATEDIFF(NOW(), p.post_date) as age_days,
pm.meta_value as price
FROM {$wpdb->posts} p
LEFT JOIN {$wpdb->postmeta} pm
ON p.ID = pm.post_id AND pm.meta_key = '_price'
WHERE p.post_type = 'product'
AND p.post_status = 'publish'
AND DATEDIFF(NOW(), p.post_date) >= %d
ORDER BY p.post_date ASC
";
This approach enables:
- Reducing the number of SQL queries
- Using indexes to speed up searches
- Calculating discounts in a single pass
Intelligent Caching
Calculation results are cached to avoid unnecessary recalculations:
- Cache of eligible products
- Automatic invalidation on updates
- WordPress transient cache for performance
Main Features
Flexible Configuration
The plugin offers great flexibility in configuration:
Discount Rules
- Minimum age: Define minimum age in days for a product to be eligible
- Progressive percentage: Discounts that increase with age
- Maximum discount: Limit the maximum discount percentage
- Exclusions: Exclude specific products or categories
Preview
Before applying discounts, administrators can:
- See a preview of products that will be affected
- Calculate the total discount amount
- Test different configurations
Cron Tasks
The plugin can be configured to:
- Automatically apply discounts at regular intervals
- Schedule discounts for specific dates
- Manage multiple rules with different frequencies
Administration Interface
The administration interface is developed with React for a modern experience:
- Intuitive dashboard: Overview of active rules
- Rule editor: Visual configuration of rules
- Statistics: Tracking of applied discounts
- Logs: History of actions performed
Compatibility
The plugin is designed to be compatible with:
- WooCommerce 3.0+: Compatible with all recent versions
- Price plugins: Compatible with third-party price management plugins
- Discount plugins: Intelligent conflict management
- Themes: Compatible with all WooCommerce themes
Optimizations and Best Practices
Security
- Nonce verification: Protection against CSRF attacks
- Capability checks: User permission verification
- Sanitization: Cleaning of all user inputs
- Escaping: Escaping of all outputs
Performance
- Lazy loading: On-demand data loading
- Batch processing: Batch processing for large databases
- Database indexing: SQL query optimization
- Object caching: WordPress cache usage
Maintainability
- Modular code: Architecture with reusable classes
- Documentation: Complete code documentation
- Tests: Unit tests for critical functions
- Versioning: Semantic version management
Results and Impact
Performance
- Calculation time: Less than 100ms for 1000 products
- Server impact: Minimal CPU consumption
- Memory: Optimized memory usage
- Compatibility: 100% compatible with major plugins
Adoption
- Active installations: 100 installations
- Average rating: 4.5/5 stars
- Contributions: Open-source project with community contributions
- Maintenance: Regular updates and active support
Business Impact
For e-merchants using the plugin:
- Stock rotation: Average 30% improvement
- Loss reduction: Decrease in obsolete products
- Automation: Significant time savings
- ROI: Positive return on investment from the first month
Open Source Development
Philosophy
The plugin is developed according to open source principles:
- Open code: Source code available on GitHub
- Documentation: Complete documentation and examples
- Contributions: Welcoming community contributions
- Support: Active community support
Ecosystem
The plugin is part of a larger ecosystem:
- Extensions: Ability to extend functionality
- Integrations: Compatible with other plugins
- API: REST API for third-party integrations
- Hooks: WordPress hooks for customizations
Conclusion
The Auto Discounts for WooCommerce plugin demonstrates how a well-designed open-source solution can address a real market need in e-commerce. The modular architecture, performance optimizations, and intuitive user interface make it a powerful and accessible tool.
This case study illustrates the importance of:
- Understanding real user needs
- Optimizing performance for large databases
- Creating intuitive interfaces even for complex features
- Maintaining clean and documented code to facilitate contributions
The plugin's success (100 active installations) shows that a user-centered approach and technical quality can create value for an entire community of e-merchants.