Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use blocks instead of directly modifying the template #38

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 0 additions & 114 deletions Model/Plugin/AfterPrice.php
Original file line number Diff line number Diff line change
@@ -1,114 +0,0 @@
<?php
/**
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*/

/**
* @category Magenerds
* @package Magenerds_GermanLaw
* @subpackage Model
* @copyright Copyright (c) 2019 TechDivision GmbH (https://www.techdivision.com)
* @link https://www.techdivision.com/
* @author Florian Sydekum <[email protected]>
*/
namespace Magenerds\GermanLaw\Model\Plugin;

use Magento\Framework\Pricing\Render;
use Magento\Framework\Pricing\SaleableInterface;

/**
* Class AfterPrice
* @package Magenerds\GermanLaw\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';

/**
* Hold layout
*
* @var \Magento\Framework\View\LayoutInterface
*/
protected $_layout;

/**
* Hold after price html string
*
* @var null|string
*/
protected $_afterPriceHtml = null;

/**
* Constructor
*
* @param \Magento\Framework\View\LayoutInterface $layout
*/
public function __construct(
\Magento\Framework\View\LayoutInterface $layout
){
$this->_layout = $layout;
}

/**
* Plugin for price rendering in order to display after price information
*
* @param \Magento\Framework\Pricing\Render $subject
* @oaram \Closure $closure
* @param array $params
* @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();
}
} 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()
{
if (null === $this->_afterPriceHtml) {
$afterPriceBlock = $this->_layout->createBlock('Magenerds\GermanLaw\Block\AfterPrice', 'after_price');
$afterPriceBlock->setTemplate('Magenerds_GermanLaw::price/after.phtml');
$this->_afterPriceHtml = $afterPriceBlock->toHtml();
}

return $this->_afterPriceHtml;
}
}
24 changes: 0 additions & 24 deletions etc/di.xml

This file was deleted.

27 changes: 27 additions & 0 deletions view/frontend/layout/catalog_product_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0"?>
<!--
/**
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*/

/**
* @category Magenerds
* @package Magenerds_GermanLaw
* @subpackage view
* @author Konrad Langenberg <[email protected]>
* @copyright Copyright (c) 2019 TechDivision GmbH (http://www.techdivision.com)
* @link http://www.techdivision.com/
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="product.info.price">
<block after="product.price.final" class="Magenerds\GermanLaw\Block\AfterPrice" name="price.final.after" as="price.final.after"
template="Magenerds_GermanLaw::price/after.phtml"/>
</referenceContainer>
</body>
</page>