init
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\ViewModel\Adminhtml;
|
||||
|
||||
use Esun\Catalog\Model\ResourceModel\ColorOption\CollectionFactory as ColorOptionCollectionFactory;
|
||||
use Magento\Eav\Model\Config as EavConfig;
|
||||
use Magento\Framework\UrlInterface;
|
||||
use Magento\Framework\View\Element\Block\ArgumentInterface;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
class ColorOptionSwatchConfig implements ArgumentInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ColorOptionCollectionFactory $colorOptionCollectionFactory,
|
||||
private readonly StoreManagerInterface $storeManager,
|
||||
private readonly EavConfig $eavConfig
|
||||
) {
|
||||
}
|
||||
|
||||
public function isColorAttribute(int $attributeId): bool
|
||||
{
|
||||
if ($attributeId <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $attributeId === (int)$this->eavConfig
|
||||
->getAttribute('catalog_product', 'color')
|
||||
->getId();
|
||||
}
|
||||
|
||||
public function enrichJsonConfig(string $jsonConfig): string
|
||||
{
|
||||
$config = json_decode($jsonConfig, true);
|
||||
if (!is_array($config)) {
|
||||
return $jsonConfig;
|
||||
}
|
||||
|
||||
$optionIds = [];
|
||||
foreach ($config['attributesData'] ?? [] as $row) {
|
||||
$optionId = (int)($row['id'] ?? 0);
|
||||
if ($optionId > 0) {
|
||||
$optionIds[$optionId] = $optionId;
|
||||
}
|
||||
}
|
||||
|
||||
$esunOptions = $this->getEsunOptionsByOptionId($optionIds);
|
||||
if (!isset($config['attributesData']) || !is_array($config['attributesData'])) {
|
||||
return (string)json_encode($config);
|
||||
}
|
||||
|
||||
foreach ($config['attributesData'] as &$row) {
|
||||
$optionId = (int)($row['id'] ?? 0);
|
||||
$esunOption = $esunOptions[$optionId] ?? [];
|
||||
$ballImage = (string)($esunOption['ball_image'] ?? '');
|
||||
$row['esun_swatch_colors'] = (string)($esunOption['swatch_colors'] ?? '');
|
||||
$row['esun_is_hot_sale'] = (int)($esunOption['is_hot_sale'] ?? 0) === 1;
|
||||
$row['esun_ball_image'] = $ballImage;
|
||||
$row['esun_ball_image_url'] = $ballImage !== ''
|
||||
? $this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA) . ltrim($ballImage, '/')
|
||||
: '';
|
||||
$row['esun_delete_ball_image'] = '';
|
||||
$row['esun_ball_image_base64'] = '';
|
||||
$row['esun_ball_image_name'] = '';
|
||||
}
|
||||
unset($row);
|
||||
|
||||
return (string)json_encode($config);
|
||||
}
|
||||
|
||||
private function getEsunOptionsByOptionId(array $optionIds): array
|
||||
{
|
||||
if (!$optionIds) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$items = [];
|
||||
$collection = $this->colorOptionCollectionFactory->create()
|
||||
->addFieldToFilter('option_id', ['in' => array_values($optionIds)]);
|
||||
foreach ($collection as $item) {
|
||||
$items[(int)$item->getData('option_id')] = [
|
||||
'swatch_colors' => (string)$item->getData('swatch_colors'),
|
||||
'is_hot_sale' => (int)$item->getData('is_hot_sale'),
|
||||
'ball_image' => (string)$item->getData('ball_image'),
|
||||
];
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\ViewModel\Category;
|
||||
|
||||
use Esun\Catalog\Model\Product\CardPriceResolver;
|
||||
use Magento\Catalog\Helper\Image as ImageHelper;
|
||||
use Magento\Catalog\Api\CategoryRepositoryInterface;
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
|
||||
use Magento\Framework\App\Http\Context as HttpContext;
|
||||
use Magento\Framework\Pricing\PriceCurrencyInterface;
|
||||
use Magento\Framework\Registry;
|
||||
use Magento\Framework\View\Element\Block\ArgumentInterface;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Magento\Swatches\Helper\Data as SwatchHelper;
|
||||
use Magento\Swatches\Helper\Media as SwatchMediaHelper;
|
||||
use Magento\Swatches\Model\Swatch;
|
||||
|
||||
class ProductCard implements ArgumentInterface
|
||||
{
|
||||
/**
|
||||
* @var array<int, array<int, array<string, int|string>>>
|
||||
*/
|
||||
private array $colorSwatches = [];
|
||||
|
||||
/**
|
||||
* @var array<int, string>
|
||||
*/
|
||||
private array $categoryNames = [];
|
||||
|
||||
public function __construct(
|
||||
private readonly CollectionFactory $productCollectionFactory,
|
||||
private readonly SwatchHelper $swatchHelper,
|
||||
private readonly SwatchMediaHelper $swatchMediaHelper,
|
||||
private readonly ImageHelper $imageHelper,
|
||||
private readonly PriceCurrencyInterface $priceCurrency,
|
||||
private readonly StoreManagerInterface $storeManager,
|
||||
private readonly Registry $registry,
|
||||
private readonly CategoryRepositoryInterface $categoryRepository,
|
||||
private readonly HttpContext $httpContext,
|
||||
private readonly CardPriceResolver $cardPriceResolver
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array<string, int|string>>
|
||||
*/
|
||||
public function getColorSwatches(Product $product): array
|
||||
{
|
||||
$productId = (int) $product->getId();
|
||||
if (isset($this->colorSwatches[$productId])) {
|
||||
return $this->colorSwatches[$productId];
|
||||
}
|
||||
|
||||
if ($product->getTypeId() !== 'configurable') {
|
||||
return $this->colorSwatches[$productId] = [];
|
||||
}
|
||||
|
||||
$colorAttribute = null;
|
||||
foreach ($product->getTypeInstance()->getConfigurableAttributes($product) as $attribute) {
|
||||
$productAttribute = $attribute->getProductAttribute();
|
||||
if ($productAttribute->getAttributeCode() === 'color') {
|
||||
$colorAttribute = $productAttribute;
|
||||
$colorAttribute->setStoreId((int) $this->storeManager->getStore()->getId());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($colorAttribute === null) {
|
||||
return $this->colorSwatches[$productId] = [];
|
||||
}
|
||||
|
||||
$childIdsByGroup = $product->getTypeInstance()->getChildrenIds($productId);
|
||||
$childIds = array_values(array_unique(array_map(
|
||||
'intval',
|
||||
array_merge(...array_values($childIdsByGroup ?: [[]]))
|
||||
)));
|
||||
if (!$childIds) {
|
||||
return $this->colorSwatches[$productId] = [];
|
||||
}
|
||||
|
||||
$childCollection = $this->productCollectionFactory->create();
|
||||
// This collection only supplies display data for color swatches. Keep
|
||||
// enabled variations even when every child is out of stock.
|
||||
$childCollection->setFlag('has_stock_status_filter', true);
|
||||
$childCollection->setStoreId((int) $this->storeManager->getStore()->getId());
|
||||
$childCollection->addIdFilter($childIds);
|
||||
$childCollection->addAttributeToSelect(['color', 'image', 'small_image', 'thumbnail', 'sku']);
|
||||
$childCollection->addAttributeToFilter(
|
||||
'status',
|
||||
Product\Attribute\Source\Status::STATUS_ENABLED
|
||||
);
|
||||
$childCollection->setOrder('entity_id', 'ASC');
|
||||
|
||||
$childrenByOption = [];
|
||||
foreach ($childCollection as $childProduct) {
|
||||
$optionId = (int) $childProduct->getData('color');
|
||||
if ($optionId > 0 && !isset($childrenByOption[$optionId])) {
|
||||
$childrenByOption[$optionId] = $childProduct;
|
||||
}
|
||||
}
|
||||
if (!$childrenByOption) {
|
||||
return $this->colorSwatches[$productId] = [];
|
||||
}
|
||||
|
||||
$swatchData = $this->swatchHelper->getSwatchesByOptionsId(array_keys($childrenByOption));
|
||||
$result = [];
|
||||
foreach ($childrenByOption as $optionId => $childProduct) {
|
||||
$swatch = $swatchData[$optionId] ?? [];
|
||||
$type = (int) ($swatch['type'] ?? Swatch::SWATCH_TYPE_EMPTY);
|
||||
$value = (string) ($swatch['value'] ?? '');
|
||||
$result[] = [
|
||||
'id' => $optionId,
|
||||
'label' => (string) $colorAttribute->getSource()->getOptionText($optionId),
|
||||
'type' => $type,
|
||||
'value' => $value,
|
||||
'image' => $type === Swatch::SWATCH_TYPE_VISUAL_IMAGE && $value !== ''
|
||||
? $this->swatchMediaHelper->getSwatchAttributeImage(Swatch::SWATCH_IMAGE_NAME, $value)
|
||||
: '',
|
||||
'product_image' => $this->getProductImageUrl($childProduct),
|
||||
];
|
||||
}
|
||||
|
||||
return $this->colorSwatches[$productId] = $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getCategoryLabels(Product $product): array
|
||||
{
|
||||
$labels = [];
|
||||
$productCategoryIds = array_map('intval', $product->getCategoryIds());
|
||||
$currentCategory = $this->registry->registry('current_category');
|
||||
if ($currentCategory && in_array((int) $currentCategory->getId(), $productCategoryIds, true)) {
|
||||
$labels[] = (string) $currentCategory->getName();
|
||||
}
|
||||
|
||||
foreach ($productCategoryIds as $categoryId) {
|
||||
if (count($labels) >= 2) {
|
||||
break;
|
||||
}
|
||||
if ($currentCategory && $categoryId === (int) $currentCategory->getId()) {
|
||||
continue;
|
||||
}
|
||||
if (!isset($this->categoryNames[$categoryId])) {
|
||||
try {
|
||||
$category = $this->categoryRepository->get(
|
||||
$categoryId,
|
||||
(int) $this->storeManager->getStore()->getId()
|
||||
);
|
||||
$this->categoryNames[$categoryId] = $category->getIsActive()
|
||||
? (string) $category->getName()
|
||||
: '';
|
||||
} catch (\Throwable) {
|
||||
$this->categoryNames[$categoryId] = '';
|
||||
}
|
||||
}
|
||||
if ($this->categoryNames[$categoryId] !== '') {
|
||||
$labels[] = $this->categoryNames[$categoryId];
|
||||
}
|
||||
}
|
||||
|
||||
return array_values(array_unique(array_filter($labels)));
|
||||
}
|
||||
|
||||
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 getCustomerGroupCacheKey(): string
|
||||
{
|
||||
return (string) $this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_GROUP);
|
||||
}
|
||||
|
||||
private function getProductImageUrl(Product $product): string
|
||||
{
|
||||
return $this->imageHelper
|
||||
->init($product, 'category_page_grid')
|
||||
->getUrl();
|
||||
}
|
||||
|
||||
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()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\ViewModel\LayeredNavigation;
|
||||
|
||||
use Magento\Framework\View\Element\Block\ArgumentInterface;
|
||||
use Magento\Swatches\Helper\Data as SwatchHelper;
|
||||
use Magento\Swatches\Helper\Media as SwatchMediaHelper;
|
||||
use Magento\Swatches\Model\Swatch;
|
||||
|
||||
class AppliedFilterSwatch implements ArgumentInterface
|
||||
{
|
||||
/**
|
||||
* @var array<int, array{color: string, image: string}>
|
||||
*/
|
||||
private array $swatches = [];
|
||||
|
||||
public function __construct(
|
||||
private readonly SwatchHelper $swatchHelper,
|
||||
private readonly SwatchMediaHelper $swatchMediaHelper
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{color: string, image: string}
|
||||
*/
|
||||
public function getSwatch(int $optionId): array
|
||||
{
|
||||
if ($optionId <= 0) {
|
||||
return ['color' => '', 'image' => ''];
|
||||
}
|
||||
|
||||
if (isset($this->swatches[$optionId])) {
|
||||
return $this->swatches[$optionId];
|
||||
}
|
||||
|
||||
$swatch = $this->swatchHelper->getSwatchesByOptionsId([$optionId])[$optionId] ?? [];
|
||||
$type = (int) ($swatch['type'] ?? Swatch::SWATCH_TYPE_EMPTY);
|
||||
$value = trim((string) ($swatch['value'] ?? ''));
|
||||
|
||||
if ($type === Swatch::SWATCH_TYPE_VISUAL_COLOR
|
||||
&& preg_match('/^#[0-9a-f]{3,8}$/i', $value)
|
||||
) {
|
||||
return $this->swatches[$optionId] = ['color' => $value, 'image' => ''];
|
||||
}
|
||||
|
||||
if ($type === Swatch::SWATCH_TYPE_VISUAL_IMAGE && $value !== '') {
|
||||
return $this->swatches[$optionId] = [
|
||||
'color' => '',
|
||||
'image' => $this->swatchMediaHelper->getSwatchAttributeImage(
|
||||
Swatch::SWATCH_IMAGE_NAME,
|
||||
$value
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
return $this->swatches[$optionId] = ['color' => '', 'image' => ''];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\ViewModel\Product;
|
||||
|
||||
use Magento\Catalog\Api\CategoryRepositoryInterface;
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Framework\Registry;
|
||||
use Magento\Framework\View\Element\Block\ArgumentInterface;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
class Breadcrumbs implements ArgumentInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Registry $registry,
|
||||
private readonly CategoryRepositoryInterface $categoryRepository,
|
||||
private readonly StoreManagerInterface $storeManager
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a stable breadcrumb path from the deepest category assigned to the product.
|
||||
*
|
||||
* @return array<int, array{label: string, link: string, last: bool}>
|
||||
*/
|
||||
public function getItems(): array
|
||||
{
|
||||
$items = [[
|
||||
'label' => (string)__('Home'),
|
||||
'link' => $this->storeManager->getStore()->getBaseUrl(),
|
||||
'last' => false,
|
||||
]];
|
||||
|
||||
$product = $this->registry->registry('current_product');
|
||||
if (!$product instanceof Product) {
|
||||
return $items;
|
||||
}
|
||||
|
||||
$category = $this->getDeepestCategory($product->getCategoryIds());
|
||||
if ($category !== null) {
|
||||
foreach ($category->getParentCategories() as $parent) {
|
||||
// Level 1 is the store root category and is not part of storefront navigation.
|
||||
if ((int)$parent->getLevel() <= 1 || !(bool)$parent->getIsActive()) {
|
||||
continue;
|
||||
}
|
||||
$items[] = [
|
||||
'label' => (string)$parent->getName(),
|
||||
'link' => (string)$parent->getUrl(),
|
||||
'last' => false,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$items[] = [
|
||||
'label' => (string)$product->getName(),
|
||||
'link' => '',
|
||||
'last' => true,
|
||||
];
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
private function getDeepestCategory(array $categoryIds): ?\Magento\Catalog\Api\Data\CategoryInterface
|
||||
{
|
||||
$deepest = null;
|
||||
foreach ($categoryIds as $categoryId) {
|
||||
try {
|
||||
$category = $this->categoryRepository->get((int)$categoryId);
|
||||
} catch (\Magento\Framework\Exception\NoSuchEntityException) {
|
||||
continue;
|
||||
}
|
||||
if (!(bool)$category->getIsActive()) {
|
||||
continue;
|
||||
}
|
||||
if ($deepest === null || (int)$category->getLevel() > (int)$deepest->getLevel()) {
|
||||
$deepest = $category;
|
||||
}
|
||||
}
|
||||
|
||||
return $deepest;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user