Adding the Pinterest “Pin it” Button to Magento Product Detail Pages
In this post, we’ll guide you through integrating the Pinterest “Pin it” button into your Magento product detail pages. This allows your customers to easily share your products on Pinterest with a single click.
Understanding the Pin It Button Parameters
The Pin It button from Pinterest requires three main parameters to function correctly:
- URL: The URL of the page containing the image.
- Media: The direct link to the image file.
- Description: A description of the pinned image.
Step 1: Getting the Product URL and Name
You’re already on the right track with your attempts. You’ve successfully retrieved the product’s URL and name using Magento registry:
<?php echo trim(Mage::registry('current_product')->getProductUrl()) ?>
and
<?php echo Mage::registry('current_product')->getName(); ?>
These lines of code ensure that you have the necessary information for the Pin It button’s URL and description parameters.
Step 2: Retrieving the Product Image
The issue lies in retrieving the product image. Let’s update your code to correctly initialize the image helper:
<?php echo Mage::helper('catalog/image')->init($_product, 'small_image')->resize(900, 900) ?>
Ensure that you’re passing the correct product object ($_product) and the appropriate image type (‘small_image’ for a small thumbnail).
Step 3: Building the Pin It Button
Now, let’s construct the HTML for the Pin It button using your retrieved parameters:
<a href="http://pinterest.com/pin/create/button/?url=<?php echo trim(Mage::registry('current_product')->getProductUrl()) ?>&media=<?php echo Mage::helper('catalog/image')->init($_product, 'small_image')->resize(900, 900) ?>&description=<?php echo Mage::registry('current_product')->getName(); ?>" class="pin-it-button" count-layout="none">Pin It</a>
The count-layout attribute is set to “none” to remove the pin count next to the button.
Step 4: Including the Pinterest JavaScript
Finally, don’t forget to include the Pinterest JavaScript file in your product detail page layout:
<script type="text/javascript" src="http://assets.pinterest.com/js/pinit.js"></script>
This JavaScript is responsible for rendering and functioning the Pin It button.
Step 5: Placing the Button in Your Layout
To place this button in your product detail pages, you can either:
- Override the
catalog/product/view.phtmltemplate file and add the code there. - Create a custom block using XML layout updates (a more sustainable approach).
For detailed instructions on creating custom blocks, refer to our guide on Creating Custom Blocks in Magento at Laravel Company.
Conclusion
By following these steps and ensuring you have the correct parameters for the Pin It button, you can seamlessly integrate Pinterest sharing into your Magento e-commerce site. This feature provides an additional channel for customers to engage with your products, potentially driving more traffic and sales through social sharing.
Remember, it’s always essential to test your implementation thoroughly after making changes to ensure that everything functions as expected in your specific store configuration.
Happy coding!