0

I have created two phtml files that can be called from within a CMS Block on a product page to echo the product name and an attribute name.

As follows:

Product Name

{{block class="Vendor\ProductName\Block\ProductName" template="Vendor_ProductName::productname.phtml" }}

Attribute Name (Artist)

{{block class="Magento\Catalog\Block\Product\View" name="artist" template="Magento_Catalog::artist.phtml"}}

I want to be able to create a title within the H1 tag for the product name, as follows

Buy 'Product name' by 'Artist'

The following is the title.phtml code thats currently used.

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/**
 * @var $block \Magento\Theme\Block\Html\Title
 */
$cssClass = $block->getCssClass() ? ' ' . $block->getCssClass() : '';
$title = '';
if (trim($block->getPageHeading())) {
    $title = '<span class="base" data-ui-id="page-title-wrapper" ' .  $block->getAddBaseAttribute() . '>'
        . $block->escapeHtml($block->getPageHeading()) . '</span>';
}
?>
<?php if ($title): ?>
    <?php if ($this->getRequest()->getFullActionName() == 'catalog_product_view'): ?>
        <h1 class="tt-title"><?= /* @escapeNotVerified */ $title ?></h1>
    <?php else: ?>

And this is how it outputs on the frontend

<h1 class="tt-title"><span class="base" data-ui-id="page-title-wrapper" itemprop="name">Terrace of a café at night (Place du Forum)</span></h1>

Is there a way or using the phtml calls that I use in the CMS Blocks to create the product page H1 title?


UPDATE

I used the following to get part of the title

Buy "Product Name" by

By using the following code,

Changed the H1 Title as follows

title="Buy <?php echo $block->getPageHeading(); ?> by">

This is the code I'm using to get the current product page attribute

<?php
$product = $block->getProduct();
$product_name = $product->getName();
$attr_exists = $product->getResource()->getAttribute('artist');

if ($attr_exists){
    $attr_name = $product->getAttributeText('artist');
}

echo $product->getAttributeText('artist');
?>

How can I use the above code to add the artist name to the H1 title?

1 Answer 1

0

I called the following block

{{block class="Magento\Catalog\Block\Product\View" name="artist" template="Magento_Catalog::artist.phtml"}}

into the title.phtml block using the following

<?php echo $this->getLayout()->createBlock("Magento\Catalog\Block\Product\View")->setTemplate("Magento_Catalog::artist.phtml")->toHtml(); ?>

so the final code to get the H1 title using the product name and attribute name is

title="Buy <?php echo $block->getPageHeading(); ?> by <?php echo $this->getLayout()->createBlock("Magento\Catalog\Block\Product\View")->setTemplate("Magento_Catalog::artist.phtml")->toHtml(); ?> ">

Resulting in the desired result creating a dynamic product name title

Buy "Product name" by "Artist"

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.