Files
stemcell/app/code/Stemcell/Customer/Block/Address/Book.php
T
2026-09-15 10:26:34 +08:00

95 lines
2.8 KiB
PHP

<?php
namespace Stemcell\Customer\Block\Address;
use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Block\Address\Grid;
use Magento\Customer\Helper\Session\CurrentCustomer;
use Magento\Customer\Model\Address\Config;
use Magento\Customer\Model\Address\Mapper;
use Magento\Customer\Model\ResourceModel\Address\Collection;
use Magento\Customer\Model\ResourceModel\Address\CollectionFactory;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\View\Element\Template\Context;
use Magento\Theme\Block\Html\Pager;
/**
* @api
*/
class Book extends \Magento\Customer\Block\Address\Book
{
/**
* @var Collection
*/
protected $addressCollection;
/**
* @var CollectionFactory
*/
protected CollectionFactory $addressCollectionFactory;
public function __construct(
Context $context,
AddressRepositoryInterface $addressRepository,
CurrentCustomer $currentCustomer,
Config $addressConfig,
Mapper $addressMapper,
CollectionFactory $addressCollectionFactory,
CustomerRepositoryInterface $customerRepository = null,
array $data = [],
Grid $addressesGrid = null
) {
parent::__construct(
$context,
$customerRepository,
$addressRepository,
$currentCustomer,
$addressConfig,
$addressMapper,
$data,
$addressesGrid
);
$this->addressCollectionFactory = $addressCollectionFactory;
}
/**
* @throws NoSuchEntityException
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function _prepareLayout(): Book
{
parent::_prepareLayout();
$pager = $this->getChildBlock('pager');
if ($pager===false) {
$pager = $this->getLayout()->createBlock(Pager::class, 'customer.addresses.pager');
$this->setChild('pager', $pager);
}
$pager->setCollection($this->getAddressCollection());
return $this;
}
/**
* Get customer addresses collection.
*
* Filters collection by customer id
*
* @return Collection
* @throws NoSuchEntityException
*/
public function getAddressCollection(): Collection
{
if (null === $this->addressCollection) {
if (null === $this->getCustomer()) {
throw new NoSuchEntityException(__('Customer not logged in'));
}
$collection = $this->addressCollectionFactory->create();
$collection->setOrder('entity_id', 'desc');
$collection->setCustomerFilter([$this->getCustomer()->getId()]);
$this->addressCollection = $collection;
}
return $this->addressCollection;
}
}