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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user