43 lines
1.3 KiB
PHP
43 lines
1.3 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\CustomerCoupon\Model\Config\Source;
|
|
|
|
use Magento\Framework\Data\OptionSourceInterface;
|
|
use Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory;
|
|
use Magento\SalesRule\Model\Rule;
|
|
|
|
class AutoGeneratedCartRule implements OptionSourceInterface
|
|
{
|
|
public function __construct(private readonly CollectionFactory $collectionFactory)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* Return active cart rules that support generated coupon codes.
|
|
*
|
|
* @return array<int, array{value: int|string, label: \Stringable|string}>
|
|
*/
|
|
public function toOptionArray(): array
|
|
{
|
|
$collection = $this->collectionFactory->create();
|
|
$collection->addFieldToSelect(['rule_id', 'name', 'coupon_type', 'use_auto_generation']);
|
|
$collection->addFieldToFilter('is_active', 1);
|
|
$collection->addFieldToFilter(
|
|
['coupon_type', 'use_auto_generation'],
|
|
[['eq' => Rule::COUPON_TYPE_AUTO], ['eq' => 1]]
|
|
);
|
|
$collection->setOrder('name', 'ASC');
|
|
|
|
$options = [['value' => '', 'label' => __('-- Disabled --')]];
|
|
foreach ($collection as $rule) {
|
|
$options[] = [
|
|
'value' => (int)$rule->getId(),
|
|
'label' => sprintf('%s (ID: %d)', (string)$rule->getName(), (int)$rule->getId()),
|
|
];
|
|
}
|
|
|
|
return $options;
|
|
}
|
|
}
|