This is related to http://drupal.org/comment/reply/480540 but I chose to open a new thread since it was dealing with a slightly different issue.
I'm planning to throw some code together using hook_recurring_renewal_pending that will modify a recurring payment in conjunction with the uc_userpoints_discount module.
I'm using uc_recurring with a.net CIM, so modifying any fee should be achieved fairly easily.
I have very little experience with ubercart, but this seems like a simple task to me...i've collected some code from the uc_recurring_userpoints module which will act in two parts:
The first section will calculate the amount of the discount, based on the users total points and the maximum percentage of an order that can be discounted using userpoints (i figure this has to be maxed at 99%, b/c A.NET CIM will not accept orders for $0). I've left some code in this function for that, however i haven't looked at it closely, and I'm not posting here to get this section looked at in detail.
The second section will apply the discount to the order in the database. I took this code, again, from uc_userpoints_discount.module--seems strange to me that on the hook_order $op:'save' the developer chose to make changes to the database tables directly, rather than changing the $order object and returning it--but there's probably a good reason for this that I don't know of.
I'm not entirely sure when hook_recurring_renewal_pending is run in the process, so I'm expecting that i may have to make changes to the $order object and return it, rather than making changes directly to the database.
The reason I'm posting here is to get some feedback on whether the approach I've outlined here will work. Thanks in advance for any feedback!
<?php
/**
* Implementation of hook_recurring_renewal_pending
*
* Checks to see if user wants userpoints automatically applied to recurring fees,
* if so, calculate the maximum discount that can be applied to the current recurring fee,
* add this discount as a line item. Upon submission of the order the user's userpoints
* total will be modified to reflect this purchase
*
* @param $order
* The order object.
* @param $fee
* The recurring Fee object.
*/
function custom_module_recurring_renewal_pending(&$order, &$fee) {
/*
* Code taken from uc_userpoints_discount module
* Determine the maximum discount that can be applied to this order
* Untested...simply some lines collected here which I will sort through later
*/
/* check if user wants to spend userpoints on recurring fees */
if (!variable_get('custom_module_uc_recurring_userpoints_user_'.$order->uid,TRUE)) return $order;
/* get users userpoints total */
$curUserPoints = intval(userpoints_get_current_points($order->uid, $tid = NULL));
/* Calculation of how much userpoints are worth vs. currency */
$multiplier = (variable_get(USERPOINTS_UC_DISC, 0));
/* discountable money value of order */
$orderamt = uc_userpoints_discount_discountable_total() + $arg1->quote['rate'];
/* Redefine max discount to be relative to the users amoun tof points */
$maxdisc = ($curUserPoints / intval(variable_get(USERPOINTS_UC_DISC, 1)));
$ptamt = maxdisc;
/* discount value in points */
$points = -(($ptamt) * $multiplier);
$ptdisc = (($ptamt) * $multiplier);
/* max discount on order total */
$ptmaxd = uc_userpoints_discount_max();
$ptmaxdPoints = $ptmaxd * $multiplier;
/*
* Code taken from uc_userpoints_discount module
* Apply discount to order
*/
if ((!empty($ptamt) || $ptamt != '') && $ptamt < $orderamt && $ptdisc <= min($curUserPoints, $ptmaxdPoints)) {
db_query("DELETE FROM {uc_updiscounts} WHERE oid=%d", $arg1->order_id);
db_query("DELETE FROM {uc_order_line_items} WHERE order_id=%d AND type='ptdiscount'", $arg1->order_id);
db_query('INSERT INTO {uc_updiscounts} (uid, oid, ptamt, points) VALUES (%d, %d, \'%f\', %d)', $curUserId, $order->order_id, $ptamt, $points);
uc_order_line_item_add($arg1->order_id, 'ptdiscount', t('Discount order using !points',userpoints_translation()), -$ptamt, 1);
}
}
?>
Comments
Comment #1
univate commentedYes you probably will need to alter the $order object.
Comment #2
vood002 commentedthanks Univate, I'll post the code I use once I get it working for others
Comment #3
univate commentedBeen a while, I assume this has been worked out, so will close this.