init
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\InteractiveCards\Controller\Product;
|
||||
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Catalog\Helper\ImageFactory;
|
||||
use Magento\Checkout\Helper\Cart;
|
||||
use Magento\Framework\App\Action\HttpGetActionInterface;
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Magento\Framework\Controller\Result\JsonFactory;
|
||||
use Magento\Framework\Pricing\PriceCurrencyInterface;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
/**
|
||||
* Returns the storefront product data used by an expanded interactive card.
|
||||
*/
|
||||
class View implements HttpGetActionInterface
|
||||
{
|
||||
/**
|
||||
* @param JsonFactory $jsonFactory
|
||||
* @param RequestInterface $request
|
||||
* @param ProductRepositoryInterface $productRepository
|
||||
* @param StoreManagerInterface $storeManager
|
||||
* @param ImageFactory $imageFactory
|
||||
* @param PriceCurrencyInterface $priceCurrency
|
||||
* @param Cart $cartHelper
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly JsonFactory $jsonFactory,
|
||||
private readonly RequestInterface $request,
|
||||
private readonly ProductRepositoryInterface $productRepository,
|
||||
private readonly StoreManagerInterface $storeManager,
|
||||
private readonly ImageFactory $imageFactory,
|
||||
private readonly PriceCurrencyInterface $priceCurrency,
|
||||
private readonly Cart $cartHelper
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return product card data for the requested SKU.
|
||||
*
|
||||
* @return \Magento\Framework\Controller\Result\Json
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
$result = $this->jsonFactory->create();
|
||||
$sku = trim((string)$this->request->getParam('sku'));
|
||||
|
||||
if ($sku === '') {
|
||||
return $result->setHttpResponseCode(400)->setData([
|
||||
'success' => false,
|
||||
'message' => __('A product SKU is required.')
|
||||
]);
|
||||
}
|
||||
|
||||
try {
|
||||
$storeId = (int)$this->storeManager->getStore()->getId();
|
||||
$product = $this->productRepository->get($sku, false, $storeId, true);
|
||||
$image = $this->imageFactory->create()
|
||||
->init($product, 'category_page_grid')
|
||||
->getUrl();
|
||||
$regularPrice = (float)$product->getPrice();
|
||||
$finalPrice = (float)$product->getFinalPrice();
|
||||
$hasRequiredOptions = $product->getTypeInstance()->hasRequiredOptions($product);
|
||||
|
||||
return $result->setData([
|
||||
'success' => true,
|
||||
'product' => [
|
||||
'id' => (int)$product->getId(),
|
||||
'sku' => $product->getSku(),
|
||||
'name' => $product->getName(),
|
||||
'image' => $image,
|
||||
'url' => $product->getProductUrl(),
|
||||
'price' => $this->priceCurrency->format($finalPrice, false),
|
||||
'old_price' => $regularPrice > $finalPrice
|
||||
? $this->priceCurrency->format($regularPrice, false)
|
||||
: null,
|
||||
'add_url' => $this->cartHelper->getAddUrl($product),
|
||||
'can_add_to_cart' => $product->isSaleable() && !$hasRequiredOptions
|
||||
]
|
||||
]);
|
||||
} catch (\Throwable $exception) {
|
||||
return $result->setHttpResponseCode(404)->setData([
|
||||
'success' => false,
|
||||
'message' => __('The product could not be loaded.')
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user