init
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\LegacyOrder\Block\Account;
|
||||
|
||||
use Esun\LegacyOrder\Model\ResourceModel\Order\Collection;
|
||||
use Esun\LegacyOrder\Model\ResourceModel\Order\CollectionFactory;
|
||||
use Magento\Customer\Model\Session;
|
||||
use Magento\Framework\View\Element\Template;
|
||||
|
||||
class History extends Template
|
||||
{
|
||||
private ?Collection $orders = null;
|
||||
|
||||
public function __construct(
|
||||
Template\Context $context,
|
||||
private readonly CollectionFactory $collectionFactory,
|
||||
private readonly Session $customerSession,
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct($context, $data);
|
||||
}
|
||||
|
||||
public function getOrders(): Collection
|
||||
{
|
||||
if ($this->orders === null) {
|
||||
$this->orders = $this->collectionFactory->create()
|
||||
->addFieldToFilter('customer_id', (int)$this->customerSession->getCustomerId())
|
||||
->setOrder('ordered_at', 'DESC');
|
||||
}
|
||||
return $this->orders;
|
||||
}
|
||||
|
||||
public function getViewUrl(int $legacyOrderId): string
|
||||
{
|
||||
return $this->getUrl('legacy_order/account/view', ['id' => $legacyOrderId]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?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)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\LegacyOrder\Block\Account;
|
||||
|
||||
use Magento\Customer\Model\Session;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Framework\Serialize\Serializer\Json;
|
||||
use Magento\Framework\View\Element\Template;
|
||||
|
||||
class OrderView extends Template
|
||||
{
|
||||
/** @var array<string, mixed>|null */
|
||||
private ?array $order = null;
|
||||
|
||||
public function __construct(
|
||||
Template\Context $context,
|
||||
private readonly Session $customerSession,
|
||||
private readonly ResourceConnection $resource,
|
||||
private readonly Json $json,
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct($context, $data);
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function getOrder(): array
|
||||
{
|
||||
if ($this->order !== null) {
|
||||
return $this->order;
|
||||
}
|
||||
$connection = $this->resource->getConnection();
|
||||
return $this->order = $connection->fetchRow(
|
||||
$connection->select()
|
||||
->from($this->resource->getTableName('esun_legacy_order'))
|
||||
->where('legacy_order_id = ?', (int)$this->getRequest()->getParam('id'))
|
||||
->where('customer_id = ?', (int)$this->customerSession->getCustomerId())
|
||||
) ?: [];
|
||||
}
|
||||
|
||||
/** @return array<int, array<string, mixed>> */
|
||||
public function getItems(): array
|
||||
{
|
||||
$order = $this->getOrder();
|
||||
if (!$order) {
|
||||
return [];
|
||||
}
|
||||
$connection = $this->resource->getConnection();
|
||||
return $connection->fetchAll(
|
||||
$connection->select()
|
||||
->from($this->resource->getTableName('esun_legacy_order_item'))
|
||||
->where('legacy_order_id = ?', (int)$order['legacy_order_id'])
|
||||
->order('line_number ASC')
|
||||
);
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function decodeAddress(?string $value): array
|
||||
{
|
||||
try {
|
||||
return $value ? (array)$this->json->unserialize($value) : [];
|
||||
} catch (\Throwable) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user