0

I want to show two product custom attributes in product view page side by side the price with the same format (font size). The attributes are PP and PR witch I created in Magento Admin and assign to the product.

enter image description here

Next I tried to put the code in list.phtml to display the attributes:

enter image description here

I used

<?php echo 'PP ' . $_product->getData('pp') . ' PR ' . $_product->getData('pr'); ?>

But unfortunately I just can't retrieved the value of the attributes and it shows blank. enter image description here

Could someone help me on this issue?

3
  • Have you re-index after new attributes?
    – kunj
    Commented May 11, 2018 at 16:58
  • Yes, I have had re-index.
    – Nuno Sousa
    Commented May 11, 2018 at 18:54
  • Ok, I have added answer, please try and let mw know that works or not.
    – kunj
    Commented May 11, 2018 at 19:18

3 Answers 3

0

Try below code.

<?php /* @escapeNotVerified */ echo 'PP '.$_product->getResource()->getAttribute('pp')->getFrontend()->getValue($_product) . ' PR ' .     $_product->getResource()->getAttribute('pr')->getFrontend()->getValue($_product)?>

Also, check both attributes are Visible on Catalog Pages on Storefront Used in Product Listing.

0

$product->getCustomAttribute('pr')->getValue();

0

Here's an example of how you can display custom product attributes in the list.phtml template file:

     <?php
        $ppValue = $_product->getResource()->getAttribute('pp')->getFrontend()->getValue($_product);
        $prValue = $_product->getResource()->getAttribute('pr')->getFrontend()->getValue($_product);
     ?>

    <?php if ($ppValue): ?>
       <div class="pp-attribute">
         <span class="label"><?php echo __('PP'); ?>:</span>
         <span class="value"><?php echo $ppValue; ?></span>
       </div>
    <?php endif; ?>

    <?php if ($prValue): ?>
      <div class="pr-attribute">
         <span class="label"><?php echo __('PR'); ?>:</span>
         <span class="value"><?php echo $prValue; ?></span>
      </div>
    <?php endif; ?>

This code uses $ppValue and $prValue to retrieve the values of the pp and pr attributes respectively and then checks if they have values before displaying them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.