cancel
Showing results for 
Search instead for 
Did you mean: 

Custom price attribute - how to add global price format and attribute label in frontend?

SOLVED

Custom price attribute - how to add global price format and attribute label in frontend?

Hello,

I've added a custom price attribute called "rrp" in magento 2.1, which i like to show on frontends category- and product-page.

But in frontend, price is shown with 4 decimals, for example 10.0000 instead 10.00, and without currency symbol and without attribute label.

code i've used to show the attribute value:

<?php /* @escapeNotVerified */ echo $_helper->productAttribute($_product, $_product->getRrp(), 'rrp') ?>


to show attribute label also, i've tried following code, but this doesn't work: 

<?php /* @escapeNotVerified */ echo $_helper->productAttribute($_product, $_product->getRrp(), 'rrp'); $_product->getResource()->getAttribute('rrp')->getStoreLabel() ?>

How i could add global currency format to the attribute?
And how i could add the attribute label?

Thanks a lot for your help and have a good day!

2 ACCEPTED SOLUTIONS

Accepted Solutions

Re: Custom price attribute - how to add global price format and attribute label in frontend?

Hi,

 

on the product page the fastest way should be using pricing helper to format the price:

<?php $pricingHelper = $this->helper(\Magento\Framework\Pricing\Helper\Data::class); ?>
<?php echo $pricingHelper->currency($_product->getData('rrp'), true, false); ?>

For the attribute label, there's an echo statement missing in your code, so the attribute label is retrieved, but not displayed:

<?php echo $_product->getResource()->getAttribute('rrp')->getStoreLabel(); ?>

Cheers,

BX

View solution in original post

Re: Custom price attribute - how to add global price format and attribute label in frontend?

Hi @cojone

 

I was looking for some built-in helper that could be used for this purpose, but I didn't find any. Actually, there's Magento\Customer\Helper\Session\CurrentCustomer class, but it doesn't inherit from AbstractHelper, so it cannot be easily called in the template. Therefore it seems that you have to create own helper to achieve your goal.

 

I assume that you are able to create a basic Magento 2 module structure containing valid module.xml and registration.php files, otherwise let me know, I will provide you with the content of those files. Let's call our module MyVendor_MyModule and let's assume it resides in app/code/MyVendor/MyModule directory.

 

Next create a new helper class in app/code/MyVendor/MyModule/Helper/Customer.php file with the following content:

 

<?php

namespace MyVendor\MyModule\Helper;

