Files
2026-09-15 10:35:56 +08:00

45 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\Reward\Model\Config\Backend;
use Magento\Config\Model\Config\Backend\Serialized\ArraySerialized;
use Magento\Config\Model\ResourceModel\Config\Data as ConfigResource;
use Magento\Config\Model\ResourceModel\Config\Data\Collection as ConfigCollection;
class EarningRuleItems extends ArraySerialized
{
protected function _construct(): void
{
$this->_setResourceModel(ConfigResource::class, ConfigCollection::class);
}
public function beforeSave()
{
$items = [];
$allowedCodes = [
'registration', 'first_shipping_address', 'order_earned',
'review_text', 'review_media', 'social_share',
];
$value = $this->getValue();
if (is_array($value)) {
unset($value['__empty']);
foreach ($value as $row) {
if (!is_array($row)) {
continue;
}
$code = strtolower(trim((string)($row['code'] ?? '')));
$points = max(0.0, (float)($row['points'] ?? 0));
$name = trim((string)($row['name'] ?? ''));
$reason = trim((string)($row['reason'] ?? ''));
if (!in_array($code, $allowedCodes, true) || $name === '') {
continue;
}
$items[$code] = ['code' => $code, 'points' => $points, 'name' => $name, 'reason' => $reason];
}
}
$this->setValue(array_values($items));
return parent::beforeSave();
}
}