123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- /**
- * @author Magento Core Team <core@magentocommerce.com>
- */
- namespace Magento\Wishlist\Block\Customer;
- /**
- * Wishlist block customer items.
- *
- * @api
- * @since 100.0.2
- */
- class Wishlist extends \Magento\Wishlist\Block\AbstractBlock
- {
- /**
- * List of product options rendering configurations by product type
- *
- * @var array
- */
- protected $_optionsCfg = [];
- /**
- * @var \Magento\Catalog\Helper\Product\ConfigurationPool
- */
- protected $_helperPool;
- /**
- * @var \Magento\Wishlist\Model\ResourceModel\Item\Collection
- * @since 101.1.1
- */
- protected $_collection;
- /**
- * @var \Magento\Customer\Helper\Session\CurrentCustomer
- */
- protected $currentCustomer;
- /**
- * @var \Magento\Framework\Data\Helper\PostHelper
- */
- protected $postDataHelper;
- /**
- * @param \Magento\Catalog\Block\Product\Context $context
- * @param \Magento\Framework\App\Http\Context $httpContext
- * @param \Magento\Catalog\Helper\Product\ConfigurationPool $helperPool
- * @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer
- * @param \Magento\Framework\Data\Helper\PostHelper $postDataHelper
- * @param array $data
- */
- public function __construct(
- \Magento\Catalog\Block\Product\Context $context,
- \Magento\Framework\App\Http\Context $httpContext,
- \Magento\Catalog\Helper\Product\ConfigurationPool $helperPool,
- \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
- \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
- array $data = []
- ) {
- parent::__construct(
- $context,
- $httpContext,
- $data
- );
- $this->_helperPool = $helperPool;
- $this->currentCustomer = $currentCustomer;
- $this->postDataHelper = $postDataHelper;
- }
- /**
- * Add wishlist conditions to collection
- *
- * @param \Magento\Wishlist\Model\ResourceModel\Item\Collection $collection
- * @return $this
- */
- protected function _prepareCollection($collection)
- {
- $collection->setInStockFilter(true)->setOrder('added_at', 'ASC');
- return $this;
- }
- /**
- * Paginate Wishlist Product Items collection
- *
- * @return void
- * @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
- */
- private function paginateCollection()
- {
- $page = $this->getRequest()->getParam("p", 1);
- $limit = $this->getRequest()->getParam("limit", 10);
- $this->_collection
- ->setPageSize($limit)
- ->setCurPage($page);
- }
- /**
- * Retrieve Wishlist Product Items collection
- *
- * @return \Magento\Wishlist\Model\ResourceModel\Item\Collection
- * @since 101.1.1
- */
- public function getWishlistItems()
- {
- if ($this->_collection === null) {
- $this->_collection = $this->_createWishlistItemCollection();
- $this->_prepareCollection($this->_collection);
- $this->paginateCollection();
- }
- return $this->_collection;
- }
- /**
- * Preparing global layout
- *
- * @return $this
- */
- protected function _prepareLayout()
- {
- parent::_prepareLayout();
- $this->pageConfig->getTitle()->set(__('My Wish List'));
- $this->getChildBlock('wishlist_item_pager')
- ->setUseContainer(
- true
- )->setShowAmounts(
- true
- )->setFrameLength(
- $this->_scopeConfig->getValue(
- 'design/pagination/pagination_frame',
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE
- )
- )->setJump(
- $this->_scopeConfig->getValue(
- 'design/pagination/pagination_frame_skip',
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE
- )
- )->setLimit(
- $this->getLimit()
- )
- ->setCollection($this->getWishlistItems());
- return $this;
- }
- /**
- * Retrieve Back URL
- *
- * @return string
- */
- public function getBackUrl()
- {
- return $this->getUrl('customer/account/');
- }
- /**
- * Sets all options render configurations
- *
- * @param null|array $optionCfg
- * @return $this
- */
- public function setOptionsRenderCfgs($optionCfg)
- {
- $this->_optionsCfg = $optionCfg;
- return $this;
- }
- /**
- * Returns all options render configurations
- *
- * @return array
- */
- public function getOptionsRenderCfgs()
- {
- return $this->_optionsCfg;
- }
- /**
- * Adds config for rendering product type options
- *
- * @param string $productType
- * @param string $helperName
- * @param null|string $template
- * @return $this
- */
- public function addOptionsRenderCfg($productType, $helperName, $template = null)
- {
- $this->_optionsCfg[$productType] = ['helper' => $helperName, 'template' => $template];
- return $this;
- }
- /**
- * Returns html for showing item options
- *
- * @param string $productType
- * @return array|null
- */
- public function getOptionsRenderCfg($productType)
- {
- if (isset($this->_optionsCfg[$productType])) {
- return $this->_optionsCfg[$productType];
- } elseif (isset($this->_optionsCfg['default'])) {
- return $this->_optionsCfg['default'];
- } else {
- return null;
- }
- }
- /**
- * Returns html for showing item options
- *
- * @param \Magento\Wishlist\Model\Item $item
- * @return string
- */
- public function getDetailsHtml(\Magento\Wishlist\Model\Item $item)
- {
- $cfg = $this->getOptionsRenderCfg($item->getProduct()->getTypeId());
- if (!$cfg) {
- return '';
- }
- $block = $this->getChildBlock('item_options');
- if (!$block) {
- return '';
- }
- if ($cfg['template']) {
- $template = $cfg['template'];
- } else {
- $cfgDefault = $this->getOptionsRenderCfg('default');
- if (!$cfgDefault) {
- return '';
- }
- $template = $cfgDefault['template'];
- }
- $block->setTemplate($template);
- $block->setOptionList($this->_helperPool->get($cfg['helper'])->getOptions($item));
- return $block->toHtml();
- }
- /**
- * Returns qty to show visually to user
- *
- * @param \Magento\Wishlist\Model\Item $item
- * @return float
- */
- public function getAddToCartQty(\Magento\Wishlist\Model\Item $item)
- {
- $qty = $this->getQty($item);
- return $qty ? $qty : 1;
- }
- /**
- * Get add all to cart params for POST request
- *
- * @return string
- */
- public function getAddAllToCartParams()
- {
- return $this->postDataHelper->getPostData(
- $this->getUrl('wishlist/index/allcart'),
- ['wishlist_id' => $this->getWishlistInstance()->getId()]
- );
- }
- /**
- * @inheritdoc
- */
- protected function _toHtml()
- {
- if ($this->currentCustomer->getCustomerId()) {
- return parent::_toHtml();
- } else {
- return '';
- }
- }
- }
|