I am working on product share part within Magento. As per link product link only get shared on social website. I want to share product with its image and details.
Magento – share product with product image and details on social media websites
productsocial-buttons
Related Solutions
If you know the storeId, set in setStoreId :
/**
* call the Magento catalog/product model
* set the current store ID
* load the product
*/
$product = Mage::getModel('catalog/product')
->setStoreId($storeId)
->load($key);
Display in a block :
echo $product->getName();
echo $product->getPrice();
We can also use print_r to see the values :
print_r($product->getData());
The following code will show current store ID :
$storeId = Mage::app()->getStore()->getId();
If you change the $storeId will show different product.
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
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.
The upside of the og tags is that they also work if someone post your URL manually on facebook.