cancel
Showing results for 
Search instead for 
Did you mean: 

Custom RRP price-module on product detail page

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

Custom RRP price-module on product detail page

hello,

 

Since days i try to show my custom RRP price-attribute (for reccomended retail prices) on product detail pages near other prices, unfortunately without success.

 

I've added a price-attribute in magento 2-backend, called "rrp" and visibility are setted to yes. It's shown in "more information"-tab, but not near other prices. My module is activated and enabled.

 

I've added registration.php for my module with following code, saved in "app/code/Acme/RRP"

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
	\Magento\Framework\Component\ComponentRegistrar::MODULE,
	'Acme_RRP',
	__DIR__
);

For module.xml i used following code, saved in app/code/Acme/RRP/etc

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Acme_RRP" setup_version="2.1.3" />
</config>

My module, called "RRP", includes a new attribute, set up below in app/code/Acme/RRP/Setup/InstallData.php

<?php
namespace Acme\RRP\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    protected $eavSetup;
    public function __construct(EavSetup $eavSetup)
    {
        $this->eavSetup = $eavSetup;
    }
    public function install(ModuleDataSetupInterface $setup,ModuleContextInterface $context)
    {
        $setup->startSetup();
        $this->eavSetup->addAttribute("catalog_product", "rrp", [
            'type'          => 'decimal',
            'backend'       => 'Magento\Catalog\Model\Product\Attribute\Backend\Price',
            'label'         => 'RRP',
            'input'         => 'price',
            'required'      => false,
            'global'        => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_WEBSITE,
            'group'         => 'General'
        ]);
        $setup->endSetup();
    }
}

At app/code/Acme/RRP/Pricing/Price/RRPPrice.php i've tried to setup a custom price type by creating a new class that extends Magento/Framework/Pricing/Price/AbstractPrice

<?php

namespace Acme\RRP\Pricing\Price;

use Magento\Framework\Pricing\Price\AbstractPrice;
use Magento\Framework\Pricing\Price\BasePriceProviderInterface;

/**
 * Class RRPPrice
 */
class RRPPrice extends AbstractPrice
{
    /**
     * Price type
     */
    const PRICE_CODE = 'rrp_price';

    /**
     * Get price value
     *
     * @return float|bool
     */
    public function getValue()
    {
        if ($this->value === null) {
            $price = $this->product->getRrp();
            $priceInCurrentCurrency = $this->priceCurrency->convertAndRound($price);
            $this->value = $priceInCurrentCurrency ? floatval($priceInCurrentCurrency) : false;
        }
        return $this->value;
    }
}

I've created di.xml in app/code/Acme/RRP/etc

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <virtualType name="Magento\Catalog\Pricing\Price\Pool">
        <arguments>
            <argument name="prices" xsi:type="array">
                <item name="rrp_price" xsi:type="string">Acme\RRP\Pricing\Price\RRPPrice</item>
            </argument>
        </arguments>
    </virtualType>
</config>

I've declared template for the "rrp_price" type in app/code/Acme/RRP/view/base/layout/catalog_product_prices.xml

<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
    <referenceBlock name="render.product.prices">
        <arguments>
            <argument name="default" xsi:type="array">
                <item name="prices" xsi:type="array">
                    <item name="rrp_price" xsi:type="array">
                        <item name="render_template" xsi:type="string">Acme_RRP::product/price/rrp_price.phtml</item>
                    </item>
                </item>
            </argument>
        </arguments>
    </referenceBlock>
</layout>

I've created template for the RRP price in app/code/Acem/RRP/view/base/templates/product/price/rrp_price.phtml

<?php
/** @var \Magento\Catalog\Pricing\Render\FinalPriceBox $block */

$productId = $block->getSaleableItem()->getId();

/** @var \Magento\Catalog\Pricing\Price\RegularPrice $priceModel */
$priceModel = $block->getPriceType('regular_price');

/* @escapeNotVerified */ echo $block->renderAmount($priceModel->getAmount(), [
    'price_id'          => $block->getPriceId('product-price-'),
    'include_container' => true
]);
?>

At least, i override final_price.phtml to include a call to render new RRP price in app/design/frontend/Acme/luma/Magento_Catalog/templates/product/price/final_price.phtml

<?php
/** @var \Magento\Catalog\Pricing\Render\FinalPriceBox $block */

$productId = $block->getSaleableItem()->getId();

/** @var \Magento\Catalog\Pricing\Price\RegularPrice $priceModel */
$priceModel = $block->getPriceType('regular_price');

/** @var \Magento\Catalog\Pricing\Price\FinalPrice $finalPriceModel */
$finalPriceModel = $block->getPriceType('final_price');

/** @var \Acme\RRP\Pricing\Price\RRPPrice $rrpPriceModel */
$rrpPriceModel = $block->getPriceType('rrp_price');

$idSuffix = $block->getIdSuffix() ? $block->getIdSuffix() : '';
$schema = ($block->getZone() == 'item_view') ? true : false;
?>

<?php /* @escapeNotVerified */ echo $block->renderAmount($rrpPriceModel->getAmount(), [
    'price_id'          => $block->getPriceId('rrp-price-' . $idSuffix),
    'price_type'        => 'rrpPrice',
    'include_container' => true,
    'schema'            => $schema
]); ?>

<?php if ($block->hasSpecialPrice()): ?>
    <span class="special-price">
        <?php /* @escapeNotVerified */ echo $block->renderAmount($finalPriceModel->getAmount(), [
            'display_label'     => __('Special Price'),
            'price_id'          => $block->getPriceId('product-price-' . $idSuffix),
            'price_type'        => 'finalPrice',
            'include_container' => true,
            'schema' => $schema
        ]); ?>
    </span>
    <span class="old-price">
        <?php /* @escapeNotVerified */ echo $block->renderAmount($priceModel->getAmount(), [
            'display_label'     => __('Regular Price'),
            'price_id'          => $block->getPriceId('old-price-' . $idSuffix),
            'price_type'        => 'oldPrice',
            'include_container' => true,
            'skip_adjustments'  => true
        ]); ?>
    </span>
<?php else: ?>
    <?php /* @escapeNotVerified */ echo $block->renderAmount($finalPriceModel->getAmount(), [
        'price_id'          => $block->getPriceId('product-price-' . $idSuffix),
        'price_type'        => 'finalPrice',
        'include_container' => true,
        'schema' => $schema
    ]); ?>
<?php endif; ?>

<?php if ($block->showMinimalPrice()): ?>
    <?php if ($block->getUseLinkForAsLowAs()):?>
        <a href="<?php /* @escapeNotVerified */ echo $block->getSaleableItem()->getProductUrl(); ?>" class="minimal-price-link">
            <?php /* @escapeNotVerified */ echo $block->renderAmountMinimal(); ?>
        </a>
    <?php else:?>
        <span class="minimal-price-link">
            <?php /* @escapeNotVerified */ echo $block->renderAmountMinimal(); ?>
        </span>
    <?php endif?>
<?php endif; ?>

Does anybody knows what i've did wrong? Unfortunately nothing is shown on product detail pages.

 

Thank you so much for your help and have a good day!