1

Hello I would like to modify the product view page, and inject php into it via my extension. I tried doing it via editing the transport block, but it can only add html files and not php files. I also tried appending an additional layout externally...to no avail.

if ($block  instanceof Mage_Catalog_Block_Product_View)
{
    $block = $observer->getLayout()->createBlock('demo\productrender.phtml');
    $observer->getLayout()->getBlock('content')->append($block);
}

The code which i need to inject externally:

// Facebook
<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:myAjax()">Ajax</a>
<?php echo Mage::app()->getRequest()->getControllerName(); ?>
// return module name
<?php echo $this->getUrl('demo/Ajax/index') ?>
<script>
    function myAjax() {
        new Ajax.Request("<?php echo $this->getUrl('demo/Ajax/index') ?>", {
            method: 'Post',
            parameters: {
                "productinfo":<?php echo $_product->getSku()?>,
                "productprice":<?php echo $_product->getPrice()?>
            },
            onComplete: function (transport) {

                alert(transport.responseText);

            }
        });
    }
</script>
2
  • What do you mean by injecting php files? What you want to do with those php files? Please specify something more and useful.
    – MagePsycho
    Commented May 25, 2016 at 10:11
  • MagePsycho, I am developing an extension, which should add social media buttons to a product view page. The onclick function of those social media buttons fires ajax call to a controller, which applies discounting logic. so far all of this has been hard coded in the view.phtml file of the product. But my extension needs to do it by externally injecting the phtml code, which also includes php. I have updated my original post, with the code(currently hardcoded in the view.phtml file of the rwd theme), which i need to externally inject:
    – anant
    Commented May 25, 2016 at 10:16

1 Answer 1

0

You can simply inject the cusotm phtml file via your layout xml as:

<catalog_product_view>
    <reference name="product.info.additional">
        <block type="your-module-alias/some_block_class" name="product.info.sharing" template="demo/productrender.phtml"/>
    </reference>
</catalog_product_view>   
2
  • thanks! this looks useful. I am a newbie to magento, so would be great if you could clarify the following: 1.where do I add this file? 2. and does the code necessarily need to be inside a class ? I just want the second block of code i pasted in my question inside the block. my module name is demo and inside the block file there is a .phtml file called productrender.phtml
    – anant
    Commented May 25, 2016 at 11:48
  • also how do i control the positioning where the phtml is inserted...and is it installable bz the installer when i package my extension into one?
    – anant
    Commented May 25, 2016 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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