0

Hi i am trying to an extension which has the facility of showing social share button below every product on category page and single product page.

Here is the code of my extension app\code\local\Companysocial\Mymodule\etc\config.xml :

<?xml version="1.0"?>
<config>
    <modules>
        <Companysocial_Mymodule>
            <version>0.1.0</version>    <!-- Version number of your module -->
        </Companysocial_Mymodule>
    </modules>
    <frontend>
        <routers>
            <mymodule>
                <use>standard</use>
                <args>
                    <module>Companysocial_Mymodule</module>
                    <frontName>mymodule</frontName>
                </args>
            </mymodule>
        </routers>
        <layout>
            <updates>
                <mymodule>
                    <file>mymodule.xml</file> <!-- Our layout file name-->
                </mymodule>
            </updates>
         </layout>
    </frontend>
    <global>
        <blocks>
            <mymodule>
                <class>Companysocial_Mymodule_Block</class>
            </mymodule>
        </blocks>
    </global>
</config>

Here is the code for my app\code\local\Companysocial\Mymodule\controllers\IndexController.php :

<?php
class Companysocial_Mymodule_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        $this->loadLayout();
        $this->renderLayout();
    }

}

Here is the code of my app\code\local\Companysocial\Mymodule\Block\Mymodule.php :

    <?php

class Companysocial_Mymodule_Block_Mymodule extends Mage_Core_Block_Template
{
    public function myfunction()
    {

        $products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');

        foreach ($products as $product) {
            $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>

            <?php
        }
    }
}

Now I have added

    <catalog_category_default>
        <reference name="content">
            <block type="mymodule/mymodule" name="mymodule" template="mymodule/mymodule.phtml" />
        </reference>
    </catalog_category_default>
    <catalog_category_layered>
        <reference name="content">
            <block type="mymodule/mymodule" name="mymodule" template="mymodule/mymodule.phtml" />
        </reference>
    </catalog_category_layered>

This In my layout file mymodule.xml

But code is not shown on product page so I tried another method In app\design\frontend\[custom_theme]\default\template\catalog\product\list.phtml :

<?php echo $this->getLayout()->createBlock('mymodule/mymodule')->setTemplate('mymodule/mymodule.phtml')->toHtml(); ?>

This code works but It will show all the social share for all the product below one single product

Then after It I try to kept the social button code I wrote in function myfunction() in Mymodule.php so after it only one product link will be showing below all the product

I have searched about on and find that I have to either override the Event or observer or custom block of magento . But as it is my first plugin so I dont have any idea about it So Please give me the solution How can I override or add my social share button in below product on category page

1

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

0

You don't need to create module for this.

Simply create a local.xml file in theme/layout folder and have this:

<?xml version="1.0"?>
<layout version="0.1.0">
<catalog_category_default>
    <reference name="product_list">
        <action method="setTemplate">
            <template>catalog/product/list_new.phtml</template>
        </action>
    </reference>
</catalog_category_default>

<catalog_category_layered>
    <reference name="product_list">
        <action method="setTemplate">
            <template>catalog/product/list_new.phtml</template>
        </action>
    </reference>
</catalog_category_layered>
</layout>

Then copy all content from thme/template/catalog/product/list_new.phtml and paste in to your new file theme/template/catalog/product/list_new.phtml.

Now, add this line inside <li> tag in list_new.phtml file:

<div class="social-share">
  <?php 
     $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>
</div>

Remember this <div> should be inside <li> (foreach).

Good luck.

0

try this code put in catalog/product/view/addto.phtml

<?php 
            $currentUrl = Mage::helper('core/url')->getCurrentUrl();
        ?>
        <ul class="add-to-links">
            <li>
                <div class="fb-share-button" data-href="<?php echo $currentUrl; ?>" data-layout="button_count" data-size="small" data-mobile-iframe="true"><a class="fb-xfbml-parse-ignore" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%<?php echo $currentUrl; ?>%2F&amp;src=sdkpreparse">Share</a></div>
                <div id="fb-root"></div>
                <script>(function(d, s, id) {
                  var js, fjs = d.getElementsByTagName(s)[0];
                  if (d.getElementById(id)) return;
                  js = d.createElement(s); js.id = id;
                  js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.7&appId=1141295062596906";
                  fjs.parentNode.insertBefore(js, fjs);
                }(document, 'script', 'facebook-jssdk'));</script>
            </li>
            <li>
                <!-- Place this tag where you want the share button to render. -->
                <div class="g-plus" data-action="share" data-annotation="bubble"></div>

                <!-- Place this tag after the last share tag. -->
                <script type="text/javascript">
                  (function() {
                    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
                    po.src = 'https://apis.google.com/js/platform.js';
                    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
                  })();
                </script>
            </li>
            <li>
                <a href="https://twitter.com/share" class="twitter-share-button" data-hashtags="VoidCanvas">Tweet</a>
                <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
            </li>
            <li>
                <a data-pin-do="buttonBookmark" data-pin-save="true" href="https://www.pinterest.com/pin/create/button/"></a>
                <script async defer src="https://assets.pinterest.com/js/pinit.js"></script>
            </li>
        </ul>
0

You can also use following code in .phtml file.

<?php $_product = $block->getProduct(); ?>
<div class="social-media-icons">
<!-- Facebook -->
<a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo urlencode($_product->getProductUrl());?>&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('images/facebook-share.jpg'); ?>" alt="Facebook" width="5%">
</a>
<!-- Pinterest -->
<a href="https://pinterest.com/pin/create/button/?url=<?php echo urlencode($_product->getProductUrl()); ?>&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('images/pinterest-share.jpg'); ?>" alt="Pinterest" width="5%">
</a>
<!-- Google Plus -->
<a href="https://plus.google.com/share?url=<?php echo urlencode($_product->getProductUrl()); ?>" 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('images/googleplus-share.jpg'); ?>" alt="Google Plus" width="5%">
</a>
</div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.