challenge_results.php 6 KB
<?php
/**
 * The template for displaying the Challenge Results page .
 *
 * Template name: Challenge Results 
 *
 * @package storefront
 */
remove_filter('the_content', 'wpautop');
get_header();
?>

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

        <?php while (have_posts()) : the_post(); ?>

            <?php
            do_action('storefront_page_before');
            ?>

            <?php
            ?>
            <div class="page_challengeresults text-center">
                <div class="bg2 text-center">
                    <div class="container">
                        <?php
                        if (isset($_POST['number_cans']) && isset($_POST['amount_pay'])) {
                            $savings_amount = fgc_calculation_challenge($_POST);
                            if ($savings_amount > 0) {
                                ?>
                                <header class="tv-header">
                                    <h1 itemprop="name" class="tv-title">Great news</h1>		
                                </header>
                                <p class="font20">
                                    If you switch to REIZE Energy Drink you will save   
                                </p>
                                <h3 class="font30 roboto_condensedbold">
                                    $<?php echo $savings_amount; ?> per year!
                                </h3>
                                <p class="font20">
                                    Just think of what you could do with all that cash.  
                                </p>
                            <?php } else { ?>
                                <p class="tvnotified">
                                    WHOA… are you claiming that your energy drink is even cheaper than REIZE? We don’t believe it. Has it expired? Is it priced in pesos? Was it given to you by a homeless person?

                                    Whatever it is, there are plenty of other great reasons to drink REIZE. Have a look at the 10 Reasons to Buy REIZE at the bottom of the "Our Drink" page. Or if you made a typo, then take the REIZE Energy Challenge again.
                                </p>
                            <?php } ?>
                        <?php } else { ?>
                            <p class="tvnotified"><a class="aborder" href="/our-drink">Click here</a> to take the REIZE Energy Drink challenge and find out how much money you could save by switching to REIZE.</p>
                        <?php } ?>
                        <div class="form-group"></div>
                        <img class="inline_block" src="<?php echo get_site_url() . '/wp-content/themes/reize/assets/images/ChallengeResults.png' ?>"/>
                        <?php if ($savings_amount > 0) { ?>
                            <div class="tvsocial">
                                <h2 class="font32 roboto_condensedbold">
                                    invite a friend to take the challenge
                                </h2>
                                <?php
                                echo do_shortcode('[jpshare]');
                                ?>
                            </div>
                        <?php } ?>
                    </div>
                </div>
                <div class="bg3">
                    <div class="container">
                        <div class="font22 text-uppercase footer_page">
                            <a href="/get-started">
                                <span class="robotoslab">start saving today.</span> <span class="robotoslabbold"><b>join the club</b></span>  
                            </a>
                        </div>
                    </div>
                </div>
            </div>
            <?php
            /**
             * @hooked storefront_display_comments - 10
             */
            do_action('storefront_page_after');
            ?>

        <?php endwhile; // end of the loop.    ?>
        <?php 
        $our_link_page = get_page_by_title('OUR DRINK', OBJECT, 'page');
        $link_page = get_permalink($our_link_page->ID);
        $link_page_replaced = str_replace(array('http://', 'http://wwww.'), array('', ''), $link_page);
        ?>
        <input type="hidden" id="text_twitter" value="Take the REIZE Energy Drink Challenge! <?php echo $link_page_replaced; ?> pic.twitter.com/irO6DrDLOt" />
        <input type="hidden" id="text_facebook" value="Take the REIZE Energy Drink Challenge today!" />
    </main><!-- #main -->
</div><!-- #primary -->
<?php get_footer(); ?>
<?php

/**
 * calculate savings amount
 * @param type $data
 * @return int
 */
function fgc_calculation_challenge($data) {
    $yearly_drink = array("144", "360", "720", "1080", "1440", "1800", "2160", "2520", "2880", "3240", "3600", "3960", "4320", "7200");
    $Yearly_Subscription_Cost = array("120", "300", "540", "672", "840", "1032", "1200", "1368", "1500", "1656", "1800", "1980", "2100", "3108");
    $yearly_arr = array();
    for ($i = 0; $i < count($yearly_drink); $i++) {
        $yearly_arr[$yearly_drink[$i]] = $Yearly_Subscription_Cost[$i];
    }

    $number_cans = $data['number_cans'];
    $amount_pay = preg_replace("/[^0-9\.]/", '', $data['amount_pay']);
    if (is_numeric($number_cans) && is_numeric($amount_pay)) {
        $yearly_cost = $number_cans * $amount_pay * 365;
        $number_of_energy_drinks = $number_cans * 365;
        $closest_plan = fgc_closest($yearly_drink, $number_of_energy_drinks);
        $Yearly_Cost_of_Closest_Plan = $yearly_arr[$closest_plan];
        $savings_amount = $yearly_cost - $Yearly_Cost_of_Closest_Plan;
        //echo $yearly_cost.'--'.$number_of_energy_drinks.'--'.$closest_plan.'--'.$Yearly_Cost_of_Closest_Plan.'--'.$savings_amount;
        return number_format($savings_amount);
    } else {
        return 0;
    }
}

function fgc_closest($array, $number) {

    sort($array);
    $arr = array();
    foreach ($array as $a) {
        if ($a <= $number) {
            $arr[] = $a;
        }
    }
    return max($arr); // or return NULL;
}
?>