GordonLesti / Lesti_Fpc

Simple Magento Fullpagecache
https://gordonlesti.com/lesti-fpc-documentationversion-1-4-5/
Other
358 stars 159 forks source link

Advice on "coupon apply" success block on home page. #303

Open gety9 opened 7 years ago

gety9 commented 7 years ago

We are using module that applies coupon using link. Here example of the link: http://www.sitename.com/applycoupon/?code=OFFNA10&return_url=http://www.sitename.com

if customer coming to the website through this link it, redirects to homepage and has pop-up window saying coupon applied successfully and applies OFFNA10 coupon.

Currently we make this block on home page dynamic in Lesti_FPC, but this makes this block's code run on every home page load. I wonder if there is a better way to do?

Basically for visitors coming straight to our site we want to display regular cached home page, and for visitors coming through this coupon links, we want to regenerate the block or full home page. (basically home pages are identical in both cases except for in 2nd case it has pop-up hidden div which becomes visible with javascript)

Here is the code: xml

<layout version="0.1.0">
  <default translate="label" module="page">
    <reference name="after_body_start">
        <block type="core/template" name="applycoupon" template="applycoupon/applycoupon.phtml"/>
    </reference>
  </default>
</layout>

phtml

<?php
    if (Mage::getStoreConfig('applycoupon_section/applycoupon_group/active')) {
        $getScript =Mage::getSingleton('core/session')->getMiscellaneous_Scripts();
        if ($getScript == 'script') {
            $script = Mage::getStoreConfig('applycoupon_section/applycoupon_group/miscellaneous_script');
            echo "<script type='text/javascript'>$script</script>"; 
        }
        Mage::getSingleton('core/session')->unsMiscellaneous_Scripts();
        ?>
        <div style="display: none"><?php echo $spopup =Mage::getSingleton('core/session')->getMyPopup(); $cpopup =Mage::getSingleton('core/session')->getMyCPopup(); ?></div>
        <?php
        if ($spopup != NULL || $cpopup != NULL ) { ?>

            <script>
                jQuery.noConflict();
            jQuery(function(){

            var appendthis =  ("<div class='modal-overlay js-modal-close'></div>");

                jQuery( document ).ready(function(e) {
                    //e.preventDefault();
                jQuery("body").append(appendthis);
                jQuery(".modal-overlay").fadeTo(500, 0.7);
                //$(".js-modalbox").fadeIn(500);
                    var modalBox = 'popup1';
                    jQuery('#'+modalBox).fadeIn(jQuery(this).data());
                });  

            jQuery(".js-modal-close, .modal-overlay").click(function() {
                jQuery(".modal-box, .modal-overlay").fadeOut(500, function() {
                    jQuery(".modal-overlay").remove();
                });

            });

            jQuery(window).resize(function() {
                jQuery(".modal-box").css({
                    top: (jQuery(window).height() - jQuery(".modal-box").outerHeight()) / 2,
                    left: (jQuery(window).width() - jQuery(".modal-box").outerWidth()) / 2
                });
            });

            jQuery(window).resize();

            });
            </script><?php 
        }
        if ($spopup == 'popup') { 
                $successmsg = Mage::getStoreConfig('applycoupon_section/applycoupon_group/success_message');
                ?>

                <div id="popup1" class="modal-box">

                  <a href="#" class="js-modal-close close">×</a>

                  <div class="modal-body">
                  <div class="coupon-successfully-img">
                    <img src="<?php echo $this->getSkinUrl('images/applycoupon/coupon-successfully.png') ?>">
                  </div>
                  <div class="coupon-successfully-text">
                    <?php echo $successmsg; ?>
                  </div>

                  </div>

                </div>

                <?php

            }elseif ($cpopup == 'cpopup') {
                $coupon_code =Mage::getSingleton('core/session')->getMyCouponcode();
                ?>
                <div id="popup1" class="modal-box">

                  <a href="#" class="js-modal-close close">×</a>

                  <div class="modal-body">
                  <div class="coupon-successfully-img">
                    <img src="<?php echo $this->getSkinUrl('images/applycoupon/coupon-expired.png') ?>">
                  </div>
                  <div class="coupon-successfully-text">
                    <?php echo 'Coupon code "' . $coupon_code . '" is not valid.'; ?>
                  </div>

                  </div>

                </div>
                <?php
            }
            Mage::getSingleton('core/session')->unsMyPopup();
            Mage::getSingleton('core/session')->unsMyCPopup();
            Mage::getSingleton('core/session')->unsMyCouponcode();

    }
    ?>

