Question

I want to share the product image in WhatsApp, But I don't know the meta concept.

How to use meta tag in phtml file to share image ?

Here phtml file code, but using this display only url and description.

I want to send description with image.

    <?php
        $helper = $this->helperInit();
        $objectManagerInstance = \Magento\Framework\App\ObjectManager::getInstance();
        $url = $objectManagerInstance -> get('Magento\Framework\UrlInterface');
        $currentUrl = $url -> getCurrentUrl();
        $biturl =  $this->getShortenUrl($currentUrl);    
        $active = $helper->whatsappshare_enable();
        $size = $helper->whatsappshare_size();
        $text = $helper->whatsappshare_text(); 
        $product = $this->getCurrentProduct();
        $p_name = $product->getName();
        $description = $product->getDescription();
        $imgUrl = $this->getImageUrl($currentUrl);
        ?>

    <?php 
        $size_class = "whatsapp_small";
        if($size == 1) {
            $size_class = "whatsapp_small";
        } else if($size == 2) {
            $size_class = "whatsapp_medium";
        } else if($size == 3) {
            $size_class = "whatsapp_large";
        }
        if($helper->isMobile() || $helper->isTablet()) {
    ?>
      <div class="whatsapp_share <?php echo $size_class; ?>">
                <a href="whatsapp://send?text=<?php echo $text?>%0A%0A<?php echo $p_name?>%0A%0A<?php echo $biturl;?>"><?php echo __('Share');?></a>
            </div>  

            <?php 
            } else {
            ?>
            <div class="whatsapp_share <?php echo $size_class; ?>">
                <a href="https://web.whatsapp.com/send?text=<?php echo $text?>%0A%0A<?php echo $p_name?>%0A%0A<?php echo $biturl;?>%0A%0A" target="_blank"><?php echo __('Share');?></a>
            </div>
            <?php
                }
            ?>
Was it helpful?

Solution

Here head.phtml file add into layout

<?php 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product ?>
    <?php if ($product): ?>
        <meta property="og:title" content="<?php echo ($product->getName()); ?>" />
        <meta property="og:type" content="product" />
        <meta property="og:image" content="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(200,200);?>" />
        <meta property="og:url" content="<?php echo $product->getProductUrl(); ?>" />
        <meta property="og:description" content="<?php echo strip_tags(($product->getShortDescription())); ?>" />
        <meta property="og:site_name" content="<?php echo $product->getStore()->getName(); ?>" />
    <?php endif;?>

Add below code into layout xml file

<body>      
    <referenceBlock name="head.additional">
        <block class="Magento\Framework\View\Element\Template" name="test" template="head.phtml" after="-"/>
    </referenceBlock> 
</body>

Just add this head.phtml file and it'ill run automatically and give image in sharing msg.But this is not work with local system.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top