Provide a way to check if a coupon is valid for a customer/user. #2399
Unanswered
simeon-smith
asked this question in
Ideas
Replies: 1 comment
-
I was able to create a module that does this using a Twig function. This is what that twig extension looks like. @lukeholder <?php
/**
* Is Coupon Valid module for Craft CMS 3.x
*
* Checks if a commerce coupon is valid. Can be used to check if the coupon is valid for a specific customer or email.
*
* @link https://uxiliary.com
* @copyright Copyright (c) 2020 Simeon Smith
*/
namespace modules\iscouponvalidmodule\twigextensions;
use modules\iscouponvalidmodule\IsCouponValidModule;
use craft\commerce\records\CustomerDiscountUse as CustomerDiscountUseRecord;
use craft\commerce\records\EmailDiscountUse as EmailDiscountUseRecord;
use craft\commerce\Plugin as Commerce;
use Craft;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
/**
* Twig can be extended in many ways; you can add extra tags, filters, tests, operators,
* global variables, and functions. You can even extend the parser itself with
* node visitors.
*
* http://twig.sensiolabs.org/doc/advanced.html
*
* @author Simeon Smith
* @package IsCouponValidModule
* @since 1.0.0
*/
class IsCouponValidModuleTwigExtension extends AbstractExtension
{
// Public Methods
// =========================================================================
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'IsCouponValidModule';
}
/**
* Returns an array of Twig filters, used in Twig templates via:
*
* {{ 'something' | someFilter }}
*
* @return array
*/
public function getFilters()
{
return [];
}
/**
* Returns an array of Twig functions, used in Twig templates via:
*
* {% set this = someFunction('something') %}
*
* @return array
*/
public function getFunctions()
{
return [
new TwigFunction('isDiscountValid', [$this, 'isDiscountValid']),
];
}
/**
* Our function called via Twig; it can do anything you want
*
* @param null $text
*
* @return string
*/
public function isDiscountValid($discountId, $customerId = null)
{
if (is_null($customerId)) {
$customer = Commerce::getInstance()->getCustomers()->getCustomer();
$customerId = $customer->id;
$email = $customer->getEmail();
} else {
$customer = Commerce::getInstance()->getCustomers()->getCustomerById($customerId);
$email = $customer->getEmail();
}
$discount = Commerce::getInstance()->getDiscounts()->getDiscountById($discountId);
$discountValid = true;
if ($discount->perUserLimit) {
$customerDiscountUseRecord = CustomerDiscountUseRecord::find()->where(['[[customerId]]' => $customer->id, '[[discountId]]' => $discount->id])->select(["discountId", "customerId", "uses"])->asArray()->all();
if (!is_null($customerDiscountUseRecord) && count($customerDiscountUseRecord) > 0) {
foreach ($customerDiscountUseRecord as $record) {
if ($record["uses"] >= $discount->perUserLimit) {
$discountValid = false;
}
}
}
}
if ($discount->perEmailLimit) {
$emailDiscountUseRecord = EmailDiscountUseRecord::find()->where(['[[email]]' => $email, '[[discountId]]' => $discount->id])->select(["discountId", "email", "uses"])->asArray()->all();
if (!is_null($emailDiscountUseRecord) && count($emailDiscountUseRecord) > 0) {
foreach ($emailDiscountUseRecord as $record) {
if ($record["uses"] >= $discount->perEmailLimit) {
$discountValid = false;
}
}
}
}
return $discountValid;
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
What are you trying to do?
I need to check if a coupon code is valid for a customer/user.
What's your proposed solution?
Provide a twig method either on the customer/user to check a coupon codes validity or provide a way on a coupon to pass a customer/users id or email to check if the coupon is valid for that.
Beta Was this translation helpful? Give feedback.
All reactions