When allowing your customers to apply discount coupons to their cart it's important to validate the given coupon code to make sure it's valid and applicable. Shopr comes with a set of default rules to make sure the most common requirements are met:
'discount_coupons' => [
'validation_rules' => [
Happypixels\Shopr\Rules\Cart\CartNotEmpty::class,
Happypixels\Shopr\Rules\Discounts\OnlyOneCouponPerOrder::class,
Happypixels\Shopr\Rules\Discounts\CouponHasNotBeenApplied::class,
Happypixels\Shopr\Rules\Discounts\CouponExists::class,
Happypixels\Shopr\Rules\Discounts\DateIsWithinCouponTimespan::class,
Happypixels\Shopr\Rules\Discounts\CartValueAboveCouponLimit::class
]
],
These rules are validated when a customer attempts to add a discount coupon to their cart. The default rules make sure the following statements are true:
To add your own rules to this process, create a new rule as described in the Laravel docs, then add it to the list in your configuration file. In your new Rule class, you'll have the following method:
public function passes($attribute, $value)
{
// Perform your validation
}
The $value
variable is the code the customer has entered.
You may wish to translate the messages returned when any of the default rules fail. First, publish the package translation files:
php artisan vendor:publish --provider="Happypixels\Shopr\ShoprServiceProvider" --tag="translations"
These will be located in resources/lang/vendor/shopr
. You can now add your own languages or customize the default (english) messages.