init
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Homepage\Block;
|
||||
|
||||
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;
|
||||
use Magento\Cms\Model\Template\FilterProvider;
|
||||
use Magento\Framework\DataObject\IdentityInterface;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
|
||||
class NewProduct extends AbstractProduct implements IdentityInterface
|
||||
{
|
||||
private const XML_PATH_SKUS = 'esun_homepage/new_product/skus';
|
||||
private const FEATURE_ATTRIBUTE = 'homepage_feature_content';
|
||||
private const GALLERY_ATTRIBUTE = 'homepage_gallery_content';
|
||||
|
||||
/**
|
||||
* @var Product[]|null
|
||||
*/
|
||||
private ?array $products = null;
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
private readonly CollectionFactory $productCollectionFactory,
|
||||
private readonly Visibility $productVisibility,
|
||||
private readonly FilterProvider $filterProvider,
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct($context, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Product[]
|
||||
*/
|
||||
public function getProducts(): array
|
||||
{
|
||||
if ($this->products !== null) {
|
||||
return $this->products;
|
||||
}
|
||||
|
||||
$skus = $this->getConfiguredSkus();
|
||||
if (!$skus) {
|
||||
return $this->products = [];
|
||||
}
|
||||
|
||||
$collection = $this->productCollectionFactory->create();
|
||||
$collection->setStoreId((int) $this->_storeManager->getStore()->getId());
|
||||
$this->_addProductAttributesAndPrices($collection);
|
||||
$collection->addAttributeToSelect([self::FEATURE_ATTRIBUTE, self::GALLERY_ATTRIBUTE]);
|
||||
$collection->addStoreFilter();
|
||||
$collection->addAttributeToFilter('sku', ['in' => $skus]);
|
||||
$collection->addAttributeToFilter(
|
||||
'visibility',
|
||||
['in' => $this->productVisibility->getVisibleInSiteIds()]
|
||||
);
|
||||
$collection->addAttributeToFilter(
|
||||
'status',
|
||||
Product\Attribute\Source\Status::STATUS_ENABLED
|
||||
);
|
||||
|
||||
$productsBySku = [];
|
||||
foreach ($collection as $product) {
|
||||
$productsBySku[strtolower((string) $product->getSku())] = $product;
|
||||
}
|
||||
|
||||
$this->products = [];
|
||||
foreach ($skus as $sku) {
|
||||
$key = strtolower($sku);
|
||||
if (isset($productsBySku[$key])) {
|
||||
$this->products[] = $productsBySku[$key];
|
||||
}
|
||||
}
|
||||
|
||||
return $this->products;
|
||||
}
|
||||
|
||||
public function getFeatureContent(Product $product): string
|
||||
{
|
||||
return $this->filterContent((string) $product->getData(self::FEATURE_ATTRIBUTE));
|
||||
}
|
||||
|
||||
public function getGalleryContent(Product $product): string
|
||||
{
|
||||
return $this->filterContent((string) $product->getData(self::GALLERY_ATTRIBUTE));
|
||||
}
|
||||
|
||||
public function getIdentities(): array
|
||||
{
|
||||
$identities = [];
|
||||
foreach ($this->getProducts() as $product) {
|
||||
$identities = array_merge($identities, $product->getIdentities());
|
||||
}
|
||||
|
||||
return array_values(array_unique($identities));
|
||||
}
|
||||
|
||||
private function getConfiguredSkus(): array
|
||||
{
|
||||
$value = (string) $this->_scopeConfig->getValue(
|
||||
self::XML_PATH_SKUS,
|
||||
ScopeInterface::SCOPE_STORE
|
||||
);
|
||||
$skus = preg_split('/[\s,;]+/', $value, -1, PREG_SPLIT_NO_EMPTY) ?: [];
|
||||
|
||||
return array_values(array_unique(array_map('trim', $skus)));
|
||||
}
|
||||
|
||||
private function filterContent(string $content): string
|
||||
{
|
||||
if (trim($content) === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $this->filterProvider->getPageFilter()->filter($content);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user