init
This commit is contained in:
@@ -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