init
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Controller\Status;
|
||||
|
||||
use Magento\Customer\Model\Session;
|
||||
use Magento\Framework\App\Action\Action;
|
||||
use Magento\Framework\App\Action\Context;
|
||||
use Magento\Framework\App\Action\HttpGetActionInterface;
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Magento\Framework\Controller\Result\Json;
|
||||
use Magento\Framework\Controller\Result\JsonFactory;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Wishlist\Model\WishlistFactory;
|
||||
|
||||
class Index extends Action implements HttpGetActionInterface
|
||||
{
|
||||
public function __construct(
|
||||
Context $context,
|
||||
private readonly RequestInterface $request,
|
||||
private readonly JsonFactory $jsonFactory,
|
||||
private readonly Session $customerSession,
|
||||
private readonly WishlistFactory $wishlistFactory,
|
||||
private readonly ResourceConnection $resourceConnection
|
||||
) {
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
public function execute(): Json
|
||||
{
|
||||
$result = $this->jsonFactory->create();
|
||||
$customerId = (int)$this->customerSession->getCustomerId();
|
||||
$productId = (int)$this->request->getParam('product_id');
|
||||
|
||||
if (!$customerId || !$productId) {
|
||||
return $result->setData(['active' => false]);
|
||||
}
|
||||
|
||||
$wishlist = $this->wishlistFactory->create()->loadByCustomerId($customerId, false);
|
||||
if (!$wishlist->getId()) {
|
||||
return $result->setData(['active' => false]);
|
||||
}
|
||||
|
||||
$connection = $this->resourceConnection->getConnection();
|
||||
$active = $connection->fetchOne(
|
||||
$connection->select()
|
||||
->from(
|
||||
$this->resourceConnection->getTableName('wishlist_item'),
|
||||
['wishlist_item_id']
|
||||
)
|
||||
->where('wishlist_id = ?', (int)$wishlist->getId())
|
||||
->where('product_id = ?', $productId)
|
||||
->limit(1)
|
||||
) !== false;
|
||||
|
||||
return $result->setData(['active' => $active]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user