Footer Caching Workaround.. what's the right way to do this? - magento

I have the following chunk of code in my footer.phtml
<!-- Customer Modal -->
<?php if (array_key_exists("customer_select", $_COOKIE) == FALSE ): ?>
<script type="text/javascript" src="<?=$this->getSkinUrl('js/jqModal.js')?>"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#customer_type_dialog").jqm().jqmShow();
});
</script>
<?php include("../../templates.eu/templates.sbc.de/_includes/customer_select.php"); ?>
<?php endif; ?>
When I turn on the block caching it stops evaluating this an only returns the value that was cached. HOw do I work around this problem or what is a better way to do this? Basically I'm checking to see if a cookie exists and if it does I'm adding some js and another file to the page.

Put the above code in it's own template file and make this layout change:
<reference name="before_body_end">
<block type="core/template" template="your/cookie/check/file.phtml" />
</reference>
This way the footer gets to be cached (which helps keep pages fast) and your cookie check is evaluated each time. Also the Javascript is left until the last possible point of the page which is a best practice for inline scripts.

Related

Magento 1.9: Add custom block to header

I have this in my local.xml.
<reference name="header">
<block type="core/template" name="wc-customheadblock" template="wc-customheadblock.phtml" />
</reference>
Inside wc-customheadblock.phtml i have:
<div style="height:100px; background-color:green">
<h1>This is the wc custom head block</h1>
</div>
But this block is not appearing in the header. I've tried the same for the sidebars (by changing the xml reference name) and it works just fine. Why does this not work for the header?
This site claims its possible to make add blocks to the header using local.xml!
http://www.classyllama.com/development/magento-development/the-better-way-to-modify-magento-layout
Go to your header.phtml file (app/design/frontend/package/theme/page/html/header.phtml) and place one of the following lines:
<?php echo $this->getChildHtml(); ?>
or
<?php echo $this->getChildHtml('wc-customheadblock'); ?>
Most likely header doesn't render child blocks by default because it would cause some template crashes. On the sidebars, extra childs can be easily appended at the end.

Change location to insert HTML Youama Ajax Login & Registration - Magento

I'm using the extension "Youama Ajax Login and Register", and it inserts an html page that serves as a modal window (lightbox popup or do not know how to call it).
however, it inserts the html on:
<div class="wrapper">
<div class="page">
<div class="main-container">
<div class="main">
<div class="col-main">
<div class="col-home">
/* [HERE] */
I wish he was inserted after the body.
any idea how to do this?
You would need to identify the XML that Youama is using to place that block at that point in you code and then move it to be rendered as a child of the block after_body_start.
So for example in your local.xml file you could do something like...
<default>
<!-- Youama Ajax Login and Register -->
<reference name="after_body_start">
<block type="youama/ajaxlogin" name="youama_login" as="youama_login" template="youama/ajaxlogin.phtml" />
</reference>
</default>
Please note I made all the attribute values up - you will need to copy them from the Youama XML for the block.
You will also need to remove the Youama block from it's current assignment or you will have it on your page twice. Which might be something like
<reference name="content">
<action method="unsetChild"><name>youama_login</name></action>
</reference>
Plan B : write some JavaScript to rip the DOM node out from its current posisiton and re-insert it after <body> but that is a bit of a hack and not really encouraged.
Either way you might have to check the CSS selectors in case moving the HTML changes the CSS paths to the nodes that are being styled or selected via JavaScript.

How to get Magento extension to run javascript when item added to cart

I'm developing a Magento extension and having trouble trying to get it to run javascript when an item is added to cart.
In config.xml I have an observer
<checkout_cart_product_add_after>
In Observer.php
public function itemAddedToCart(Varien_Event_Observer $observer){}
This is firing (I can test by echo-ing). But how do I get it to inject a block of javascript (preferably into the footer)?
Thanks in advance.
UPDATE:
Used Chris' solution, but instead of registry I used the session to avoid potential multi-user issues:
In Observer.php
public function itemAddedToCart(Varien_Event_Observer $observer){}
$itemAddedToCart = 'true';
Mage::getSingleton('core/session')->setItemAddedToCart($itemAddedToCart);
In my 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 this helps others.
You should be able to accomplish this via XML in the layout XML file of your 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 you delete your cache because XML updates are only applied after refreshing your cache.
If you are using the default Magento theme you don't need to call this template because it automatically calls all children $this->getChildHtml('');.
But if you are using a custom theme you should add this to your footer.phtml: $this->getChildHtml('INSERT_CUSTOM_NAME_HERE');.
Or if you'd like to have the javascript on the cart page you should replace <catalog_product_view> with <checkout_cart_index>.

Moving Magento "Add to Cart" button to new file not working

I want to move Add to cart button from view.phtml file to 2columns-right.phtml file and I am cant make it work. What I did is that I copied <?php echo $this->getChildHtml('addtocart') ?> from view.phtml file to 2columns-right.phtml and it does not appear at all. I did Flush Magento cache too but nothing again.
Any suggestions on how to make this work?
You should get familiar with magento layout system. To make possible <?php echo $this->getChildHtml('addtocart') ?> work in 2columns-right.phtml this block should be declared as child block of root block (the root is the block that is rendered with 2columns-right.phtml). Actually, I don't see much sense in moving add to cart to other template, because addtocart.phtml itself is just a button that submits whole form that is located at catalog/product/view.phtml. If you take it out of there it won't work.
First of all i agree with nevermourn you can not get childhtml if you havn't declared it. But you can use
<?php echo $this->getLayout()->createBlock('catalog/product_view')->setTemplate('catalog/product/view/addtocart.phtml')->toHtml(); ?>
By using this in 2columns-right.phtml you will get addtocart.phtml for sure.
in order to call add to cart button by using
<?php echo $this->getChildHtml('addtocart') ?>
on the page you desire in layout/local.xml
<yourModule_YourController_yourAction>
<reference name="content">
<block type="catalog/product_view" name="product.info.addtocart" as="addtocart" template="catalog/product/view/addtocart.phtml"/>
</reference>
</yourModule_YourController_yourAction>

Magento block file is not showing up some times

Catalog.xml
<default>
<!-- Mage_Catalog -->
<reference name="top.menu">
<block type="catalog/navigation" name="catalog.topnav" template="catalog/navigation/top.phtml">
<block type="catalog/navigation" name="topnav_extra" template="catalog/navigation/top_extra.phtml" />
</block>
</reference>
top_extra.phtml
<ul>
<li> Custom Menu1 </li>
<li> Custom Menu2 </li>
</ul>
top.phtml
<?php if($_menu): ?>
<ul id="topnav">
<?php echo $_menu ?>
<!-- Header Menu laset tab start here -->
<?php echo $this->getChildHtml('topnav_extra'); ?>
</ul>
This block is not showing sometimes when i refreshing twice or thrice it's coming. I am doing reindexing and cache refreshing on daily using cron. But i dont know why its coming randomly?
That sounds like a caching issue to me. Try calling your custom menu via PHP instead of XML
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('block_id')- >toHtml(); ?>
This will alleviate any issues you might encounter using the XML files. Hope this helps!
<?php echo $this->getLayout()->createBlock('core/template')->setTemplate('catalog/navigation/top_extra.phtml')->toHtml(); ?>
i include that file with out layout handles. I am not sure is the right way to do this? but its showing up. I put log to know how many times its not showing up.. Will see

Resources