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

69 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\LegacyOrder\Block\Account;
use Magento\Customer\Block\Account\SortLinkInterface;
use Magento\Customer\Model\Session;
use Magento\Framework\App\DefaultPathInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\View\Element\Html\Link\Current;
use Magento\Framework\View\Element\Template\Context;
class NavigationLink extends Current implements SortLinkInterface
{
private ?bool $hasLegacyOrders = null;
public function __construct(
Context $context,
DefaultPathInterface $defaultPath,
private readonly Session $customerSession,
private readonly ResourceConnection $resource,
array $data = []
) {
parent::__construct($context, $defaultPath, $data);
}
public function getSortOrder(): int
{
return (int)$this->getData(self::SORT_ORDER);
}
public function isCurrent(): bool
{
if (
$this->getRequest()->getModuleName() === 'legacy_order'
&& $this->getRequest()->getControllerName() === 'account'
) {
return true;
}
return parent::isCurrent();
}
protected function _toHtml(): string
{
return $this->hasLegacyOrders() ? parent::_toHtml() : '';
}
private function hasLegacyOrders(): bool
{
if ($this->hasLegacyOrders !== null) {
return $this->hasLegacyOrders;
}
$customerId = (int)$this->customerSession->getCustomerId();
if ($customerId <= 0) {
return $this->hasLegacyOrders = false;
}
$connection = $this->resource->getConnection();
return $this->hasLegacyOrders = (bool)$connection->fetchOne(
$connection->select()
->from($this->resource->getTableName('esun_legacy_order'), ['legacy_order_id'])
->where('customer_id = ?', $customerId)
->limit(1)
);
}
}