init
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Reward\Observer;
|
||||
|
||||
use Esun\Reward\Model\BalanceManager;
|
||||
use Esun\Reward\Model\Config;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Framework\Event\Observer;
|
||||
use Magento\Framework\Event\ObserverInterface;
|
||||
use Magento\Review\Model\Review;
|
||||
use Magento\Customer\Api\CustomerRepositoryInterface;
|
||||
|
||||
class AwardApprovedReviewPoints implements ObserverInterface
|
||||
{
|
||||
public function __construct(private readonly BalanceManager $balanceManager, private readonly Config $config, private readonly ResourceConnection $resource, private readonly CustomerRepositoryInterface $customers) {}
|
||||
public function execute(Observer $observer): void
|
||||
{
|
||||
/** @var Review|null $review */ $review = $observer->getEvent()->getDataObject();
|
||||
$customerId = (int)($review?->getCustomerId() ?? 0);
|
||||
if (!$review || (int)$review->getStatusId() !== Review::STATUS_APPROVED || $customerId <= 0 || !$this->config->isEnabled((int)$review->getStoreId())) { return; }
|
||||
$connection = $this->resource->getConnection();
|
||||
$purchase = $connection->fetchRow($connection->select()->from($this->resource->getTableName('esun_review_purchase'))->where('review_id = ?', (int)$review->getId())->limit(1));
|
||||
if (!$purchase) { return; }
|
||||
$hasMedia = (bool)$connection->fetchOne($connection->select()->from($this->resource->getTableName('esun_review_image'), ['image_id'])->where('review_id = ?', (int)$review->getId())->where('status = ?', 1)->limit(1));
|
||||
$type = $hasMedia ? 'review_media' : 'review_text';
|
||||
$rule = $this->config->earningRule($type, (int)$review->getStoreId());
|
||||
$points = (int)$rule['points'];
|
||||
$websiteId = (int)$this->customers->getById($customerId)->getWebsiteId();
|
||||
$this->balanceManager->change($customerId, $websiteId, $points, $type, 'review_reward:' . (int)$review->getId(), [
|
||||
'reference_type' => 'review', 'reference_id' => (int)$review->getId(), 'reason' => (string)__($rule['reason']),
|
||||
'metadata' => ['product_id' => (int)$review->getEntityPkValue(), 'order_id' => (int)$purchase['order_id'], 'order_item_id' => (int)$purchase['order_item_id'], 'has_media' => $hasMedia]
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user