aneesahmadweb@gmail.com     03040836207

    Get Current product

    $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
    
    $registry = $objectManager->get('\Magento\Framework\Registry');
    
    $currentProduct = $registry->registry('current_product');
    
     $currentProduct->getId();
    ?>
    
    

    To Get Current category and parent category on category page phtml file

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
          $current_category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');
          $parent_category = $current_category->getParentCategory();
          echo $current_category->getName();
          echo $parent_category->getName(); ?>

    To add a Custom block under Product title on Product Page

    Paste below code in catalog_product_view.xml

    <referenceBlock name="product.info.main">
       <block class="Magento\Catalog\Block\Product\View" name="block.namemy"  before="-" as="block.alias"
             template="Magento_Catalog::product/title.phtml">
       </block>
    </referenceBlock>
    <move element="block.namemy" destination="product.info.main" after="page.main.title" />
    
    

    To Add Social Icon on Product Page

    <?php $url= $block->getProduct()->getProductUrl();?>
    <?php $_product = $block->getProduct(); ?>
    <div class="social-media-icons">
        <!-- Facebook -->
        <a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo $url;?>&t=<?php echo urlencode($_product->getName())?>" onclick='javascript:window.open(this.href,"","width=640,height=480,left=0,top=0,location=no,status=yes,scrollbars=yes,resizable=yes");return false' title="Share on facebook">
            <img src="<?php echo $this->getViewFileUrl('Magento_Catalog::images/facebook-share.jpg'); ?>" alt="Facebook" width="12%">
        </a>
        <!-- TWITTER -->
        <a href="http://twitter.com/home/?status=<?php echo $url;?>(<?php echo urlencode($_product->getName())?>)" onclick='javascript:window.open(this.href,"","width=640,height=480,left=0,top=0,location=no,status=yes,scrollbars=yes,resizable=yes");return false' title="Share on Twitter">
            <img src="<?php echo $this->getViewFileUrl('Magento_Catalog::images/tw.png'); ?>" alt="Twitter" width="5%">
        </a>
        <!-- Pinterest -->
        <a href="https://pinterest.com/pin/create/button/?url=<?php echo $url; ?>&media=<?php echo urlencode($_product->getImageUrl());?> &description=<?php echo urlencode($_product->getName()); ?>" onclick='javascript:window.open(this.href,"","width=640,height=480,left=0,top=0,location=no,status=yes,scrollbars=yes,resizable=yes");return false' title="Pin it">
            <img src="<?php echo $this->getViewFileUrl('Magento_Catalog::images/pinterest-share.jpg'); ?>" alt="Pinterest" width="12%">
        </a>
        <!-- Google Plus -->
        <a href="https://plus.google.com/share?url=<?php echo $url; ?>" onclick='javascript:window.open(this.href,"","width=640,height=480,left=0,top=0,location=no,status=yes,scrollbars=yes,resizable=yes");return false' title="Share on Google Plus">
            <img src="<?php echo $this->getViewFileUrl('Magento_Catalog::images/google-plus-share.jpg'); ?>" alt="Google Plus" width="12%">
        </a>
    </div>
    

    To Shorten the Product name title or to add read more

    <?php  $string=$block->getShorContent($_post);
    
                        if (strlen($string) > 75) {
                            $trimstring = substr($string, 0, 175). ' <a href="#">readmore...</a>';
                        } else {
                            $trimstring = $string;
                        }
                        echo $trimstring;
                        //Output : Lorem Ipsum is simply dum [readmore...][1]
    
    
                        ?>

    About Author

    You may also like

    No Comment

    You can post first response comment.

    Leave A Comment

    Please enter your name. Please enter an valid email address. Please enter a message.