|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 */ 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() ); } }