Conditional Coupon PHP Code

griffin

Member
I have a php form plugin for a promotional coupon which provides discount on fee for a course as follows:

$coupon_code = JRequest::getVar('register___coupon','');
if (!empty($coupon_code)) {
$db =& JFactory::getDBO();
$db->setQuery("SELECT fee FROM coupons WHERE code = " . $db->Quote($coupon_code));
$discount = $db->loadResult();
if (!empty($discount)) {
$price = $discount;
$formModel->updateFormData('register___fee', $price);
$formModel->updateFormData('register___fee_raw', $price);
}
}

This works fine with the register___fee being updated on the form on submission etc checking the coupon code entered matches the codes in the coupon table.

It displays the coupon price in a cascading dropdown element watching coupon_code entered which has element WHERE QUERY checking expiry date on coupons table is greater than current date.

As you would expect though, a cunning user decided to enter the code from one promotion into a registration for a course that wasn't eligible for the discounted rate.

I would be very grateful for some advice/help on how to include a condition where:

"coupons___location = register___location"

and how to display message "Not Valid" where coupon not valid for that location.

Cheers
 
For the where clause, just grab $register_location the same way you grab the code (although JRequest has been deprecated, and you should now use this method ...

Code:
$app = JFactory::getApplication();
$register_location = $app->input->get('register___location');
// if location is a join it'll be an array and you'll need to grab the first entry ...
$register_location = is_array($register_location) ? $register_location[0] : $register_location;

and add...

Code:
" AND location = " . $db->Quote($register_location)"

to the query.

You could abort the submission by returning false, and put up a message ...

Code:
$app->enqueueMessage('Naughty!");
return false;

I think returning false aborts submission ... if not we'll work out how to do it ...

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top