This commit is contained in:
2026-09-15 10:35:56 +08:00
commit 782f8ea7aa
4086 changed files with 631288 additions and 0 deletions
@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Observer\Adminhtml;
use Esun\Catalog\Setup\Patch\Data\AddMixStepPricesAttribute;
use Magento\Catalog\Model\Product;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class SaveMixStepPrices implements ObserverInterface
{
public function execute(Observer $observer): void
{
$product = $observer->getEvent()->getProduct();
$controller = $observer->getEvent()->getController();
if (!$product instanceof Product
|| $product->getTypeId() !== Configurable::TYPE_CODE
|| !$controller
) {
return;
}
$post = $controller->getRequest()->getPostValue();
$productData = is_array($post['product'] ?? null) ? $post['product'] : [];
$attributeCode = AddMixStepPricesAttribute::ATTRIBUTE_CODE;
if (!array_key_exists($attributeCode, $productData)) {
return;
}
$value = $this->unwrapNestedValue($productData[$attributeCode], $attributeCode);
$attribute = $product->getResource()->getAttribute($attributeCode);
if (!$attribute) {
return;
}
$product->setData($attributeCode, is_array($value) ? $value : []);
$attribute->getBackend()->beforeSave($product);
$product->getResource()->saveAttribute($product, $attributeCode);
}
private function unwrapNestedValue(mixed $value, string $attributeCode): mixed
{
if (is_array($value) && isset($value[$attributeCode]) && is_array($value[$attributeCode])) {
return $value[$attributeCode];
}
return $value;
}
}
@@ -0,0 +1,89 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Observer\Adminhtml;
use Esun\Catalog\Model\ProductRecommendation\RecommendationRepository;
use Esun\Catalog\Model\ProductRecommendation\WebsiteResolver;
use Esun\Catalog\Model\ProductRecommendation\Writer;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
class SaveProductRecommendations implements ObserverInterface
{
public function __construct(
private readonly ProductRepositoryInterface $productRepository,
private readonly RecommendationRepository $recommendationRepository,
private readonly WebsiteResolver $websiteResolver,
private readonly Writer $writer
) {
}
public function execute(Observer $observer): void
{
$product = $observer->getEvent()->getProduct();
$controller = $observer->getEvent()->getController();
if (!$product instanceof Product || !$product->getId() || !$controller) {
return;
}
$post = $controller->getRequest()->getPostValue();
$productData = is_array($post['product'] ?? null) ? $post['product'] : [];
$useDefaults = (array)($post['use_default'] ?? []);
$field = 'esun_recommendations';
if (!array_key_exists($field, $productData) && !array_key_exists($field, $useDefaults)) {
return;
}
$store = $product->getStore();
$websiteId = (int)$store->getId() > 0 ? (int)$store->getWebsiteId() : 0;
if ($websiteId > 0 && !empty($useDefaults[$field])) {
$this->recommendationRepository->delete((int)$product->getId(), $websiteId);
$this->writer->invalidateCache();
return;
}
$groups = [[
'product_id' => (int)$product->getId(),
'website_id' => $websiteId,
'product_ids' => $this->resolveSkuText(
(string)($productData[$field] ?? ''),
$product,
$websiteId,
$websiteId > 0 ? (string)$store->getWebsite()->getCode() : 'default'
),
]];
$this->writer->replaceGroups($groups);
}
private function resolveSkuText(string $value, Product $source, int $websiteId, string $websiteAlias): array
{
$skus = array_filter(array_map('trim', preg_split('/[\r\n,;]+/', $value) ?: []),
static fn(string $sku): bool => $sku !== '');
$ids = [];
foreach ($skus as $sku) {
try {
$recommended = $this->productRepository->get((string)$sku, false, 0);
} catch (NoSuchEntityException) {
throw new LocalizedException(__('Recommended SKU "%1" does not exist.', $sku));
}
if ((int)$recommended->getId() === (int)$source->getId()) {
throw new LocalizedException(__('A product cannot recommend itself: %1.', $sku));
}
if ($websiteId > 0
&& !in_array($websiteId, array_map('intval', (array)$recommended->getWebsiteIds()), true)
) {
throw new LocalizedException(
__('Recommended SKU "%1" is not assigned to website "%2".', $sku, $websiteAlias)
);
}
if (in_array((int)$recommended->getId(), $ids, true)) {
throw new LocalizedException(__('Recommended SKU "%1" is duplicated.', $sku));
}
$ids[] = (int)$recommended->getId();
}
return $ids;
}
}
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Observer;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\CatalogInventory\Helper\Stock;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class InStockProductListFilter implements ObserverInterface
{
public function __construct(
private readonly RequestInterface $request,
private readonly Stock $stockHelper
) {
}
public function execute(Observer $observer): void
{
if ((string) $this->request->getParam('in_stock') !== '1') {
return;
}
$collection = $observer->getData('collection');
if ($collection instanceof Collection) {
$this->stockHelper->addIsInStockFilterToCollection($collection);
}
}
}