Can anyone guide me how can i add social share buttons in Magento 2 ?
Magento – add social share buttons in Magento 2
magento2social-buttons
Related Solutions
As far as I know it is not possible anymore to pass parameters via URL to the share function.
The best practice in my opinion is to implement open graph tags in your website, then facebook is able to extract the correct information. More on open graph tags can be found in the facebook documentation.
https://developers.facebook.com/docs/sharing/best-practices#tags
The most important og tags for your purpose might be the description and the image.
<meta property="og:description" content="MyFavNews is the best source for your favorite news." />
<meta property="og:image" content="http://graphics.myfavnews.com/images/logo-100x100.jpg" />
The upside of the og tags is that they also work if someone post your URL manually on facebook.
For your requirement,you need to some modification
at module.phtml and Mymodule.php.
Instead of loading all product at myfunction() and you need to send product Object
to call from list.phtml and product/view.phtml
.
Like: setProduct($project)
.
<?php class Companysocial_Mymodule_Block_Mymodule extends Mage_Core_Block_Template { protected $_product = null; public function setProduct($product) { $this->_product = $product; return $this; } public function myfunction() { return $this->_product; }
}
Then add below code at module.phtml:
$product=$this->myfunction()
$sku = $product->getSku();
$productUrl = $product->getProductUrl();
$productImage = $product->getImageUrl(); ?>
<a href="javascript:popWin('https://plus.google.com/share?url=<?php echo urlencode($productUrl); ?>', 'google', 'width=640,height=480,left=0,top=0,location=no,status=yes,scrollbars=yes,resizable=yes');" title="<?php echo $this->__('Share on Google Plus') ?>">Google Plus</a>
<a href="javascript:popWin('https://www.facebook.com/sharer/sharer.php?u=<?php echo urlencode($productUrl); ?>&t=<?php echo urlencode($productName); ?>', 'facebook', 'width=640,height=480,left=0,top=0,location=no,status=yes,scrollbars=yes,resizable=yes');" title="<?php echo $this->__('Share on Facebook') ?>">Facebook</a>
<a href="javascript:popWin('http://twitter.com/home/?status=<?php echo urlencode($productName . ' (' . $productUrl . ')'); ?>', 'twitter', 'width=640,height=480,left=0,top=0,location=no,status=yes,scrollbars=yes,resizable=yes');" title="<?php echo $this->__('Tweet') ?>">Twitter</a>
<a href="javascript:popWin('https://pinterest.com/pin/create/button/?url=<?php echo urlencode($productUrl); ?>&media=<?php echo urlencode($productImage); ?>&description=<?php echo urlencode($productName); ?>', 'pinterest', 'width=640,height=480,left=0,top=0,location=no,status=yes,scrollbars=yes,resizable=yes');" title="<?php echo $this->__('Pin it') ?>">Pinterest</a>
Let call this code at list.phtml.
<?php echo $this->getLayout()->createBlock('mymodule/mymodule') ->setProduct($_product)->setTemplate('mymodule/mymodule.phtml')->toHtml();
?>
You need to call this code below each product in list.phtml
Also you can call share block using below code:
<?php echo $this->getLayout()->createBlock('mymodule/mymodule')->setProduct($_product)->setTemplate('mymodule/mymodule.phtml')->toHtml(); ?>
Note that in my process you does not need mymodule.xml
Best Answer
If you want to install extension which are easy to implement then prefer below url it is working
https://marketplace.magento.com/magebuzz-socialshare.html
If you want to do custom code
Refer:-https://www.cloudways.com/blog/add-social-share-buttons-magento-2/