How to get Magento extension to run javascript when item added to cart -
i'm developing magento extension , having trouble trying run javascript when item added cart.
in config.xml have observer
<checkout_cart_product_add_after>
in observer.php
public function itemaddedtocart(varien_event_observer $observer){}
this firing (i can test echo-ing). how inject block of javascript (preferably footer)?
thanks in advance.
update: used chris' solution, instead of registry used session avoid potential multi-user issues:
in observer.php
public function itemaddedtocart(varien_event_observer $observer){} $itemaddedtocart = 'true'; mage::getsingleton('core/session')->setitemaddedtocart($itemaddedtocart);
in custom block (script.phtml):
<?php if($itemaddedtocart) : ?> <script type="text/javascript"> alert(<?php echo '"' . $this->__($itemaddedtocart) . '"' ?>); </script> <?php // clear itemaddedtocart session variable: mage::getsingleton('core/session')->unsitemaddedtocart(); ?> <?php endif; ?>
hope helps others.
you should able accomplish via xml in layout xml file of extension.
<catalog_product_view> <reference name="footer"> <block type="core/template" name="insert_custom_name_here" template="path/to/your/phtml/file.phtml" /> </reference> </catalog_product_view>
make sure delete cache because xml updates applied after refreshing cache.
if using default magento theme don't need call template because automatically calls children $this->getchildhtml('');
. if using custom theme should add footer.phtml: $this->getchildhtml('insert_custom_name_here');
.
or if you'd have javascript on cart page should replace <catalog_product_view>
<checkout_cart_index>
.
Comments
Post a Comment