50 lines
1.4 KiB
PHP
50 lines
1.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\CustomerCoupon\Plugin;
|
|
|
|
use Magento\Framework\App\ResourceConnection;
|
|
use Magento\SalesRule\Model\ValidateCouponCode;
|
|
|
|
class ValidateAssignedCouponPlugin
|
|
{
|
|
public function __construct(private readonly ResourceConnection $resourceConnection)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @param string[] $result
|
|
* @param string[] $couponCodes
|
|
* @return string[]
|
|
*/
|
|
public function afterExecute(
|
|
ValidateCouponCode $subject,
|
|
array $result,
|
|
array $couponCodes,
|
|
?int $customerId = null
|
|
): array {
|
|
if (!$result) {
|
|
return $result;
|
|
}
|
|
|
|
$connection = $this->resourceConnection->getConnection();
|
|
$select = $connection->select()
|
|
->from(['coupon' => $this->resourceConnection->getTableName('salesrule_coupon')], ['code'])
|
|
->joinInner(
|
|
['assignment' => $this->resourceConnection->getTableName('esun_customer_coupon')],
|
|
'assignment.coupon_id = coupon.coupon_id',
|
|
['customer_id']
|
|
)
|
|
->where('coupon.code IN (?)', array_values($result));
|
|
$assigned = $connection->fetchPairs($select);
|
|
|
|
foreach ($result as $ruleId => $code) {
|
|
if (isset($assigned[$code]) && (!$customerId || (int)$assigned[$code] !== $customerId)) {
|
|
unset($result[$ruleId]);
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
}
|