102 lines
3.2 KiB
PHP
102 lines
3.2 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Lts\HomePage\Block;
|
|
|
|
use Magento\Catalog\Helper\Image as ImageHelper;
|
|
use Magento\Customer\Model\Session as CustomerSession;
|
|
use Magento\Framework\View\Element\Template;
|
|
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory as OrderCollectionFactory;
|
|
use Magento\Wishlist\Model\WishlistFactory;
|
|
|
|
class CustomerDashboard extends Template
|
|
{
|
|
public function __construct(
|
|
Template\Context $context,
|
|
private readonly CustomerSession $customerSession,
|
|
private readonly OrderCollectionFactory $orderCollectionFactory,
|
|
private readonly WishlistFactory $wishlistFactory,
|
|
private readonly ImageHelper $imageHelper,
|
|
array $data = []
|
|
) {
|
|
parent::__construct($context, $data);
|
|
}
|
|
|
|
protected function _prepareLayout(): self
|
|
{
|
|
if ($this->isLoggedIn()) {
|
|
$this->pageConfig->addBodyClass('lts-customer-logged-in');
|
|
}
|
|
return parent::_prepareLayout();
|
|
}
|
|
|
|
public function isLoggedIn(): bool
|
|
{
|
|
return $this->customerSession->isLoggedIn();
|
|
}
|
|
|
|
public function getCustomerName(): string
|
|
{
|
|
return trim((string) $this->customerSession->getCustomer()->getName());
|
|
}
|
|
|
|
public function getRecentOrders()
|
|
{
|
|
return $this->orderCollectionFactory->create()
|
|
->addFieldToFilter('customer_id', (int) $this->customerSession->getCustomerId())
|
|
->setOrder('created_at', 'DESC')
|
|
->setPageSize(2);
|
|
}
|
|
|
|
public function getWishlistItems(): array
|
|
{
|
|
$wishlist = $this->wishlistFactory->create()
|
|
->loadByCustomerId((int) $this->customerSession->getCustomerId(), true);
|
|
$items = [];
|
|
foreach ($wishlist->getItemCollection() as $item) {
|
|
$items[] = $item;
|
|
if (count($items) === 3) {
|
|
break;
|
|
}
|
|
}
|
|
return $items;
|
|
}
|
|
|
|
public function getOrderImages($order): array
|
|
{
|
|
$images = [];
|
|
foreach ($order->getAllVisibleItems() as $item) {
|
|
$product = $item->getProduct();
|
|
if ($product && $product->getId()) {
|
|
$images[] = $this->imageHelper->init($product, 'product_thumbnail_image')
|
|
->resize(42, 42)
|
|
->getUrl();
|
|
}
|
|
if (count($images) === 2) {
|
|
break;
|
|
}
|
|
}
|
|
return $images;
|
|
}
|
|
|
|
public function getBranchName(): string
|
|
{
|
|
$address = $this->customerSession->getCustomer()->getDefaultBillingAddress();
|
|
$city = $address ? trim((string) $address->getCity()) : '';
|
|
return $city !== '' ? $city . ' Branch' : (string) __('My Local Branch');
|
|
}
|
|
|
|
public function getBranchAddress(): string
|
|
{
|
|
$address = $this->customerSession->getCustomer()->getDefaultBillingAddress();
|
|
if (!$address) {
|
|
return (string) __('Add a default billing address in My Account');
|
|
}
|
|
$parts = array_filter([
|
|
implode(' ', (array) $address->getStreet()),
|
|
trim(implode(' ', [(string) $address->getCity(), (string) $address->getRegion(), (string) $address->getPostcode()]))
|
|
]);
|
|
return implode(', ', $parts);
|
|
}
|
|
}
|