- How do I install and set up Discountify in a Laravel project?
- Run `composer require safemood/discountify` to install the package, then publish the config file with `php artisan vendor:publish --tag=discountify-config`. The package requires no additional setup for basic usage, but you can customize field mappings (e.g., `price`, `quantity`) and global discount/tax rates in `config/discountify.php`.
- What Laravel versions does Discountify support?
- Discountify is designed for Laravel 8.x and 9.x. Check the package’s [Packagist page](https://packagist.org/packages/safemood/discountify) for the latest version compatibility. Always verify your Laravel version matches the package’s requirements before installation.
- Can I define custom discount conditions beyond percentage-based rules?
- Yes, Discountify supports both closure-based and class-based conditions. For example, you can create a `MinimumCartValueCondition` class or use anonymous functions to evaluate conditions like user roles, cart size, or temporal rules (e.g., discounts active only on weekends).
- How does Discountify handle conflicting discounts (e.g., two coupons applying to the same cart)?
- Discountify applies discounts in the order they are defined, with later discounts potentially overriding earlier ones. There’s no built-in priority system for competing discounts, so explicitly order your conditions or use coupon-specific logic (e.g., single-use coupons) to avoid conflicts.
- Is Discountify suitable for high-traffic e-commerce sites with millions of users?
- The package uses a JSON file for coupon storage by default, which isn’t scalable for high-volume systems. For production use, migrate coupons to a database (e.g., MySQL) and cache coupon data in Redis to reduce latency. Also, optimize condition logic to avoid CPU-intensive evaluations during peak traffic.
- Can I use Discountify for fixed-amount discounts (e.g., $5 off) instead of just percentages?
- Discountify currently supports percentage-based discounts only. For fixed-amount discounts, you’d need to extend the package or pre-calculate percentage equivalents (e.g., $5 off a $100 item = 5% discount). Consider submitting a feature request or contributing a pull request for native fixed-amount support.
- How do I log discount applications for compliance or analytics?
- Discountify fires events like `DiscountAppliedEvent` and `CouponAppliedEvent` when discounts are applied. Listen to these events in your `EventServiceProvider` to log details (e.g., discount type, amount, user ID) to a database or external service. For audit trails, pair events with Laravel’s logging or a dedicated analytics package.
- Does Discountify support multi-tenancy (e.g., SaaS applications with tenant-specific discounts)?
- The current design assumes a single-tenant environment. For multi-tenancy, you’ll need to extend the package to scope discounts by tenant ID (e.g., via middleware or a custom condition class). Override the `setItems()` method or use Laravel’s context binding to inject tenant-specific data.
- How can I skip evaluating certain discount conditions for specific carts?
- Use the `skipConditions()` method to exclude specific condition classes or IDs during calculation. For example, `Discountify::setItems($cart)->skipConditions(['MinimumCartValueCondition'])->calculateTotal()`. This is useful for bypassing conditions like minimum order values for VIP users.
- Are there performance concerns with Discountify in production, and how can I mitigate them?
- Discountify evaluates conditions sequentially for each cart update, which can become slow with complex or numerous conditions. Mitigate this by caching condition results (e.g., Redis) for static rules, optimizing closures, and using database indexes for coupon lookups. For high concurrency, wrap usage in a service class to avoid static method race conditions.