Files
2026-09-15 10:35:56 +08:00

155 lines
5.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\Homepage\Block;
use Esun\Catalog\Model\Product\CardPriceResolver;
use Esun\Video\Model\ResourceModel\Video\CollectionFactory as VideoCollectionFactory;
use Magento\Catalog\Block\Product\AbstractProduct;
use Magento\Catalog\Block\Product\Context;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
use Magento\Customer\Model\Context as CustomerContext;
use Magento\Framework\App\Http\Context as HttpContext;
use Magento\Framework\DataObject\IdentityInterface;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\ScopeInterface;
class LandscapeVideo extends AbstractProduct implements IdentityInterface
{
private const XML_PATH = 'esun_homepage/landscape_video/';
/**
* @var array<string, mixed>|null
*/
private ?array $item = null;
public function __construct(
Context $context,
private readonly ProductCollectionFactory $productCollectionFactory,
private readonly VideoCollectionFactory $videoCollectionFactory,
private readonly Visibility $productVisibility,
private readonly PriceCurrencyInterface $priceCurrency,
private readonly HttpContext $httpContext,
private readonly CardPriceResolver $cardPriceResolver,
array $data = []
) {
parent::__construct($context, $data);
}
/**
* @return array<string, mixed>
*/
public function getItem(): array
{
if ($this->item !== null) {
return $this->item;
}
$videoId = (int) $this->getConfig('video_id');
$sku = trim((string) $this->getConfig('product_sku'));
if ($videoId <= 0 || $sku === '') {
return $this->item = [];
}
$videos = $this->videoCollectionFactory->create();
$videos->addFieldToFilter('video_id', $videoId);
$videos->addFieldToFilter('status', 1);
$videos->setPageSize(1);
$video = $videos->getFirstItem();
if (!$video->getId() || trim((string) $video->getData('cover_path')) === '') {
return $this->item = [];
}
$products = $this->productCollectionFactory->create();
$products->setStoreId((int) $this->_storeManager->getStore()->getId());
$this->_addProductAttributesAndPrices($products);
$products->addStoreFilter();
$products->addAttributeToFilter('sku', $sku);
$products->addAttributeToFilter(
'visibility',
['in' => $this->productVisibility->getVisibleInSiteIds()]
);
$products->addAttributeToFilter(
'status',
Product\Attribute\Source\Status::STATUS_ENABLED
);
$products->setPageSize(1);
$product = $products->getFirstItem();
if (!$product->getId()) {
return $this->item = [];
}
$mediaBaseUrl = $this->_storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA);
return $this->item = [
'video_url' => $mediaBaseUrl . ltrim((string) $video->getData('video_path'), '/'),
'cover_url' => $mediaBaseUrl . ltrim((string) $video->getData('cover_path'), '/'),
'product' => $product,
];
}
public function getFormattedFinalPrice(Product $product): string
{
return $this->formatPrice($this->getFinalPriceValue($product));
}
public function getFormattedRegularPrice(Product $product): string
{
return $this->formatPrice($this->getRegularPriceValue($product));
}
public function hasRegularPrice(Product $product): bool
{
return $this->getRegularPriceValue($product) > $this->getFinalPriceValue($product);
}
public function getCacheKeyInfo(): array
{
return [
'ESUN_HOMEPAGE_LANDSCAPE_VIDEO',
(int) $this->_storeManager->getStore()->getId(),
(int) $this->httpContext->getValue(CustomerContext::CONTEXT_GROUP),
(int) $this->getConfig('video_id'),
sha1(trim((string) $this->getConfig('product_sku'))),
];
}
public function getIdentities(): array
{
$product = $this->getItem()['product'] ?? null;
return $product instanceof Product ? $product->getIdentities() : [];
}
private function getConfig(string $field): mixed
{
return $this->_scopeConfig->getValue(
self::XML_PATH . $field,
ScopeInterface::SCOPE_STORE
);
}
private function getFinalPriceValue(Product $product): float
{
return $this->cardPriceResolver->getFinalPrice($product);
}
private function getRegularPriceValue(Product $product): float
{
return $this->cardPriceResolver->getRegularPrice($product);
}
private function formatPrice(float $price): string
{
return $this->priceCurrency->format(
$price,
false,
PriceCurrencyInterface::DEFAULT_PRECISION,
$this->_storeManager->getStore()
);
}
}