add coupon code to whmcs order form

1- add smarty plugin in this path includes/smarty/plugins

<?php

/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     function.check_coupon.php
 * Type:     function
 * Name:     check_coupon
 * Purpose:  check coupon code and return info
 * -------------------------------------------------------------
 */

function smarty_function_check_coupon($params, $smarty) {
global $smarty;
    if ($_GET['promocode']) {

        $command = "getpromotions";
        $adminuser = "admincpo";
        $values["code"] = $_GET['promocode'];

        $results = localAPI($command, $values, $adminuser);
//print_r(strtotime($results['promotions']['promotion'][0]['expirationdate']));
        $coupon_types = array("Percentage", "Fixed Amount", "Price Override");

if(time() > strtotime($results['promotions']['promotion'][0]['expirationdate']) && strtotime($results['promotions']['promotion'][0]['expirationdate']) > 0){
$expired = true;
 echo '<div class="alert alert-block alert-error">
    <strong>Oh!</strong> This coupon code has Expired.
    </div>
';
}

        if ($results['totalresults'] > 0 && in_array($results['promotions']['promotion'][0]['type'], $coupon_types) && !$expired) {

            echo '<div class="alert alert-block alert-success">
    <strong>Congratulations!</strong> You have selected our most valuable offer.
    </div>
';

            $coupon_recurring = '';
            if ($results['promotions']['promotion'][0]['recurring'] == 1) {
                $coupon_recurring = 'Recurring';
            } else {
                $coupon_recurring = 'One time';
            }

            $products_appliesto = array();
            $products_appliesto = explode(",", $results['promotions']['promotion'][0]['appliesto']);
            $coupon_type = $results['promotions']['promotion'][0]['type'];
            $coupon_value = $results['promotions']['promotion'][0]['value'];

            $myproducts = array();
            $myproducts = $smarty->get_template_vars('products');
            $mycurrency = $smarty->get_template_vars('currency');

            foreach ($myproducts as $key => $myproduct) {

                if (in_array($myproduct['pid'], $products_appliesto)) {

                    switch ($coupon_type) {
                        case 'Percentage':
                            $old_price = $myproduct['pricing']['minprice']['price'];
                            $new_price = $myproduct['pricing']['rawpricing'][$myproduct['pricing']['minprice']['cycle']] - (($coupon_value / 100) * $myproduct['pricing']['rawpricing'][$myproduct['pricing']['minprice']['cycle']]);
                            $coupon_phrase = $coupon_recurring . " " . number_format($coupon_value) . "% Discount";
                            break;
                        case 'Fixed Amount':
                            $old_price = $myproduct['pricing']['minprice']['price'];
                            $new_price = ($myproduct['pricing']['rawpricing'][$myproduct['pricing']['minprice']['cycle']] - $coupon_value);
                            $coupon_phrase = $coupon_recurring . " " . $mycurrency['prefix'] . number_format($coupon_value) . " off";
                            break;
                        case 'Price Override':
                            $old_price = $myproduct['pricing']['minprice']['price'];
                            $new_price = $coupon_value;
                            break;
                    }

                    $cycling = '<br /><span style="font-size: 12px;line-height: 22px;">' . ucfirst($myproduct['pricing']['minprice']['cycle']) . '</span>';
                    if ($results['promotions']['promotion'][0]['recurring'] != 1) {
                        $cycling = '<br /><span style="font-size: 12px;line-height: 22px;">First Month</span>';
                    }

                    $myproducts[$key]['pricing']['minprice']['price'] = '<span style="font-size: 12px;line-height: 22px;">' . $coupon_phrase . '</span><br /><span style="background-color: rgb(255, 255, 255); border: 1px solid rgb(204, 204, 204); border-radius: 5px 5px 5px 5px; color: rgb(255, 102, 0); padding: 5px; margin: 5px 0px 5px 5px; line-height: 50px; text-align: center;">' . $mycurrency['prefix'] . $new_price . ' ' . $mycurrency['suffix'] . '</span>' . $cycling . '<br /><br /><span style="font-size: 14px; color: red;"><del>' . $old_price . '</del></span>';
                }
            }

            $smarty->assign('products', $myproducts);
        }
    }
}

?>

then in the file

Leave a Reply

Your email address will not be published. Required fields are marked *