init
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Reward\Block\Adminhtml\Customer;
|
||||
|
||||
use Esun\Reward\Model\BalanceManager;
|
||||
use Esun\Reward\Model\TierResolver;
|
||||
use Magento\Backend\Block\Template;
|
||||
use Magento\Customer\Api\CustomerRepositoryInterface;
|
||||
use Esun\Reward\Model\Config;
|
||||
|
||||
class Reward extends Template
|
||||
{
|
||||
protected $_template = 'Esun_Reward::customer/reward.phtml';
|
||||
public function __construct(Template\Context $context, private readonly CustomerRepositoryInterface $customers, private readonly BalanceManager $balanceManager, private readonly TierResolver $tierResolver, private readonly Config $config, array $data = []) { parent::__construct($context, $data); }
|
||||
public function getCustomerId(): int { return (int)$this->getRequest()->getParam('id'); }
|
||||
public function getRewardData(): array
|
||||
{
|
||||
$customerId = $this->getCustomerId(); if ($customerId <= 0) { return []; }
|
||||
$customer = $this->customers->getById($customerId); $account = $this->balanceManager->getAccount($customerId, (int)$customer->getWebsiteId());
|
||||
$account['tier'] = $this->tierResolver->resolveEffective((string)$account['tier_code'], (float)$account['lifetime_spend_base']); return $account;
|
||||
}
|
||||
public function getTransactions(): array { return $this->getCustomerId() > 0 ? $this->balanceManager->getTransactions($this->getCustomerId(), 20) : []; }
|
||||
public function getAdjustUrl(): string { return $this->getUrl('esun_reward/customer/adjust'); }
|
||||
public function getSocialPoints(): int { return $this->config->socialPoints(); }
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Reward\Block\Adminhtml\System\Config\Form\Field;
|
||||
|
||||
use Magento\Framework\View\Element\Context;
|
||||
use Magento\Framework\View\Element\Html\Select;
|
||||
|
||||
class EarningEvent extends Select
|
||||
{
|
||||
public function __construct(Context $context, array $data = [])
|
||||
{
|
||||
parent::__construct($context, $data);
|
||||
$this->setOptions([
|
||||
['value' => 'registration', 'label' => __('Customer Registration')],
|
||||
['value' => 'first_shipping_address', 'label' => __('First Shipping Address')],
|
||||
['value' => 'order_earned', 'label' => __('Paid Order')],
|
||||
['value' => 'review_text', 'label' => __('Approved Text Review')],
|
||||
['value' => 'review_media', 'label' => __('Approved Review with Media')],
|
||||
['value' => 'social_share', 'label' => __('Social Share')],
|
||||
]);
|
||||
}
|
||||
|
||||
public function setInputName(string $value): self
|
||||
{
|
||||
return $this->setName($value);
|
||||
}
|
||||
|
||||
public function setInputId(string $value): self
|
||||
{
|
||||
return $this->setId($value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Reward\Block\Adminhtml\System\Config\Form\Field;
|
||||
|
||||
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
|
||||
use Magento\Framework\Data\Form\Element\AbstractElement;
|
||||
|
||||
class EarningRuleItems extends AbstractFieldArray
|
||||
{
|
||||
private ?EarningEvent $eventRenderer = null;
|
||||
|
||||
public function render(AbstractElement $element): string
|
||||
{
|
||||
$inheritRequired = $this->_isInheritCheckboxRequired($element);
|
||||
if ($element->getInherit() == 1 && $inheritRequired) {
|
||||
$element->setDisabled(true);
|
||||
}
|
||||
if ($element->getIsDisableInheritance()) {
|
||||
$element->setReadonly(true);
|
||||
}
|
||||
$html = '<td class="value" colspan="2">' . $this->_getElementHtml($element);
|
||||
if ($element->getComment()) {
|
||||
$html .= '<p class="note"><span>' . $element->getComment() . '</span></p>';
|
||||
}
|
||||
$html .= '</td>';
|
||||
if ($inheritRequired) {
|
||||
$html .= $this->_renderInheritCheckbox($element);
|
||||
}
|
||||
return $this->_decorateRowHtml($element, $html);
|
||||
}
|
||||
|
||||
protected function _prepareToRender(): void
|
||||
{
|
||||
$this->addColumn('code', [
|
||||
'label' => __('Trigger Event'),
|
||||
'renderer' => $this->getEventRenderer(),
|
||||
]);
|
||||
$this->addColumn('points', [
|
||||
'label' => __('Reward Points'),
|
||||
'class' => 'validate-number validate-zero-or-greater',
|
||||
'style' => 'width:130px',
|
||||
]);
|
||||
$this->addColumn('name', ['label' => __('Name'), 'style' => 'width:220px']);
|
||||
$this->addColumn('reason', ['label' => __('Reason'), 'style' => 'width:320px']);
|
||||
$this->_addAfter = false;
|
||||
$this->_addButtonLabel = __('Add Supported Rule');
|
||||
}
|
||||
|
||||
protected function _prepareArrayRow(\Magento\Framework\DataObject $row): void
|
||||
{
|
||||
$code = (string)$row->getData('code');
|
||||
$row->setData('option_extra_attrs', [
|
||||
'option_' . $this->getEventRenderer()->calcOptionHash($code) => 'selected="selected"',
|
||||
]);
|
||||
}
|
||||
|
||||
private function getEventRenderer(): EarningEvent
|
||||
{
|
||||
if ($this->eventRenderer === null) {
|
||||
$this->eventRenderer = $this->getLayout()->createBlock(
|
||||
EarningEvent::class,
|
||||
'',
|
||||
['data' => ['is_render_to_js_template' => true]]
|
||||
);
|
||||
}
|
||||
return $this->eventRenderer;
|
||||
}
|
||||
|
||||
public function getArrayRows(): array
|
||||
{
|
||||
$element = $this->getElement();
|
||||
if (!$element->getValue()) {
|
||||
$element->setValue([
|
||||
'_registration' => ['code' => 'registration', 'points' => '100', 'name' => 'Account Registration', 'reason' => 'Account registration reward'],
|
||||
'_first_address' => ['code' => 'first_shipping_address', 'points' => '100', 'name' => 'First Shipping Address', 'reason' => 'First saved shipping address reward'],
|
||||
'_order' => ['code' => 'order_earned', 'points' => '1', 'name' => 'Order Reward', 'reason' => 'Points earned from order %1'],
|
||||
'_review_text' => ['code' => 'review_text', 'points' => '20', 'name' => 'Product Review', 'reason' => 'Approved product review reward'],
|
||||
'_review_media' => ['code' => 'review_media', 'points' => '40', 'name' => 'Product Review with Media', 'reason' => 'Approved product review reward'],
|
||||
'_social' => ['code' => 'social_share', 'points' => '300', 'name' => 'Social Share', 'reason' => 'Social share reward'],
|
||||
]);
|
||||
}
|
||||
return parent::getArrayRows();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Reward\Block\Adminhtml\System\Config\Form\Field;
|
||||
|
||||
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
|
||||
use Magento\Framework\Data\Form\Element\AbstractElement;
|
||||
|
||||
class TierItems extends AbstractFieldArray
|
||||
{
|
||||
public function render(AbstractElement $element): string
|
||||
{
|
||||
$isCheckboxRequired = $this->_isInheritCheckboxRequired($element);
|
||||
if ($element->getInherit() == 1 && $isCheckboxRequired) {
|
||||
$element->setDisabled(true);
|
||||
}
|
||||
if ($element->getIsDisableInheritance()) {
|
||||
$element->setReadonly(true);
|
||||
}
|
||||
|
||||
$html = '<td class="value" colspan="2">' . $this->_getElementHtml($element);
|
||||
if ($element->getComment()) {
|
||||
$html .= '<p class="note"><span>' . $element->getComment() . '</span></p>';
|
||||
}
|
||||
$html .= '</td>';
|
||||
if ($isCheckboxRequired) {
|
||||
$html .= $this->_renderInheritCheckbox($element);
|
||||
}
|
||||
|
||||
return $this->_decorateRowHtml($element, $html);
|
||||
}
|
||||
|
||||
protected function _prepareToRender(): void
|
||||
{
|
||||
$this->addColumn('label', ['label' => __('Tier Name'), 'style' => 'width:220px']);
|
||||
$this->addColumn('threshold', [
|
||||
'label' => __('Lifetime Spend Threshold'),
|
||||
'class' => 'validate-number validate-zero-or-greater',
|
||||
'style' => 'width:220px',
|
||||
]);
|
||||
$this->addColumn('multiplier', [
|
||||
'label' => __('Points Multiplier'),
|
||||
'class' => 'validate-number validate-greater-than-zero',
|
||||
'style' => 'width:160px',
|
||||
]);
|
||||
$this->_addAfter = false;
|
||||
$this->_addButtonLabel = __('Add Tier');
|
||||
}
|
||||
|
||||
public function getArrayRows(): array
|
||||
{
|
||||
$element = $this->getElement();
|
||||
if (!$element->getValue()) {
|
||||
$element->setValue([
|
||||
'_bronze' => ['label' => 'Bronze Maker', 'threshold' => '0', 'multiplier' => '1'],
|
||||
'_silver' => ['label' => 'Silver Maker', 'threshold' => '3000', 'multiplier' => '1.2'],
|
||||
'_gold' => ['label' => 'Gold Maker', 'threshold' => '7000', 'multiplier' => '1.5'],
|
||||
]);
|
||||
}
|
||||
|
||||
return parent::getArrayRows();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user