I am reviving an old thread, sorry about that.
I am using 1.3.9g. I did some code change to ot_gv.php to achieve this, without adding the button or showing the Apply Credit box always.
Change is to collect_posts()
Here is the entire function code:
function collect_posts() {
global $db, $currencies, $messageStack;
// if we have no GV amount selected, set it to 0
if (!$_POST['cot_gv']) $_SESSION['cot_gv'] = '0.00';
// if we have a GV redemption code submitted, process it
if ($_POST['gv_redeem_code']) {
// check for validity
$_POST['gv_redeem_code'] = preg_replace('/[^0-9a-zA-Z]/', '', $_POST['gv_redeem_code']);
$gv_result = $db->Execute("select coupon_id, coupon_type, coupon_amount from " . TABLE_COUPONS . " where coupon_code = '" . zen_db_prepare_input($_POST['gv_redeem_code']) . "'");
if ($gv_result->RecordCount() > 0) {
$redeem_query = $db->Execute("select * from " . TABLE_COUPON_REDEEM_TRACK . " where coupon_id = '" . (int)$gv_result->fields['coupon_id'] . "'");
// if already redeemed, throw error
if ( ($redeem_query->RecordCount() > 0) && ($gv_result->fields['coupon_type'] == 'G') ) {
$messageStack->add_session('checkout_payment', ERROR_NO_INVALID_REDEEM_GV, error);
zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL'));
}
} else {
// if not valid redemption code, throw error
$messageStack->add_session('checkout_payment', ERROR_NO_INVALID_REDEEM_GV, error);
zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL'));
}
// if valid, add redeemed amount to customer's GV balance and mark as redeemed
if ($gv_result->fields['coupon_type'] == 'G') {
$gv_amount = $gv_result->fields['coupon_amount'];
// Things to set
// ip address of claimant
// customer id of claimant
// date
// redemption flag
// now update customer account with gv_amount
$gv_amount_result=$db->Execute("select amount from " . TABLE_COUPON_GV_CUSTOMER . " where customer_id = '" . (int)$_SESSION['customer_id'] . "'");
$customer_gv = false;
$total_gv_amount = $gv_amount;;
if ($gv_amount_result->RecordCount() > 0) {
$total_gv_amount = $gv_amount_result->fields['amount'] + $gv_amount;
$customer_gv = true;
}
$db->Execute("update " . TABLE_COUPONS . " set coupon_active = 'N' where coupon_id = '" . $gv_result->fields['coupon_id'] . "'");
$db->Execute("insert into " . TABLE_COUPON_REDEEM_TRACK . " (coupon_id, customer_id, redeem_date, redeem_ip) values ('" . $gv_result->fields['coupon_id'] . "', '" . (int)$_SESSION['customer_id'] . "', now(),'" . $_SERVER['REMOTE_ADDR'] . "')");
if ($customer_gv) {
// already has gv_amount so update
$db->Execute("update " . TABLE_COUPON_GV_CUSTOMER . " set amount = '" . $total_gv_amount . "' where customer_id = '" . (int)$_SESSION['customer_id'] . "'");
} else {
// no gv_amount so insert
$db->Execute("insert into " . TABLE_COUPON_GV_CUSTOMER . " (customer_id, amount) values ('" . (int)$_SESSION['customer_id'] . "', '" . $total_gv_amount . "')");
}
// zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(ERROR_REDEEMED_AMOUNT. $currencies->format($gv_amount)), 'SSL'));
$messageStack->add_session('redemptions',ERROR_REDEEMED_AMOUNT. $currencies->format($gv_amount), 'success' );
** // if we have no GV amount selected, set it to total GV value
if ((!zen_not_null(ltrim($_SESSION['cot_gv'], ' 0')) || $_SESSION['cot_gv'] == '0')) $_SESSION['cot_gv'] = $total_gv_amount;
** }
}
if ($_POST['submit_redeem_x'] && $gv_result->fields['coupon_type'] == 'G') zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(ERROR_NO_REDEEM_CODE), 'SSL'));
}
I just added the lines in bold. Its very simple; if the user entered a Redeem code, it sets the "cot_gv" session variable to the total value of the GV for the customer.
Below are the test cases:
- New GV; without existing balance: It applied the GV amount on checkout page.
- New GV with existing GV balance: It applied the Total amount, new + existing balance (if the GV total was over; it retained the balance in the account).
- No GV, straight checkout, worked fine.
- No GV, with existing balance. Did not enter amount in the box; did not take it; charged the full amount.
- No GV, with existing balance, entered amount in the box; deducted it
- Existing balance, if we enter amount over the allowed amount, it throws an error.
Not sure if I missed any test cases. If I did, please let me know.
Zen-cart Guru's: If you see an issue with the logic, please chime in.