This commit is contained in:
2026-09-15 10:35:56 +08:00
commit 782f8ea7aa
4086 changed files with 631288 additions and 0 deletions
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Esun\CustomerCoupon\Block\Adminhtml\Form;
use Magento\Backend\Block\Template;
use Magento\Framework\Registry;
use Magento\SalesRule\Model\RegistryConstants;
class CustomerSelect extends Template
{
public function __construct(
Template\Context $context,
private readonly Registry $registry,
array $data = []
) {
parent::__construct($context, $data);
}
public function getSearchUrl(): string
{
$rule = $this->registry->registry(RegistryConstants::CURRENT_SALES_RULE);
return $this->getUrl('esun_customer_coupon/customer/search', [
'rule_id' => $rule ? (int)$rule->getId() : 0,
]);
}
}
@@ -0,0 +1,97 @@
<?php
declare(strict_types=1);
namespace Esun\CustomerCoupon\Block\Adminhtml\Promo\Quote\Edit\Tab\Coupons;
use Magento\Framework\Data\Collection;
use Magento\Framework\Data\Collection\AbstractDb;
class Grid extends \Magento\SalesRule\Block\Adminhtml\Promo\Quote\Edit\Tab\Coupons\Grid
{
public function setCollection($collection)
{
if ($collection instanceof AbstractDb && !$collection->getFlag('esun_customer_coupon_joined')) {
// Keep rule_id out of the joined relation. The core coupon collection adds an
// unqualified `rule_id` condition before the grid receives the collection.
$assignmentSelect = $collection->getConnection()->select()
->from(
$collection->getTable('esun_customer_coupon'),
['coupon_id', 'customer_id']
);
$collection->getSelect()
->joinLeft(
['customer_coupon_assignment' => $assignmentSelect],
'main_table.coupon_id = customer_coupon_assignment.coupon_id',
['assigned_customer_id' => 'customer_id']
)
->joinLeft(
['assigned_customer' => $collection->getTable('customer_entity')],
'customer_coupon_assignment.customer_id = assigned_customer.entity_id',
[
'assigned_customer_name' => new \Zend_Db_Expr(
"TRIM(CONCAT(COALESCE(assigned_customer.firstname, ''), ' ', "
. "COALESCE(assigned_customer.lastname, '')))"
),
'assigned_customer_email' => 'email',
]
);
$collection->setFlag('esun_customer_coupon_joined', true);
}
return parent::setCollection($collection);
}
protected function _prepareColumns()
{
parent::_prepareColumns();
$this->addColumnAfter(
'assigned_customer_name',
[
'header' => __('Assigned Customer'),
'index' => 'assigned_customer_name',
'filter_condition_callback' => [$this, 'filterAssignedCustomer'],
'renderer' => \Esun\CustomerCoupon\Block\Adminhtml\Promo\Quote\Edit\Tab\Coupons\Grid\CustomerRenderer::class,
],
'code'
);
$this->addColumnAfter(
'assigned_customer_email',
[
'header' => __('Customer Email'),
'index' => 'assigned_customer_email',
'filter_condition_callback' => [$this, 'filterCustomerEmail'],
],
'assigned_customer_name'
);
return $this;
}
public function filterAssignedCustomer(AbstractDb $collection, $column): void
{
$value = trim((string)$column->getFilter()->getValue());
if ($value === '') {
return;
}
$connection = $collection->getConnection();
$like = '%' . $value . '%';
$conditions = [
$connection->quoteInto('assigned_customer.firstname LIKE ?', $like),
$connection->quoteInto('assigned_customer.lastname LIKE ?', $like),
];
if (ctype_digit($value)) {
$conditions[] = $connection->quoteInto('assigned_customer.entity_id = ?', (int)$value);
}
$collection->getSelect()->where(implode(' OR ', $conditions));
}
public function filterCustomerEmail(AbstractDb $collection, $column): void
{
$value = trim((string)$column->getFilter()->getValue());
if ($value !== '') {
$collection->getSelect()->where('assigned_customer.email LIKE ?', '%' . $value . '%');
}
}
}
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace Esun\CustomerCoupon\Block\Adminhtml\Promo\Quote\Edit\Tab\Coupons\Grid;
use Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer;
use Magento\Framework\DataObject;
class CustomerRenderer extends AbstractRenderer
{
public function render(DataObject $row): string
{
if (!$row->getData('assigned_customer_id')) {
return '&mdash;';
}
$name = trim((string)$row->getData('assigned_customer_name')) ?: (string)__('Customer');
return $this->_escaper->escapeHtml($name) . ' (#' . (int)$row->getData('assigned_customer_id') . ')';
}
}