diff --git a/Block/AfterPrice.php b/Block/AfterPrice.php index f5a2711..33a127f 100755 --- a/Block/AfterPrice.php +++ b/Block/AfterPrice.php @@ -34,14 +34,14 @@ class AfterPrice extends \Magento\Framework\View\Element\Template protected $_helper; /** - * @var \Magento\Catalog\Model\Product + * @var string */ - protected $_product; + protected $_configurablePricesJson; /** - * @var string + * @var \Magento\Framework\Registry */ - protected $_configurablePricesJson; + protected $_registry; /** * Constructor @@ -56,12 +56,12 @@ class AfterPrice extends \Magento\Framework\View\Element\Template public function __construct( \Magento\Backend\Block\Template\Context $context, \Magenerds\BasePrice\Helper\Data $helper, - \Magento\Catalog\Model\Product $product, + \Magento\Framework\Registry $registry, array $data = [] ){ $this->_scopeConfig = $context->getScopeConfig(); $this->_helper = $helper; - $this->_product = $product; + $this->_registry = $registry; parent::__construct($context, $data); } @@ -82,6 +82,13 @@ public function isEnabled() return $moduleEnabled && !empty($productAmount); } + /** + * @return bool + */ + public function isConfigurable():bool { + return $this->getProduct()->getTypeId() == 'configurable'; + } + /** * Retrieve current product * @@ -89,7 +96,7 @@ public function isEnabled() */ public function getProduct() { - return $this->_product; + return $this->_registry->registry('current_product'); } /** diff --git a/Model/Plugin/AfterPrice.php b/Model/Plugin/AfterPrice.php deleted file mode 100644 index 18f5566..0000000 --- a/Model/Plugin/AfterPrice.php +++ /dev/null @@ -1,135 +0,0 @@ - - */ -namespace Magenerds\BasePrice\Model\Plugin; - -use Magento\Catalog\Pricing\Price\FinalPrice; -use Magento\Framework\Pricing\Render; -use Magento\Framework\Pricing\SaleableInterface; -use Magento\Framework\View\LayoutInterface; - -/** - * Class AfterPrice - * @package Magenerds\BasePrice\Model\Plugin - */ -class AfterPrice -{ - /** - * Hold final price code - * - * @var string - */ - const FINAL_PRICE = 'final_price'; - - /** - * Hold tier price code - * - * @var string - */ - const TIER_PRICE = 'tier_price'; - - /** - * @var LayoutInterface - */ - protected $layout; - - /** - * @var [] - */ - protected $afterPriceHtml = []; - - /** - * @param LayoutInterface $layout - */ - public function __construct( - LayoutInterface $layout - ){ - $this->layout = $layout; - } - - /** - * Plugin for price rendering in order to display after price information - * - * @param Render $subject - * @param $renderHtml string - * @return string - */ - public function aroundRender(Render $subject, \Closure $closure, ...$params) - { - // run default render first - $renderHtml = $closure(...$params); - - try{ - // Get Price Code and Product - list($priceCode, $productInterceptor) = $params; - $emptyTierPrices = empty($productInterceptor->getTierPrice()); - - // If it is final price block and no tier prices exist set additional render - // If it is tier price block and tier prices exist set additional render - if ((static::FINAL_PRICE === $priceCode && $emptyTierPrices) || (static::TIER_PRICE === $priceCode && !$emptyTierPrices)) { - $renderHtml .= $this->getAfterPriceHtml($productInterceptor); - } - } catch (\Exception $ex) { - // if an error occurs, just render the default since it is preallocated - return $renderHtml; - } - - return $renderHtml; - } - - /** - * Renders and caches the after price html - * - * @return null|string - */ - protected function getAfterPriceHtml(SaleableInterface $product) - { - // check if product is available - if (!$product) return ''; - - // if a grouped product is given we need the current child - if ($product->getTypeId() == 'grouped') { - $product = $product->getPriceInfo() - ->getPrice(FinalPrice::PRICE_CODE) - ->getMinProduct(); - - // check if we found a product - if (!$product) return ''; - } - - // check if price for current product has been rendered before - if (!array_key_exists($product->getId(), $this->afterPriceHtml)) { - $afterPriceBlock = $this->layout->createBlock( - 'Magenerds\BasePrice\Block\AfterPrice', - 'baseprice_afterprice_' . $product->getId(), - ['product' => $product] - ); - - // use different templates for configurables and other product types - if ($product->getTypeId() == 'configurable') { - $templateFile = 'Magenerds_BasePrice::configurable/afterprice.phtml'; - } else { - $templateFile = 'Magenerds_BasePrice::afterprice.phtml'; - } - - $afterPriceBlock->setTemplate($templateFile); - $this->afterPriceHtml[$product->getId()] = $afterPriceBlock->toHtml(); - } - - return $this->afterPriceHtml[$product->getId()]; - } -} diff --git a/etc/di.xml b/etc/di.xml index f9f9ec0..07a8de9 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -18,9 +18,6 @@ */ --> - - - diff --git a/view/frontend/layout/catalog_product_view.xml b/view/frontend/layout/catalog_product_view.xml new file mode 100644 index 0000000..7bdfce2 --- /dev/null +++ b/view/frontend/layout/catalog_product_view.xml @@ -0,0 +1,27 @@ + + + + + + + + + diff --git a/view/frontend/templates/afterprice.phtml b/view/frontend/templates/afterprice.phtml index bbf4492..c8211a0 100755 --- a/view/frontend/templates/afterprice.phtml +++ b/view/frontend/templates/afterprice.phtml @@ -24,4 +24,41 @@
getBasePrice() ?>
- \ No newline at end of file + isConfigurable()): ?> + + + diff --git a/view/frontend/templates/configurable/afterprice.phtml b/view/frontend/templates/configurable/afterprice.phtml deleted file mode 100755 index b29840d..0000000 --- a/view/frontend/templates/configurable/afterprice.phtml +++ /dev/null @@ -1,62 +0,0 @@ - - * @copyright Copyright (c) 2017 TechDivision GmbH (http://www.techdivision.com) - * @link http://www.techdivision.com/ - */ - -/** - * @var $block \Magenerds\BasePrice\Block\AfterPrice - */ -?> -isEnabled()): ?> -
- getBasePrice() ?> -
- -