indexcontroller

<?php

class LinkstureDCCL_ApplyCoupon_IndexController extends Mage_Core_Controller_Front_Action

{

    public function indexAction() 

    {

        if (Mage::getStoreConfig('applycoupon_section/applycoupon_group/active')) {

            $coupon_code = $this->getRequest()->getParam('code');

            $return_url=$this->getRequest()->getParam('return_url');

            if ($coupon_code != '') 

            {

                $c_available = 0;

                $applycoupon = Mage::getModel('applycoupon/applycoupon');

                $collections = $applycoupon->getCollection();

                $websiteId = Mage::app()->getStore()->getWebsiteId();

                foreach ($collections as $collection) {

                    if ($collection->getCouponCode() == $coupon_code && $collection->getWebsites() == $websiteId && $collection->getStatus() == 1) {

                        $nid=$collection->getId();

                        $views=$collection->getViews();

                        $c_available = 1;

                    }

                }

                if ($c_available == 1) {

                    $views += 1;

                    $applycoupon->setData(array('id' => $nid,'views' => $views));

                    $applycoupon->save();

                }else{

                    $s_rules = Mage::getResourceModel('salesrule/rule_collection')->load();

                    foreach ($s_rules as $s_rule) {

                        if ($s_rule->getIsActive() == 1 && $s_rule->getCode() == $coupon_code) {

                            $c_available = 1;

                        }

                    }

                }

                if ($c_available == 1) {

                        Mage::getSingleton("checkout/session")->setData("coupon_code",$coupon_code);

                        Mage::getSingleton('checkout/cart')->getQuote()->setCouponCode($coupon_code)->save();

                        Mage::getSingleton('core/session')->setMiscellaneous_Scripts('script'); 

                        if(Mage::getStoreConfig('applycoupon_section/applycoupon_group/popup')) {

                            Mage::getSingleton('core/session')->setMyPopup('popup');

                        }else{

                            $successmsg = Mage::getStoreConfig('applycoupon_section/applycoupon_group/success_message');

                            Mage::getSingleton('core/session')->addSuccess($successmsg);

                        }

                    }else{

                        if(Mage::getStoreConfig('applycoupon_section/applycoupon_group/popup')) {

                            Mage::getSingleton('core/session')->setMyCPopup('cpopup');

                            Mage::getSingleton('core/session')->setMyCouponcode($coupon_code);

                        }else{

                            Mage::getSingleton('core/session')->setMyCouponcode($coupon_code);

                            Mage::getSingleton('core/session')->addError('Coupon code "' . $coupon_code . '" is not valid.');

                        }

                    }

            }

            else 

            {

                Mage::getSingleton("checkout/session")->setData("coupon_code","");

                $cart = Mage::getSingleton('checkout/cart');

                foreach( Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item ) 

                {

                    $cart->removeItem( $item->getId() );

                }

                $cart->save();

            }

            if($return_url == 'no' || $return_url == '' || $return_url == NULL)

            {

                if (isset($_SERVER['HTTP_REFERER']))

                {

                    $simple = $_SERVER['HTTP_REFERER']; 

                }

                else

                {

                    $simple = '';

                }

                 if($simple == ''){

                    $simple = Mage::getUrl();

                 }

                header('Location: ' . $simple);

                exit();

            }

            else

            { 

                header("Location: ".$return_url);

                exit();

            }

        }else{

            header("Location: ".Mage::getUrl('errors/404'));

            exit();

        }

    }

}
GordonLesti commented 7 years ago

Hello @gety9 unfortunately I didn't see a better soluation for your problem. Maybe a lazy block and overwriting Lesti_Fpc_Helper_Block::_getLazyBlocksValidHash, but I wouldn't recommend that solution. In general is your solution fine, but I would recommend to clean up the code a bit. Putting logic into a block class instead of a template and so on.