0

I'm looking to retrieve the value of an attribut with the ID "size_clamp" and show it on the short and long description of my product. I have tried to add the following code directly in the short description area on the back-end but no luck :

  • "This clamp has an exterior size <?php echo $_product->getAttributeText("size_clamp"); ?> for clamping on the tube"...

This gives me an error on the front end. I was hopping for the following result :

  • "This clamp has an exterior size 27mm for clamping on the tube"

Here is why i'm looking for this :

  • Avoid repetitive manual input in the text for thousand products, it's the same clamps with different sizes. I want to have a default text with those shortcode and just paste it in both short and long description.
  • Seo reason, i was thinking to create an attribut called "seo_text" that is different per store view which would change based on the store view. This could avoid having duplicate content on google.

Thank you in advance for your help.

Regards, Marc

3 Answers 3

0

You can use below methods :

$attributeCode = 'attribute_text_field';
$attributeValue = $product->getData($attributeCode);

Or

$product->getCustomAttribute('attribute_text_field');

or

$product->getResource()->getAttribute('attribute_text_field')->getFrontend()->getValue($product)
2
  • Hi Divyarajsinh Barad, thank you for your response. Unfortunatly as i understand i cannot add this code directly on the short description tab in magento ? Do you know a way it ?
    – Marcwales
    Commented Aug 4, 2023 at 10:31
  • Please use "<?= $block->getProduct()->getShortDescription(); ?>" this and let me know if any issue. Commented Aug 7, 2023 at 12:26
0

If you want product attribute value on front-end, Please try below method-:

Method1 :-

<?php /* @escapeNotVerified */ echo $product->getResource()->getAttribute('size_clamp')->getFrontend()->getValue($product); ?>

Method2:-

<div><?php echo $_product->getData('size_clamp'); ?></div>

Method3:-

<div><?php echo $product->getCustomAttribute('size_clamp'); ?></div>

Verify attribute Yes for frontend:-

Admin -> Stores -> Attributes -> Product ->

Find your attribute in list and click on edit Click on Storefront Properties tab and check Allow HTML Tags on Storefront and Visible on Catalog Pages on Storefront are YES

5
  • Hi MP Raj, thank you for your fast return. I've copied both codes in the short description area on the product page but no luck, i get the following on the front-end "getData('code_produit'); ?>". I've change the attribut to "code_product" which is a simple texte field attribut in case that was the problem.
    – Marcwales
    Commented Aug 4, 2023 at 9:12
  • Can you please tell me what type of attribute in Admin and Magento verstion ?
    – MP Raj
    Commented Aug 4, 2023 at 9:14
  • Please check updated answer
    – MP Raj
    Commented Aug 4, 2023 at 9:21
  • By not being able to add php directly in the short description tab, i will not be able to add the value inside a specific place (like inside a phrase). Is there no other way to achieve this ? I've read something about dynamic text being used for seo, that should be the same idear !
    – Marcwales
    Commented Aug 4, 2023 at 10:21
  • Or a widget that calls the specific attribut ?
    – Marcwales
    Commented Aug 4, 2023 at 10:30
0

Thank you for your help. I'm running on magento 2.4.5 and i've added a screenshot of my attribut to make it easier.

attribut

And this is where i'm trying to input the value :

description

Regards, Marc

2
  • PHP code will not work in backend description and short description tab. You can add hard value there.
    – MP Raj
    Commented Aug 4, 2023 at 9:29
  • That is what i'm looking for ;) Maybe i need to change my question at the beginning to be more precise, "hard value" is that the right term ?
    – Marcwales
    Commented Aug 4, 2023 at 9:39

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.