123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Checkout\Block\Cart\Item;
- use Magento\Catalog\Pricing\Price\ConfiguredPriceInterface;
- use Magento\Checkout\Block\Cart\Item\Renderer\Actions;
- use Magento\Framework\Pricing\PriceCurrencyInterface;
- use Magento\Framework\View\Element\AbstractBlock;
- use Magento\Framework\View\Element\Message\InterpretationStrategyInterface;
- use Magento\Quote\Model\Quote\Item\AbstractItem;
- use Magento\Framework\App\ObjectManager;
- use Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface;
- /**
- * Shopping cart item render block
- *
- * @api
- * @author Magento Core Team <core@magentocommerce.com>
- *
- * @method \Magento\Checkout\Block\Cart\Item\Renderer setProductName(string)
- * @method \Magento\Checkout\Block\Cart\Item\Renderer setDeleteUrl(string)
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
- * @since 100.0.2
- */
- class Renderer extends \Magento\Framework\View\Element\Template implements
- \Magento\Framework\DataObject\IdentityInterface
- {
- /**
- * @var \Magento\Checkout\Model\Session
- */
- protected $_checkoutSession;
- /**
- * @var AbstractItem
- */
- protected $_item;
- /**
- * @var string
- */
- protected $_productUrl;
- /**
- * Whether qty will be converted to number
- *
- * @var bool
- */
- protected $_strictQtyMode = true;
- /**
- * Check, whether product URL rendering should be ignored
- *
- * @var bool
- */
- protected $_ignoreProductUrl = false;
- /**
- * Catalog product configuration
- *
- * @var \Magento\Catalog\Helper\Product\Configuration
- */
- protected $_productConfig = null;
- /**
- * @var \Magento\Framework\Url\Helper\Data
- */
- protected $_urlHelper;
- /**
- * @var \Magento\Framework\Message\ManagerInterface
- */
- protected $messageManager;
- /**
- * @var \Magento\Catalog\Block\Product\ImageBuilder
- */
- protected $imageBuilder;
- /**
- * @var PriceCurrencyInterface
- */
- protected $priceCurrency;
- /**
- * @var \Magento\Framework\Module\Manager
- */
- public $moduleManager;
- /**
- * @var InterpretationStrategyInterface
- */
- private $messageInterpretationStrategy;
- /** @var ItemResolverInterface */
- private $itemResolver;
- /**
- * @param \Magento\Framework\View\Element\Template\Context $context
- * @param \Magento\Catalog\Helper\Product\Configuration $productConfig
- * @param \Magento\Checkout\Model\Session $checkoutSession
- * @param \Magento\Catalog\Block\Product\ImageBuilder $imageBuilder
- * @param \Magento\Framework\Url\Helper\Data $urlHelper
- * @param \Magento\Framework\Message\ManagerInterface $messageManager
- * @param PriceCurrencyInterface $priceCurrency
- * @param \Magento\Framework\Module\Manager $moduleManager
- * @param InterpretationStrategyInterface $messageInterpretationStrategy
- * @param array $data
- * @param ItemResolverInterface|null $itemResolver
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- * @codeCoverageIgnore
- */
- public function __construct(
- \Magento\Framework\View\Element\Template\Context $context,
- \Magento\Catalog\Helper\Product\Configuration $productConfig,
- \Magento\Checkout\Model\Session $checkoutSession,
- \Magento\Catalog\Block\Product\ImageBuilder $imageBuilder,
- \Magento\Framework\Url\Helper\Data $urlHelper,
- \Magento\Framework\Message\ManagerInterface $messageManager,
- PriceCurrencyInterface $priceCurrency,
- \Magento\Framework\Module\Manager $moduleManager,
- InterpretationStrategyInterface $messageInterpretationStrategy,
- array $data = [],
- ItemResolverInterface $itemResolver = null
- ) {
- $this->priceCurrency = $priceCurrency;
- $this->imageBuilder = $imageBuilder;
- $this->_urlHelper = $urlHelper;
- $this->_productConfig = $productConfig;
- $this->_checkoutSession = $checkoutSession;
- $this->messageManager = $messageManager;
- parent::__construct($context, $data);
- $this->_isScopePrivate = true;
- $this->moduleManager = $moduleManager;
- $this->messageInterpretationStrategy = $messageInterpretationStrategy;
- $this->itemResolver = $itemResolver ?: ObjectManager::getInstance()->get(ItemResolverInterface::class);
- }
- /**
- * Set item for render
- *
- * @param AbstractItem $item
- * @return $this
- * @codeCoverageIgnore
- */
- public function setItem(AbstractItem $item)
- {
- $this->_item = $item;
- return $this;
- }
- /**
- * Get quote item
- *
- * @return AbstractItem
- * @codeCoverageIgnore
- */
- public function getItem()
- {
- return $this->_item;
- }
- /**
- * Get item product
- *
- * @return \Magento\Catalog\Model\Product
- * @codeCoverageIgnore
- */
- public function getProduct()
- {
- return $this->getItem()->getProduct();
- }
- /**
- * Identify the product from which thumbnail should be taken.
- *
- * @return \Magento\Catalog\Model\Product
- * @codeCoverageIgnore
- */
- public function getProductForThumbnail()
- {
- return $this->itemResolver->getFinalProduct($this->getItem());
- }
- /**
- * @param string $productUrl
- * @return $this
- * @codeCoverageIgnore
- */
- public function overrideProductUrl($productUrl)
- {
- $this->_productUrl = $productUrl;
- return $this;
- }
- /**
- * Check Product has URL
- *
- * @return bool
- */
- public function hasProductUrl()
- {
- if ($this->_ignoreProductUrl) {
- return false;
- }
- if ($this->_productUrl || $this->getItem()->getRedirectUrl()) {
- return true;
- }
- $product = $this->getProduct();
- $option = $this->getItem()->getOptionByCode('product_type');
- if ($option) {
- $product = $option->getProduct();
- }
- if ($product->isVisibleInSiteVisibility()) {
- return true;
- } else {
- if ($product->hasUrlDataObject()) {
- $data = $product->getUrlDataObject();
- if (in_array($data->getVisibility(), $product->getVisibleInSiteVisibilities())) {
- return true;
- }
- }
- }
- return false;
- }
- /**
- * Retrieve URL to item Product
- *
- * @return string
- */
- public function getProductUrl()
- {
- if ($this->_productUrl !== null) {
- return $this->_productUrl;
- }
- if ($this->getItem()->getRedirectUrl()) {
- return $this->getItem()->getRedirectUrl();
- }
- $product = $this->getProduct();
- $option = $this->getItem()->getOptionByCode('product_type');
- if ($option) {
- $product = $option->getProduct();
- }
- return $product->getUrlModel()->getUrl($product);
- }
- /**
- * Get item product name
- *
- * @return string
- */
- public function getProductName()
- {
- if ($this->hasProductName()) {
- return $this->getData('product_name');
- }
- return $this->getProduct()->getName();
- }
- /**
- * Get product customize options
- *
- * @return array
- */
- public function getProductOptions()
- {
- /* @var $helper \Magento\Catalog\Helper\Product\Configuration */
- $helper = $this->_productConfig;
- return $helper->getCustomOptions($this->getItem());
- }
- /**
- * Get list of all options for product
- *
- * @return array
- * @codeCoverageIgnore
- */
- public function getOptionList()
- {
- return $this->getProductOptions();
- }
- /**
- * Get quote item qty
- *
- * @return float|int
- */
- public function getQty()
- {
- if (!$this->_strictQtyMode && (string)$this->getItem()->getQty() == '') {
- return '';
- }
- return $this->getItem()->getQty() * 1;
- }
- /**
- * Get checkout session
- *
- * @return \Magento\Checkout\Model\Session
- * @codeCoverageIgnore
- */
- public function getCheckoutSession()
- {
- return $this->_checkoutSession;
- }
- /**
- * Retrieve item messages
- * Return array with keys
- *
- * text => the message text
- * type => type of a message
- *
- * @return array
- */
- public function getMessages()
- {
- $messages = [];
- $quoteItem = $this->getItem();
- // Add basic messages occurring during this page load
- $baseMessages = $quoteItem->getMessage(false);
- if ($baseMessages) {
- foreach ($baseMessages as $message) {
- $messages[] = ['text' => $message, 'type' => $quoteItem->getHasError() ? 'error' : 'notice'];
- }
- }
- /* @var $collection \Magento\Framework\Message\Collection */
- $collection = $this->messageManager->getMessages(true, 'quote_item' . $quoteItem->getId());
- if ($collection) {
- $additionalMessages = $collection->getItems();
- foreach ($additionalMessages as $message) {
- /* @var $message \Magento\Framework\Message\MessageInterface */
- $messages[] = [
- 'text' => $this->messageInterpretationStrategy->interpret($message),
- 'type' => $message->getType()
- ];
- }
- }
- $this->messageManager->getMessages(true, 'quote_item' . $quoteItem->getId())->clear();
- return $messages;
- }
- /**
- * Accept option value and return its formatted view
- *
- * @param string|array $optionValue
- * Method works well with these $optionValue format:
- * 1. String
- * 2. Indexed array e.g. array(val1, val2, ...)
- * 3. Associative array, containing additional option info, including option value, e.g.
- * array
- * (
- * [label] => ...,
- * [value] => ...,
- * [print_value] => ...,
- * [option_id] => ...,
- * [option_type] => ...,
- * [custom_view] =>...,
- * )
- *
- * @return array
- */
- public function getFormatedOptionValue($optionValue)
- {
- /* @var $helper \Magento\Catalog\Helper\Product\Configuration */
- $helper = $this->_productConfig;
- $params = [
- 'max_length' => 55,
- 'cut_replacer' => ' <a href="#" class="dots tooltip toggle" onclick="return false">...</a>'
- ];
- return $helper->getFormattedOptionValue($optionValue, $params);
- }
- /**
- * Check whether Product is visible in site
- *
- * @return bool
- * @codeCoverageIgnore
- */
- public function isProductVisible()
- {
- return $this->getProduct()->isVisibleInSiteVisibility();
- }
- /**
- * Return product additional information block
- *
- * @return AbstractBlock
- * @codeCoverageIgnore
- */
- public function getProductAdditionalInformationBlock()
- {
- return $this->getLayout()->getBlock('additional.product.info');
- }
- /**
- * Set qty mode to be strict or not
- *
- * @param bool $strict
- * @return $this
- * @codeCoverageIgnore
- */
- public function setQtyMode($strict)
- {
- $this->_strictQtyMode = $strict;
- return $this;
- }
- /**
- * Set ignore product URL rendering
- *
- * @param bool $ignore
- * @return $this
- * @codeCoverageIgnore
- */
- public function setIgnoreProductUrl($ignore = true)
- {
- $this->_ignoreProductUrl = $ignore;
- return $this;
- }
- /**
- * Return identifiers for produced content
- *
- * @return array
- */
- public function getIdentities()
- {
- $identities = [];
- if ($this->getItem()) {
- $identities = $this->getProduct()->getIdentities();
- }
- return $identities;
- }
- /**
- * Get product price formatted with html (final price, special price, mrp price)
- *
- * @param \Magento\Catalog\Model\Product $product
- * @return string
- */
- public function getProductPriceHtml(\Magento\Catalog\Model\Product $product)
- {
- $priceRender = $this->getPriceRender();
- $priceRender->setItem($this->getItem());
- $price = '';
- if ($priceRender) {
- $price = $priceRender->render(
- ConfiguredPriceInterface::CONFIGURED_PRICE_CODE,
- $product,
- [
- 'include_container' => true,
- 'display_minimal_price' => true,
- 'zone' => \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST
- ]
- );
- }
- return $price;
- }
- /**
- * @return \Magento\Framework\Pricing\Render
- * @codeCoverageIgnore
- */
- protected function getPriceRender()
- {
- return $this->getLayout()->getBlock('product.price.render.default');
- }
- /**
- * Convert prices for template
- *
- * @param float $amount
- * @param bool $format
- * @return float
- */
- public function convertPrice($amount, $format = false)
- {
- return $format
- ? $this->priceCurrency->convertAndFormat($amount)
- : $this->priceCurrency->convert($amount);
- }
- /**
- * Return the unit price html
- *
- * @param AbstractItem $item
- * @return string
- */
- public function getUnitPriceHtml(AbstractItem $item)
- {
- /** @var Renderer $block */
- $block = $this->getLayout()->getBlock('checkout.item.price.unit');
- $block->setItem($item);
- return $block->toHtml();
- }
- /**
- * Return row total html
- *
- * @param AbstractItem $item
- * @return string
- */
- public function getRowTotalHtml(AbstractItem $item)
- {
- /** @var Renderer $block */
- $block = $this->getLayout()->getBlock('checkout.item.price.row');
- $block->setItem($item);
- return $block->toHtml();
- }
- /**
- * Return item price html for sidebar
- *
- * @param AbstractItem $item
- * @return string
- */
- public function getSidebarItemPriceHtml(AbstractItem $item)
- {
- /** @var Renderer $block */
- $block = $this->getLayout()->getBlock('checkout.cart.item.price.sidebar');
- $block->setItem($item);
- return $block->toHtml();
- }
- /**
- * Get unit price excluding tax html
- *
- * @param AbstractItem $item
- * @return string
- */
- public function getUnitPriceExclTaxHtml(AbstractItem $item)
- {
- /** @var Renderer $block */
- $block = $this->getLayout()->getBlock('checkout.onepage.review.item.price.unit.excl');
- $block->setItem($item);
- return $block->toHtml();
- }
- /**
- * Get unit price including tax html
- *
- * @param AbstractItem $item
- * @return string
- */
- public function getUnitPriceInclTaxHtml(AbstractItem $item)
- {
- /** @var Renderer $block */
- $block = $this->getLayout()->getBlock('checkout.onepage.review.item.price.unit.incl');
- $block->setItem($item);
- return $block->toHtml();
- }
- /**
- * Get row total excluding tax html
- *
- * @param AbstractItem $item
- * @return string
- */
- public function getRowTotalExclTaxHtml(AbstractItem $item)
- {
- /** @var Renderer $block */
- $block = $this->getLayout()->getBlock('checkout.onepage.review.item.price.rowtotal.excl');
- $block->setItem($item);
- return $block->toHtml();
- }
- /**
- * Get row total including tax html
- *
- * @param AbstractItem $item
- * @return string
- */
- public function getRowTotalInclTaxHtml(AbstractItem $item)
- {
- /** @var Renderer $block */
- $block = $this->getLayout()->getBlock('checkout.onepage.review.item.price.rowtotal.incl');
- $block->setItem($item);
- return $block->toHtml();
- }
- /**
- * Get row total including tax html
- *
- * @param AbstractItem $item
- * @return string
- */
- public function getActions(AbstractItem $item)
- {
- /** @var Actions $block */
- $block = $this->getChildBlock('actions');
- if ($block instanceof Actions) {
- $block->setItem($item);
- return $block->toHtml();
- } else {
- return '';
- }
- }
- /**
- * Retrieve product image
- *
- * @param \Magento\Catalog\Model\Product $product
- * @param string $imageId
- * @param array $attributes
- * @return \Magento\Catalog\Block\Product\Image
- */
- public function getImage($product, $imageId, $attributes = [])
- {
- return $this->imageBuilder->create($product, $imageId, $attributes);
- }
- }
|