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,65 @@
<?php
declare(strict_types=1);
namespace Esun\Review\Model\Order;
use Magento\Framework\App\ResourceConnection;
use Magento\Sales\Model\ResourceModel\Order\Item\Collection;
use Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory;
class AwaitingReviewProvider
{
public function __construct(
private readonly CollectionFactory $itemCollections,
private readonly ResourceConnection $resource
) {
}
public function getCollection(int $customerId): Collection
{
$collection = $this->itemCollections->create();
$orderTable = $this->resource->getTableName('sales_order');
$reviewTable = $this->resource->getTableName('review');
$detailTable = $this->resource->getTableName('review_detail');
$itemTable = $this->resource->getTableName('sales_order_item');
$connection = $this->resource->getConnection();
$reviewExists = $connection->select()
->from(['review_pending' => $reviewTable], ['review_id'])
->joinInner(
['review_pending_detail' => $detailTable],
'review_pending_detail.review_id = review_pending.review_id',
[]
)
->where('review_pending.entity_pk_value = main_table.product_id')
->where('review_pending_detail.customer_id = ?', $customerId);
$latestEligibleItems = $connection->select()
->from(['eligible_item' => $itemTable], [new \Zend_Db_Expr('MAX(eligible_item.item_id)')])
->joinInner(['eligible_order' => $orderTable], 'eligible_order.entity_id = eligible_item.order_id', [])
->where('eligible_order.customer_id = ?', $customerId)->where('eligible_order.status = ?', 'received')
->where('eligible_item.parent_item_id IS NULL')->where('eligible_item.product_id IS NOT NULL')
->where('(eligible_item.qty_ordered - eligible_item.qty_canceled) > 0')->group('eligible_item.product_id');
$collection->addFieldToSelect('*');
$collection->getSelect()
->joinInner(
['review_order' => $orderTable],
'review_order.entity_id = main_table.order_id',
['order_increment_id' => 'increment_id', 'order_created_at' => 'created_at']
)
->where('review_order.customer_id = ?', $customerId)
->where('review_order.status = ?', 'received')
->where('main_table.parent_item_id IS NULL')
->where('main_table.product_id IS NOT NULL')
->where('(main_table.qty_ordered - main_table.qty_canceled) > 0')
->where('main_table.item_id IN (?)', $latestEligibleItems)
->where('NOT EXISTS (?)', $reviewExists)
->order('review_order.created_at DESC')
->order('main_table.item_id DESC');
return $collection;
}
public function getCount(int $customerId): int
{
return (int)$this->getCollection($customerId)->getSize();
}
}
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
namespace Esun\Review\Model\Order;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\LocalizedException;
use Magento\Sales\Api\OrderItemRepositoryInterface;
class ReviewEligibilityService
{
public function __construct(private readonly OrderItemRepositoryInterface $items, private readonly ResourceConnection $resource)
{
}
public function getItem(int $orderItemId, int $customerId): \Magento\Sales\Api\Data\OrderItemInterface
{
return $this->loadEligibleItem($orderItemId, $customerId, true);
}
public function getItemForLink(int $orderItemId, int $customerId): \Magento\Sales\Api\Data\OrderItemInterface
{
return $this->loadEligibleItem($orderItemId, $customerId, false);
}
private function loadEligibleItem(int $orderItemId, int $customerId, bool $checkExisting): \Magento\Sales\Api\Data\OrderItemInterface
{
$item = $this->items->get($orderItemId);
$order = $item->getOrder();
if ((int)$order->getCustomerId() !== $customerId || (int)$item->getParentItemId() > 0) {
throw new LocalizedException(__('This order item cannot be reviewed.'));
}
if ((float)$item->getQtyOrdered() - (float)$item->getQtyCanceled() <= 0 || !$this->isOrderEligible($order)) {
throw new LocalizedException(__('This order is not yet eligible for review.'));
}
if ($checkExisting && $this->hasReviewed($customerId, (int)$item->getProductId())) {
throw new LocalizedException(__('You have already reviewed this product.'));
}
return $item;
}
public function canReview(\Magento\Sales\Model\Order\Item $item, int $customerId): bool
{
try {
$this->getItem((int)$item->getId(), $customerId);
return true;
} catch (\Throwable) {
return false;
}
}
public function hasReviewed(int $customerId, int $productId): bool
{
$connection = $this->resource->getConnection();
return (bool)$connection->fetchOne($connection->select()->from(['r' => $this->resource->getTableName('review')], ['review_id'])->join(['d' => $this->resource->getTableName('review_detail')], 'd.review_id = r.review_id', [])->where('r.entity_pk_value = ?', $productId)->where('d.customer_id = ?', $customerId)->limit(1));
}
private function isOrderEligible(\Magento\Sales\Model\Order $order): bool
{
return (string)$order->getStatus() === 'received';
}
}