init
This commit is contained in:
@@ -0,0 +1,233 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Plugin\Adminhtml;
|
||||
|
||||
use Esun\Catalog\Model\ColorOptionFactory;
|
||||
use Esun\Catalog\Model\ColorOptionImageUploader;
|
||||
use Esun\Catalog\Model\ColorOption\CacheInvalidator;
|
||||
use Esun\Catalog\Model\ResourceModel\ColorOption as ColorOptionResource;
|
||||
use Magento\Catalog\Controller\Adminhtml\Product\Attribute\Save;
|
||||
use Magento\Eav\Model\Config as EavConfig;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Framework\Controller\ResultInterface;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Message\ManagerInterface;
|
||||
|
||||
class ColorAttributeSavePlugin
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EavConfig $eavConfig,
|
||||
private readonly ResourceConnection $resourceConnection,
|
||||
private readonly ColorOptionFactory $colorOptionFactory,
|
||||
private readonly ColorOptionResource $colorOptionResource,
|
||||
private readonly ColorOptionImageUploader $imageUploader,
|
||||
private readonly CacheInvalidator $cacheInvalidator,
|
||||
private readonly ManagerInterface $messageManager
|
||||
) {
|
||||
}
|
||||
|
||||
public function afterExecute(Save $subject, ResultInterface $result): ResultInterface
|
||||
{
|
||||
try {
|
||||
$request = $subject->getRequest();
|
||||
$attributeId = (int)$request->getParam('attribute_id');
|
||||
$colorAttribute = $this->eavConfig->getAttribute('catalog_product', 'color');
|
||||
if ($attributeId !== (int)$colorAttribute->getId()) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$rows = $this->parseSerializedRows((string)$request->getParam('serialized_options', '[]'));
|
||||
if (!$rows) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$optionLookup = $this->loadSavedOptionLookup($attributeId);
|
||||
$hasChanges = false;
|
||||
foreach ($rows as $rowKey => $row) {
|
||||
$optionId = $this->resolveOptionId((string)$rowKey, $row, $optionLookup);
|
||||
if ($optionId <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!empty($row['delete'])) {
|
||||
$this->deleteColorOption($optionId);
|
||||
$hasChanges = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
$esunData = $row['esun'] ?? [];
|
||||
if (!$esunData) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->saveColorOption($optionId, $row, $esunData);
|
||||
$hasChanges = true;
|
||||
}
|
||||
|
||||
if ($hasChanges) {
|
||||
$this->cacheInvalidator->invalidate();
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$this->messageManager->addErrorMessage(
|
||||
__('The Esun color option data could not be saved: %1', $e->getMessage())
|
||||
);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function parseSerializedRows(string $serializedOptions): array
|
||||
{
|
||||
$decoded = json_decode($serializedOptions, true);
|
||||
if (!is_array($decoded)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$rows = [];
|
||||
foreach ($decoded as $queryString) {
|
||||
if (!is_string($queryString) || $queryString === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$data = [];
|
||||
parse_str($queryString, $data);
|
||||
$rowKey = $this->extractRowKey($data);
|
||||
if ($rowKey === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$rows[$rowKey] = [
|
||||
'sort_order' => (int)($data['optionvisual']['order'][$rowKey] ?? 0),
|
||||
'label' => trim((string)($data['optionvisual']['value'][$rowKey][0] ?? '')),
|
||||
'delete' => (string)($data['optionvisual']['delete'][$rowKey] ?? '') === '1',
|
||||
'esun' => $data['esun_color_option'][$rowKey] ?? [],
|
||||
];
|
||||
}
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
private function extractRowKey(array $data): string
|
||||
{
|
||||
foreach ([
|
||||
$data['optionvisual']['order'] ?? null,
|
||||
$data['optionvisual']['value'] ?? null,
|
||||
$data['esun_color_option'] ?? null,
|
||||
] as $group) {
|
||||
if (is_array($group)) {
|
||||
$key = array_key_first($group);
|
||||
if ($key !== null) {
|
||||
return (string)$key;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
private function loadSavedOptionLookup(int $attributeId): array
|
||||
{
|
||||
$connection = $this->resourceConnection->getConnection();
|
||||
$select = $connection->select()
|
||||
->from(['option' => $this->resourceConnection->getTableName('eav_attribute_option')], [
|
||||
'option_id',
|
||||
'sort_order',
|
||||
])
|
||||
->joinLeft(
|
||||
['value' => $this->resourceConnection->getTableName('eav_attribute_option_value')],
|
||||
'value.option_id = option.option_id AND value.store_id = 0',
|
||||
['label' => 'value.value']
|
||||
)
|
||||
->where('option.attribute_id = ?', $attributeId);
|
||||
|
||||
$lookup = [
|
||||
'by_id' => [],
|
||||
'by_label_sort' => [],
|
||||
];
|
||||
foreach ($connection->fetchAll($select) as $row) {
|
||||
$optionId = (int)$row['option_id'];
|
||||
$label = trim((string)($row['label'] ?? ''));
|
||||
$sortOrder = (int)($row['sort_order'] ?? 0);
|
||||
$lookup['by_id'][$optionId] = $optionId;
|
||||
$lookup['by_label_sort'][$this->buildLookupKey($label, $sortOrder)] = $optionId;
|
||||
}
|
||||
|
||||
return $lookup;
|
||||
}
|
||||
|
||||
private function resolveOptionId(string $rowKey, array $row, array $lookup): int
|
||||
{
|
||||
if (ctype_digit($rowKey)) {
|
||||
$optionId = (int)$rowKey;
|
||||
return (int)($lookup['by_id'][$optionId] ?? 0);
|
||||
}
|
||||
|
||||
return (int)($lookup['by_label_sort'][$this->buildLookupKey((string)$row['label'], (int)$row['sort_order'])] ?? 0);
|
||||
}
|
||||
|
||||
private function saveColorOption(int $optionId, array $row, array $esunData): void
|
||||
{
|
||||
$item = $this->colorOptionFactory->create();
|
||||
$this->colorOptionResource->load($item, $optionId, 'option_id');
|
||||
|
||||
$ballImage = (string)$item->getData('ball_image');
|
||||
if (!empty($esunData['delete_ball_image'])) {
|
||||
$ballImage = '';
|
||||
}
|
||||
if (!empty($esunData['ball_image_base64'])) {
|
||||
$ballImage = $this->imageUploader->saveBase64Image(
|
||||
(string)$esunData['ball_image_base64'],
|
||||
(string)($esunData['ball_image_name'] ?? 'color-ball.png')
|
||||
);
|
||||
} elseif (!empty($esunData['ball_image'])) {
|
||||
$ballImage = (string)$esunData['ball_image'];
|
||||
}
|
||||
|
||||
$item->addData([
|
||||
'option_id' => $optionId,
|
||||
'display_name' => (string)$row['label'],
|
||||
'swatch_colors' => $this->normalizeSwatchColors((string)($esunData['swatch_colors'] ?? '')),
|
||||
'ball_image' => $ballImage,
|
||||
'is_hot_sale' => !empty($esunData['is_hot_sale']) ? 1 : 0,
|
||||
'status' => 1,
|
||||
'sort_order' => (int)$row['sort_order'],
|
||||
]);
|
||||
$this->colorOptionResource->save($item);
|
||||
}
|
||||
|
||||
private function deleteColorOption(int $optionId): void
|
||||
{
|
||||
$item = $this->colorOptionFactory->create();
|
||||
$this->colorOptionResource->load($item, $optionId, 'option_id');
|
||||
if ($item->getId()) {
|
||||
$this->colorOptionResource->delete($item);
|
||||
}
|
||||
}
|
||||
|
||||
private function normalizeSwatchColors(string $value): string
|
||||
{
|
||||
$parts = preg_split('/[\s,;]+/', trim($value)) ?: [];
|
||||
$colors = [];
|
||||
foreach ($parts as $part) {
|
||||
$color = trim($part);
|
||||
if ($color === '') {
|
||||
continue;
|
||||
}
|
||||
if (!str_starts_with($color, '#')) {
|
||||
$color = '#' . $color;
|
||||
}
|
||||
if (!preg_match('/^#[0-9a-fA-F]{3}([0-9a-fA-F]{3})?$/', $color)) {
|
||||
throw new LocalizedException(__('Invalid swatch color "%1". Use hex values such as #FF0000.', $part));
|
||||
}
|
||||
$colors[] = strtoupper($color);
|
||||
}
|
||||
|
||||
return implode(',', array_values(array_unique($colors)));
|
||||
}
|
||||
|
||||
private function buildLookupKey(string $label, int $sortOrder): string
|
||||
{
|
||||
return strtolower(trim($label)) . '|' . $sortOrder;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Plugin\Adminhtml;
|
||||
|
||||
use Magento\Catalog\Controller\Adminhtml\Product\Save;
|
||||
|
||||
class ProductSaveDetailContentPlugin
|
||||
{
|
||||
private const ATTRIBUTE_CODES = [
|
||||
'esun_industry_applications',
|
||||
'esun_detail_gallery',
|
||||
'esun_creative_video_ids',
|
||||
'esun_product_faqs',
|
||||
'esun_certification_downloads',
|
||||
'esun_mix_step_prices',
|
||||
];
|
||||
|
||||
public function beforeExecute(Save $subject): void
|
||||
{
|
||||
$request = $subject->getRequest();
|
||||
$post = $request->getPostValue();
|
||||
if (!is_array($post)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$product = isset($post['product']) && is_array($post['product']) ? $post['product'] : [];
|
||||
foreach (self::ATTRIBUTE_CODES as $attributeCode) {
|
||||
if (array_key_exists($attributeCode, $product)) {
|
||||
$product[$attributeCode] = $this->unwrapNestedValue($product[$attributeCode], $attributeCode);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Every custom dynamic row is scoped to data.product. When its
|
||||
// final row is deleted, Magento omits the field from that scope.
|
||||
// Do not recursively recover the old top-level data-provider copy,
|
||||
// otherwise deleting every row restores the values being deleted.
|
||||
$product[$attributeCode] = [];
|
||||
}
|
||||
|
||||
if ($product) {
|
||||
$post['product'] = $product;
|
||||
$request->setPostValue($post);
|
||||
}
|
||||
}
|
||||
|
||||
private function unwrapNestedValue(mixed $value, string $attributeCode): mixed
|
||||
{
|
||||
if (is_array($value) && array_key_exists($attributeCode, $value) && is_array($value[$attributeCode])) {
|
||||
return $value[$attributeCode];
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Plugin\WeltPixel;
|
||||
|
||||
use WeltPixel\LayeredNavigation\Helper\Data;
|
||||
|
||||
class ForcePriceSliderPlugin
|
||||
{
|
||||
public function afterGetPriceIsSliderMode(Data $subject, mixed $result): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function afterCanShowPriceInput(Data $subject, mixed $result): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Plugin\WeltPixel;
|
||||
|
||||
use Esun\Catalog\Model\ColorOption\HotSaleOptionProvider;
|
||||
use WeltPixel\LayeredNavigation\Block\LayeredNavigation\RenderLayered;
|
||||
|
||||
class HotSaleColorSwatchPlugin
|
||||
{
|
||||
public function __construct(
|
||||
private readonly HotSaleOptionProvider $hotSaleOptionProvider
|
||||
) {
|
||||
}
|
||||
|
||||
public function afterGetSwatchData(RenderLayered $subject, array $result): array
|
||||
{
|
||||
if (($result['attribute_code'] ?? '') !== 'color' || empty($result['options'])) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$hotSaleOptionIds = $this->hotSaleOptionProvider->getOptionIds();
|
||||
if (!$hotSaleOptionIds) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$hotSaleOptionSet = array_fill_keys($hotSaleOptionIds, true);
|
||||
$aggregationOptionSet = [];
|
||||
foreach ($result['options'] as $optionId => $option) {
|
||||
if (($option['custom_style'] ?? '') !== 'disabled') {
|
||||
$aggregationOptionSet[$optionId] = true;
|
||||
}
|
||||
}
|
||||
|
||||
$availableHotSaleOptionSet = array_intersect_key($hotSaleOptionSet, $aggregationOptionSet);
|
||||
$availableHotSaleOptions = array_intersect_key($result['options'], $availableHotSaleOptionSet);
|
||||
if (!$availableHotSaleOptions) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$result['options'] = $availableHotSaleOptions;
|
||||
$result['swatches'] = array_intersect_key($result['swatches'] ?? [], $availableHotSaleOptions);
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Plugin\WeltPixel;
|
||||
|
||||
use Magento\Catalog\Model\Layer\Filter\FilterInterface;
|
||||
use WeltPixel\LayeredNavigation\Block\Navigation\FilterRenderer;
|
||||
|
||||
class PriceStepCountsPlugin
|
||||
{
|
||||
public function beforeRender(FilterRenderer $subject, FilterInterface $filter): array
|
||||
{
|
||||
if ($filter->getRequestVar() !== 'price') {
|
||||
return [$filter];
|
||||
}
|
||||
|
||||
$collection = $filter->getLayer()->getProductCollection();
|
||||
|
||||
if (!method_exists($collection, 'getFacetedData')) {
|
||||
$subject->setData('esun_price_step_ranges', []);
|
||||
return [$filter];
|
||||
}
|
||||
|
||||
$ranges = [];
|
||||
foreach ($collection->getFacetedData('price') as $bucket) {
|
||||
$count = (int)($bucket['count'] ?? 0);
|
||||
if ($count <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$value = (string)($bucket['value'] ?? '');
|
||||
$from = $bucket['from'] ?? null;
|
||||
$to = $bucket['to'] ?? null;
|
||||
|
||||
if (($from === null || $to === null) && str_contains($value, '_')) {
|
||||
[$fromValue, $toValue] = explode('_', $value, 2);
|
||||
$from = $fromValue === '*' ? null : (float)$fromValue;
|
||||
$to = $toValue === '*' ? null : (float)$toValue;
|
||||
}
|
||||
|
||||
if ($from === null && $to === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$ranges[] = [
|
||||
'from' => $from === null ? '' : (float)$from,
|
||||
'to' => $to === null ? '' : (float)$to,
|
||||
'count' => $count,
|
||||
'value' => $value,
|
||||
];
|
||||
}
|
||||
|
||||
usort($ranges, static function (array $left, array $right): int {
|
||||
return (float)$left['from'] <=> (float)$right['from'];
|
||||
});
|
||||
|
||||
$subject->setData('esun_price_step_ranges', $ranges);
|
||||
|
||||
return [$filter];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Plugin\Wishlist;
|
||||
|
||||
use Magento\Customer\Model\Session;
|
||||
use Magento\Wishlist\CustomerData\Wishlist as WishlistSection;
|
||||
use Magento\Wishlist\Model\WishlistFactory;
|
||||
|
||||
class CustomerDataWishlistPlugin
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Session $customerSession,
|
||||
private readonly WishlistFactory $wishlistFactory
|
||||
) {
|
||||
}
|
||||
|
||||
public function aroundGetSectionData(WishlistSection $subject, callable $proceed): array
|
||||
{
|
||||
$customerId = (int) $this->customerSession->getCustomerId();
|
||||
$result = $proceed();
|
||||
$result['product_ids'] = [];
|
||||
|
||||
if (!$customerId) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$wishlist = $this->wishlistFactory->create()->loadByCustomerId($customerId, false);
|
||||
if (!$wishlist->getId()) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$result['product_ids'] = array_values(array_unique(array_map(
|
||||
'intval',
|
||||
$wishlist->getItemCollection()->getColumnValues('product_id')
|
||||
)));
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user