init
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\CustomerCoupon\Controller\Account;
|
||||
|
||||
use Magento\Customer\Controller\AbstractAccount;
|
||||
use Magento\Framework\App\Action\Context;
|
||||
use Magento\Framework\App\Action\HttpGetActionInterface;
|
||||
use Magento\Framework\View\Result\Page;
|
||||
use Magento\Framework\View\Result\PageFactory;
|
||||
|
||||
class Index extends AbstractAccount implements HttpGetActionInterface
|
||||
{
|
||||
public function __construct(Context $context, private readonly PageFactory $pageFactory)
|
||||
{
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$page = $this->pageFactory->create();
|
||||
$page->getConfig()->getTitle()->set(__('My Coupons'));
|
||||
|
||||
return $page;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\CustomerCoupon\Controller\Adminhtml\Customer;
|
||||
|
||||
use Magento\Backend\App\Action;
|
||||
use Magento\Backend\App\Action\Context;
|
||||
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory;
|
||||
use Magento\Framework\Controller\Result\Json;
|
||||
use Magento\Framework\Controller\Result\JsonFactory;
|
||||
use Magento\SalesRule\Model\RuleFactory;
|
||||
|
||||
class Search extends Action
|
||||
{
|
||||
public const ADMIN_RESOURCE = 'Magento_SalesRule::quote';
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
private readonly JsonFactory $jsonFactory,
|
||||
private readonly CollectionFactory $customerCollectionFactory,
|
||||
private readonly RuleFactory $ruleFactory
|
||||
) {
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
public function execute(): Json
|
||||
{
|
||||
$searchKey = trim((string)$this->getRequest()->getParam('searchKey', ''));
|
||||
$page = max(1, (int)$this->getRequest()->getParam('page', 1));
|
||||
$limit = min(50, max(1, (int)$this->getRequest()->getParam('limit', 20)));
|
||||
$ruleId = (int)$this->getRequest()->getParam('rule_id');
|
||||
|
||||
$collection = $this->customerCollectionFactory->create();
|
||||
$collection->addAttributeToSelect(['firstname', 'lastname', 'email']);
|
||||
|
||||
if ($ruleId) {
|
||||
$rule = $this->ruleFactory->create()->load($ruleId);
|
||||
if ($rule->getId()) {
|
||||
$collection->addFieldToFilter('website_id', ['in' => array_map('intval', $rule->getWebsiteIds())]);
|
||||
$collection->addFieldToFilter('group_id', ['in' => array_map('intval', $rule->getCustomerGroupIds())]);
|
||||
}
|
||||
}
|
||||
|
||||
if ($searchKey !== '') {
|
||||
$like = '%' . addcslashes($searchKey, '%_') . '%';
|
||||
$conditions = [
|
||||
['attribute' => 'email', 'like' => $like],
|
||||
['attribute' => 'firstname', 'like' => $like],
|
||||
['attribute' => 'lastname', 'like' => $like],
|
||||
];
|
||||
if (ctype_digit($searchKey)) {
|
||||
$conditions[] = ['attribute' => 'entity_id', 'eq' => (int)$searchKey];
|
||||
}
|
||||
$collection->addAttributeToFilter($conditions, null, 'left');
|
||||
}
|
||||
|
||||
$collection->setOrder('entity_id', 'DESC')->setPageSize($limit)->setCurPage($page);
|
||||
$options = [];
|
||||
foreach ($collection as $customer) {
|
||||
$name = trim((string)$customer->getFirstname() . ' ' . (string)$customer->getLastname());
|
||||
$options[] = [
|
||||
'value' => (int)$customer->getId(),
|
||||
'label' => sprintf('%s <%s> (#%d)', $name ?: __('Customer'), $customer->getEmail(), $customer->getId()),
|
||||
'path' => '',
|
||||
'level' => 0,
|
||||
];
|
||||
}
|
||||
|
||||
return $this->jsonFactory->create()->setData([
|
||||
'options' => $options,
|
||||
'total' => (int)$collection->getSize(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\CustomerCoupon\Controller\Ajax;
|
||||
|
||||
use Esun\CustomerCoupon\Model\Coupon\ListProvider;
|
||||
use Magento\Checkout\Model\Session as CheckoutSession;
|
||||
use Magento\Customer\Model\Session as CustomerSession;
|
||||
use Magento\Framework\App\Action\Action;
|
||||
use Magento\Framework\App\Action\Context;
|
||||
use Magento\Framework\App\Action\HttpGetActionInterface;
|
||||
use Magento\Framework\Controller\Result\Json;
|
||||
use Magento\Framework\Controller\Result\JsonFactory;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
class ListAction extends Action implements HttpGetActionInterface
|
||||
{
|
||||
public function __construct(
|
||||
Context $context,
|
||||
private readonly JsonFactory $jsonFactory,
|
||||
private readonly CustomerSession $customerSession,
|
||||
private readonly CheckoutSession $checkoutSession,
|
||||
private readonly StoreManagerInterface $storeManager,
|
||||
private readonly ListProvider $listProvider
|
||||
) {
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
public function execute(): Json
|
||||
{
|
||||
$result = $this->jsonFactory->create();
|
||||
if (!$this->customerSession->isLoggedIn()) {
|
||||
return $result->setHttpResponseCode(401)->setData([
|
||||
'success' => false,
|
||||
'message' => (string)__('Please sign in to view your coupons.'),
|
||||
]);
|
||||
}
|
||||
|
||||
$quote = $this->checkoutSession->getQuote();
|
||||
$coupons = $this->listProvider->getForCustomer(
|
||||
(int)$this->customerSession->getCustomerId(),
|
||||
(int)$this->storeManager->getStore()->getWebsiteId(),
|
||||
(int)$this->customerSession->getCustomerGroupId(),
|
||||
$quote
|
||||
);
|
||||
$coupons = array_values(array_filter(
|
||||
$coupons,
|
||||
static fn(array $coupon): bool => $coupon['status'] === 'available' && $coupon['cart_applicable']
|
||||
));
|
||||
|
||||
return $result->setData([
|
||||
'success' => true,
|
||||
'current_coupon_code' => (string)$quote->getCouponCode(),
|
||||
'coupons' => $coupons,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user