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 #41

Open
wants to merge 3 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 14 additions & 7 deletions Block/AfterPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

Expand All @@ -82,14 +82,21 @@ public function isEnabled()
return $moduleEnabled && !empty($productAmount);
}

/**
* @return bool
*/
public function isConfigurable():bool {
return $this->getProduct()->getTypeId() == 'configurable';
}

/**
* Retrieve current product
*
* @return \Magento\Catalog\Model\Product
*/
public function getProduct()
{
return $this->_product;
return $this->_registry->registry('current_product');
}

/**
Expand Down
135 changes: 0 additions & 135 deletions Model/Plugin/AfterPrice.php

This file was deleted.

3 changes: 0 additions & 3 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Pricing\Render">
<plugin name="magenerds_baseprice_priceRender" type="Magenerds\BasePrice\Model\Plugin\AfterPrice" />
</type>
<type name="Magento\ConfigurableProduct\Block\Product\View\Type\Configurable">
<plugin name="magenerds_baseprice_configurablePriceRender" type="Magenerds\BasePrice\Model\Plugin\ConfigurablePrice" />
</type>
Expand Down
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 <k.langenberg@imi.de>
* @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\BasePrice\Block\AfterPrice" name="price.final.base" as="price.final.base"
template="Magenerds_BasePrice::afterprice.phtml"/>
</referenceContainer>
</body>
</page>
39 changes: 38 additions & 1 deletion view/frontend/templates/afterprice.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,41 @@
<div class="baseprice">
<?php echo $block->getBasePrice() ?>
</div>
<?php endif; ?>
<?php if ($block->isConfigurable()): ?>
<script type="text/javascript">
require(['jquery', 'jquery/ui', 'domReady!'], function($) {
$('body').on('updatePrice', function (e, data) {

var swatchWidget = $('.swatch-opt').data('mage-SwatchRenderer');
if(swatchWidget) {
var options = _.object(_.keys(swatchWidget.optionsMap), {});

swatchWidget.element.find('.' + swatchWidget.options.classes.attributeClass + '[option-selected]').each(function () {
options[$(this).attr('attribute-id')] = $(this).attr('option-selected');
});
result = swatchWidget.options.jsonConfig.optionPrices[_.findKey(swatchWidget.options.jsonConfig.index, options)];
} else {
var configurableWidget = $('#product_addtocart_form').data('mage-configurable');

var options = {};

configurableWidget.element.find(configurableWidget.options.superSelector + ' option:selected').each(function () {
if($(this).val()) {
options[Number($(this).parent().prop('id').replace('attribute', ''))] = $(this).val();
}

});

var config = configurableWidget.option('spConfig');
result = config.optionPrices[_.findKey(config.index, options)];
}

var basePriceField = $('.product-info-price .baseprice');
if (typeof result != 'undefined' && result.magenerds_baseprice_text.length && basePriceField.length) {
basePriceField.html(result.magenerds_baseprice_text);
}
});
});
</script>
<?php endif; ?>
<?php endif; ?>
62 changes: 0 additions & 62 deletions view/frontend/templates/configurable/afterprice.phtml

This file was deleted.