0

I am brand new to Magento and am a Wordpress developer and am trying to get my bearings on something that seems like it should be simple to accomplish. I am trying to simply add a data-attribute to my product title on the product details page. I want to do this so I can add some logic in my JS based on it. The issue is despite the research I have done I cannot get it to work.

I have located the title.phtml file and tried adding in

data-category="<?php echo $block->getCurrentCategory->getId() ?>"

but when I reload the page it is just white.

What is the easiest way to go about this? In wordpress I can literally just do the_title() and it gets the title of the post. Is there something similar by chance?

0

Product title (product name) in product detail page define in

vendor\magento\module-catalog\view\frontend\layout\catalog_product_view.xml

<referenceBlock name="page.main.title">
            <arguments>
                <argument name="css_class" xsi:type="string">product</argument>
                <argument name="add_base_attribute" xsi:type="string">itemprop="name"</argument>
            </arguments>
</referenceBlock>

i think the easiest way to go about this is create plugin.

app/code/Company/Module/etc/frontend/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <type name="Magento\Catalog\Model\Product">
        <plugin name="training_catalog_product" type="Company\Module\Plugin\Catalog\Model\Product"/>
    </type>
</config>

app/code/Company/Module/Plugin/Catalog/Model/Product.php

class Product
{
    public function afterGetName(\Magento\Catalog\Model\Product $subject, $result)
    {
        $title = $subject->getAttributeText('other_name');
        return $title;
    }
}
|improve this answer|||||
  • I appreciate your reply but I think you may have misread. I am looking to get the product category and add it into the title. – OMGDrAcula Mar 19 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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