1

hi i want to know is it possible to show a custom meta tag here i have created an attribute and assigned to the seo group and i want this value to be visible under meta tag for the current product enter image description here

enter image description here

default_head_blocks.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
            <meta property="og:type" content="product" />
            </head>
       <body>
           <referenceBlock name="head.additional">
                <block class="Test\Meta\Block\Meta" name="custom_meta" template="Test_Meta::custom_meta.phtml"/>
            </referenceBlock>
       </body> 
    

</page>

phtml file

<?php
$productCollection = $block->getProductCollection();
foreach ($productCollection as $product) {
    print_r($product->getData());     
    echo "<br>";

    $attribute = $_product->getResource()->getAttribute('miscellaneous_meta');
    $attribute->getFrontend()->getValue($_product);
    $attribute->getFrontendLabel();
}

block file

<?php
namespace Test\Meta\Block;
class Meta extends \Magento\Framework\View\Element\Template
{    
    protected $_productCollectionFactory;
        
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,        
        array $data = []
    )
    {    
        $this->_productCollectionFactory = $productCollectionFactory;    
        parent::__construct($context, $data);
    }
    
    public function getProductCollection()
    {
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->addAttributeToFilter('miscellaneous_meta');
        
        return $collection;
    }
}
?>

but this is not working!!

2

If you want to add custom meta tag only on product page :

Create:

app/design/frontend/{Package}/{theme}/Magento_Catalog/layout/catalog_product_view.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="head.additional">
            <block class="Magento\Catalog\Block\Product\View" name="custom_meta" template="Magento_Catalog::product/view/custom_meta.phtml" />
        </referenceBlock>
    </body>
</page> 

Create phtml at:

app/design/frontend/{Package}/{theme}/Magento_Catalog/templates/product/view/custom_meta.phtml

<meta property="og:custom" content="<?= /* @noEscape */ $block->stripTags($block->getProduct()->getMiscellaneousMeta()) ?>" />

Note:

  • If you add phtml in default_head_blocks.xml it will render on all page and we only need for product page so use catalog_product_view.xml

  • No need of custom block, we can use default Magento\Catalog\Block\Product\View block and can get product

  • You can add as many meta tag you want in this phtml!

6
  • its still not coming as other meta tag and i want it to be displayed at top near meta title like this <meta name="meta-miscellaneous" content="misc conetnt"/> – Pramod Aug 12 '20 at 3:40
  • it will add meta tag in head tag, you can use after or before to move content – Pawan Aug 12 '20 at 7:34
  • i was trying it like this but its not working <referenceBlock name="head.additional" before="meta.title"> – Pramod Aug 12 '20 at 8:37
  • after and before can not be work with refrenceblock, you need to add with custom block we added – Pawan Aug 12 '20 at 8:40
  • i need to put this reference block inside a referececontainter – Pramod Aug 12 '20 at 8:45
0

You do not think you need to add phtml here. You can manage from the plugin.

Create after plugin on \Magento\Catalog\Helper\Product\View::prepareAndRender add your custom meta info.

here the plugin class

<?php

namespace Devbera\Test\Plugin;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;

class View
{
    /**
     * @var ProductRepositoryInterface
     */
    private $productRepository;
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    private $storeManager;
    /**
     * @var \Magento\Framework\View\Page\Config
     */
    private $config;

    public function __construct(
        ProductRepositoryInterface $productRepository,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\View\Page\Config $config
    )
    {
        $this->productRepository = $productRepository;
        $this->storeManager = $storeManager;
        $this->config = $config;
    }

    public function FunctionName($productId = false)
    {
        if (!$productId) {
            return false;
        }

        try {
            $product = $this->productRepository
                ->getById(
                    $productId,
                    false,
                    $this->storeManager->getStore()->getId()
                );
            $attribute = $product->getResource()->getAttribute('miscellaneous_meta');
            $attribute->getFrontend()->getValue($product);
            $value = $attribute->getFrontendLabel();
            $this->config->setMetadata('miscellaneous_meta', $value);
        } catch (NoSuchEntityException $e) {
            return false;
        }
    }

    public function afterPrepareAndRender(
        \Magento\Catalog\Helper\Product\View $subject,
        $result,
        $productId,
        $resultPage,
        $params = null,
        $controller
    )
    {
        if ($result instanceof \Magento\Catalog\Helper\Product\View) {
            $this->addMetaData($productId);
        }
        return $result;
    }
}

Note that code is not tested. It is given based idea.

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.