class Customer extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $customerSession;

    public function __construct(\Magento\Customer\Model\Session $customerSession)
{ $this->customerSession = $customerSession; } public function isCurrentCustomerRetailer() { // instead of 3 you can put any customer group ID return $this->customerSession->getCustomerGroupId() == 3; } }

 

In the template file use following condition to decide whether to display your custom price attribute or not:

 

<?php $pricingHelper = $this->helper(\Magento\Framework\Pricing\Helper\Data::class); ?>
<?php $customerHelper = $this->helper(\MyVendor\MyModule\Helper\Customer::class); ?>
<?php if ($customerHelper->isCurrentCustomerRetailer()): ?>
<?php echo $_product->getResource()->getAttribute('rrp')->getStoreLabel(); ?>
<?php echo $pricingHelper->currency($_product->getData('rrp'), true, false); ?>
<?php endif; ?>

 

You may also need to enable your module and run upgrade scripts from the console:

 

$ bin/magento module:enable MyVendor_MyModule
$ bin/magento setup:upgrade
$ bin/magento cache:clean

 

Hope this helps.

 

Cheers,

BX

View solution in original post

7 REPLIES 7

Re: Custom price attribute - how to add global price format and attribute label in frontend?

Hi,

 

on the product page the fastest way should be using pricing helper to format the price:

<?php $pricingHelper = $this->helper(\Magento\Framework\Pricing\Helper\Data::class); ?>
<?php echo $pricingHelper->currency($_product->getData('rrp'), true, false); ?>

For the attribute label, there's an echo statement missing in your code, so the attribute label is retrieved, but not displayed:

<?php echo $_product->getResource()->getAttribute('rrp')->getStoreLabel(); ?>

Cheers,

BX

Re: Custom price attribute - how to add global price format and attribute label in frontend?

Thank you very much for your help!
Do you know how i could make this visible for a specific customer group (retailers) only?
thank you & greetings

Re: Custom price attribute - how to add global price format and attribute label in frontend?

Hi @cojone

 

I was looking for some built-in helper that could be used for this purpose, but I didn't find any. Actually, there's Magento\Customer\Helper\Session\CurrentCustomer class, but it doesn't inherit from AbstractHelper, so it cannot be easily called in the template. Therefore it seems that you have to create own helper to achieve your goal.

 

I assume that you are able to create a basic Magento 2 module structure containing valid module.xml and registration.php files, otherwise let me know, I will provide you with the content of those files. Let's call our module MyVendor_MyModule and let's assume it resides in app/code/MyVendor/MyModule directory.

 

Next create a new helper class in app/code/MyVendor/MyModule/Helper/Customer.php file with the following content:

 

<?php

namespace MyVendor\MyModule\Helper;

class Customer extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $customerSession;

    public function __construct(\Magento\Customer\Model\Session $customerSession)
{ $this->customerSession = $customerSession; } public function isCurrentCustomerRetailer() { // instead of 3 you can put any customer group ID return $this->customerSession->getCustomerGroupId() == 3; } }

 

In the template file use following condition to decide whether to display your custom price attribute or not:

 

<?php $pricingHelper = $this->helper(\Magento\Framework\Pricing\Helper\Data::class); ?>
<?php $customerHelper = $this->helper(\MyVendor\MyModule\Helper\Customer::class); ?>
<?php if ($customerHelper->isCurrentCustomerRetailer()): ?>
<?php echo $_product->getResource()->getAttribute('rrp')->getStoreLabel(); ?>
<?php echo $pricingHelper->currency($_product->getData('rrp'), true, false); ?>
<?php endif; ?>

 

You may also need to enable your module and run upgrade scripts from the console:

 

$ bin/magento module:enable MyVendor_MyModule
$ bin/magento setup:upgrade
$ bin/magento cache:clean

 

Hope this helps.

 

Cheers,

BX

Re: Custom price attribute - how to add global price format and attribute label in frontend?

Hello,

Thank you so much for your help!

But sadely, i can't bring it to work till now... I think i've did something wrong, but i can't find my mistake(s)...

I've added customer.php and template code as you described.

Then i've added a registration.php with following code, saved in app/code/MyVendor/MyModule/

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


after, i've created module.xml with following code, saved in app/code/MyVendor/MyModule/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="MyVendor_MyModule" setup_version="2.1.3" />
</config>


then i've created composer.json with following code, saved in vendor/myvendor/mymodule/

{
  "name": "myvendor/mymodule",
  "description": "Module für UVP Kundengruppe Händler",
  "require": {
    "magento/magento-composer-installer": "*"
  },
  "type": "magento2-module",
  "version": "0.0.1",
  "autoload": {
    "files": [ "registration.php" ],
      "psr-4": {
        "MyVendor\\MyModule\\": ""
    }
  }
}


and then i've added second part of following code in root/composer.json to "repositories"

"repositories": [
        {
            "type": "composer",
            "url": "https://repo.magento.com/"
        },
		{
            "type": "vcs",
            "url": "/app/code/MyVendor/MyModule"
        }
    ],


i think i've did something wrong in this files, but i can't find where...
when i try to enable module and run upgrade scripts i got some failed message...
Do you have an idea what i've did wrong?

Thank you very much for your help and greetings

Stephan

Re: Custom price attribute - how to add global price format and attribute label in frontend?

Hi @cojone

 

for the modules residing in app/code directory, you don't have to bother with composer configuration, modules from this directory are automatically registered and recognized by autoloader. Just make sure you follow the naming conventions, i.e. if you put your code under app/code in MyVendor/MyModule directory, your namespaces should start with MyVendor\MyModule, if you use MyVendor\MyModule\Helper\Customer helper, its class can be found in app/code/MyVendor/MyModule/Helper/Customer.php file (note, that everything is case-sensitive).

 

I believe it's enough for you to rollback all changes you did to the global composer.json, because your module.xml and registration.php files look ok for me.

 

I would also delete the custom composer.json from vendor/myvendor/mymodule directory, but I guess it doesn't really matter.

 

Cheers,

BX

Re: Custom price attribute - how to add global price format and attribute label in frontend?

Thank you very much for your help! now it works on catalog page, thank you!

Last quetion i have... :-) i try to add this to product pages and order invoice also (for retailer customer group), but i can't make this work... In product pages, it's visible in "more information"-tab, but i like to show it below prices, instead information-tab...
would you have any ideas for this also..?

Thank you so much and have a nice day!

Re: Custom price attribute - how to add global price format and attribute label in frontend?

Hello,

Thank you so much for your usefull help.
But sadely i still have problems to show it on product detail pages... Do you have any ideas what i've done wrong? I tried to make a new module for this...

I started new thread with code at following link...

https://community.magento.com/t5/Programming-Questions/Custom-RRP-price-module-on-product-detail-pag...

Thank you very much if you could help me for this!