Hello,

Great job on the module, as always :) However, I thought I remember having the option to "assign" points to a user for purchasing a certain product? Basically I'm trying to integrate this feature with a Subscription model for the site... so, you Purchase a "Subscription" product, which includes a Role ("Subscriber") and you also get, say, 1000 points for subscribing - which can then be used to purchase things, as is the current module.

Right now we don't see an issue with "buying points with points" either, since essentially, we are getting the customer's money either way.

Is this something that I'm just missing, or is it a plan to include in the future? If not, it's something I could definitely use (and keeping it as a Product Feature would be ideal, since then you can avoid using Workflow-ng or some other "outside of the module" method). If you don't have time or resources to add this in, I'd be more than happy to work on a patch. Please let me know.

Comments

torgospizza’s picture

Wouldn't you know it, I had already done this. (That must be why I "remember" this being an option).. it's in the thread on the Ubercart.org site:

http://www.ubercart.org/contrib/1700#comment-9604

I haven't tested this edit with the latest version of Userpoints_ubercart.module, but I will give it a shot. One thing I'll need to double-check is if someone buys, say, 5x the Subscription product, with 1000 points attached to the product, that the multiplier works as it should and award the user 5000 points and not just 1000.

Let me know if you'd still like to include this feature, since it seems that might have slipped by us both. (I know I've been busy...)

Cheers!

bmagistro’s picture

If you happen to have the dev version loaded up, does it have the award points when an item is purchased? If not I will go back in and look at that patch you made.

Thanks for your help with this. In all honesty I don't even have this module in use anymore (the client decided they didn't want to do it around a day or two after I finished the initial version). So I make updates when time permits....

torgospizza’s picture

Looking at the dev version, the only thing I found for awarding points on a purchase was for a referral program. There is also a USERPOINTS_UC_EARN option that just appears to be "earn points for every dollar you spend." These are close, but not quite exactly what I'm hoping to achieve. Ideally, you could set an amount of points to award for that product's purchase, on a per-product basis, hence the idea for doing it as a Product Feature.

Let me know if this makes sense - I don't want to step on any toes with regard to what the module's already capable of. Of course there's the chance I'm missing something altogether, so feel free to correct me if I am just oblivious to it.

And I can re-add my code into the dev version and submit a patch when I have tested the Product Feature, if it doesn't already exist as I'm envisioning.. (and if you want to include it). Let me know.

bmagistro’s picture

The ability to "purchase" points has been requested by several people if I recall correctly. I will see if I can get your patch applied. I am going to try and make it something like points_product.module since I don't think that code was used by anyone other than you. I will try and have something later today or tomorrow hopefully.

torgospizza’s picture

Wow, cool - yeah I haven't gotten to testing my code against the latest rev of this module (I was waiting to hear back from you) ... but I'm more than willing to work on it as well. Looking at my old code, I was going to create a similar .inc file - mine was to be called uc_points_feature.inc - but I also had to add code to the hook_order_complete hook to award the points to the user... feel free to reuse any of the code I linked to above. Again, let me know if I can assist in any way.

Thanks for looking into it :)

bmagistro’s picture

Version: 5.x-1.7 » 5.x-1.x-dev

Hello,

I didn't really dig into your code too much but I did build the following three files off of it with minimal changes (only function names). I have not tested it at all yet but it looks like it will work nicely and breaking out the hook_order so that it is separate from the other portion of the module seems like a better way to do this than what was discussed in the previous thread.

uc_userpoints_product.module
uc_userpoints_product.install
uc_userpoints_product.info

These will be in the dev release whenever the packaging script runs again. Please feel free to edit them as needed. I consider this portion of the module/package to be more yours than mine.

torgospizza’s picture

You, sir, rock. I'm glad I was able to help provide a useful feature :)

Thanks for your hard work and attention! I will grab the update when it's available, and let you know any feedback.

torgospizza’s picture

Title: Userpoints as Product Feature? » Userpoints as Product Feature - new patch included
Status: Active » Needs review
StatusFileSize
new2.56 KB

bmagistro,

I installed the new module and some of it worked, but much of it didn't. Mainly the "Completed" action when a product is purchased that contains points, the points never got awarded.

Another thing I added was the qty multiplier, so for instance if someone is buying 2x a month subscription, it will award them 2x the amount of points contained in the product.

In the future I think this could be configurable, as well as what order status is required to award the points to the user (currently it is "Completed").

There are some other tweaks that I made, please take a look at the patches and feel free to include them. I have now tested this on our development site and it all seems to work splendidly. Feedback welcome and appreciated. PLEASE NOTE the patches I've attached are just for the uc_userpoints_product files.

Thanks again for your work.

torgospizza’s picture

StatusFileSize
new12.99 KB

If the patches don't work (I had some issues on my server) here is the tarball for the complete module.

bmagistro’s picture

it looks good, i have updated the dev version with the changes you made. Thanks for your help with this.

torgospizza’s picture

Here is another addition. This will prevent users from purchasing points using points. After talking to the boss, it appears that this is probably the way to go. I plan on making this into a configuration; but I imagine this is probably what most people will want. Even if the conversion ration is 1:1, it's still kind of silly to allow purchasing of points using points. In cases where you get, say, $15 worth of points for $10, as an example - this could easily get the user a lot more "money" than what they are spending.

Spend | Receive
$10 $15
10 points $15 in more points
------------------------------------
$10 $30

So you can see where the original purchase may have only been $10, but now they have $30 worth of points. I think in most cases that would be a problem.

Anyway here is the addition I added to uc_userpoints_product.module:

/**
 * Check and make sure the user isn't trying to buy points with points.
 *  @todo: Make this configurable?
 */
function uc_userpoints_product_form_alter($form_id, &$form) {
  // We may need to alter the checkout form to remove invalid payment methods.
  if ($form_id == 'uc_cart_checkout_form' && isset($form['panes']['payment'])) {
			
    $order->products = uc_cart_get_contents();

    // Make no changes if no products containing Userpoints are found.
    if (uc_userpoints_find_points_products($order) == array()) {
      return;
    } else {
			unset($form['panes']['payment']['payment_method']['#options']['points']);
			drupal_set_message(t('You cannot purchase !Points with !Points, so that payment option has been removed.', userpoints_translation(), userpoints_translation()));
		}
		
    $count = count($form['panes']['payment']['payment_method']['#options']);
    if ($count == 0) {
      // Display an error message if no payment methods remain.
      drupal_set_message(t('There are no payment methods available to purchase !points with!', userpoints_translation()), 'error');
      drupal_set_message(t('Please contact an administrator to solve the issue.'), 'error');
    }
    elseif ($count == 1) {
      // If only one payment method remains, make it the default.
      $form['panes']['payment']['payment_method']['#default_value'] = array_pop(array_keys($form['panes']['payment']['payment_method']['#options']));
    }
  }
}


/**
 * Returns an array of nids associated with user's cart contents
 *
 * @param $order
 *   The order object in question.
 * @return
 *   An array of matching nids from uc_userpoints_products.
 */
function uc_userpoints_find_points_products($order) {
  if (!is_array($order->products) || count($order->products) == 0) {
    return array();
  }

  foreach ($order->products as $product) {
    $nids[] = check_plain($product->nid);
		//dsm('Nid: '.$product->nid);
  }

  $pointsproducts = array();
  $result = db_query("SELECT * FROM {uc_userpoints_products} WHERE nid IN ('". implode("', '", $nids) ."')");
  while ($pntproduct = db_fetch_object($result)) {
    $pointsproducts[] = $pntproduct;
		//dsm('Matched nid: '.$pntproduct->nid);
  }

  return $pointsproducts;
}

Also there was a typo in the uc_userpoints_product.info file. Under "dependencies" there was a "diff" thrown in there for some reason. Removing it solves the issue.

bmagistro’s picture

Hello,

Thanks for your work on this. I will review and get it committed tonight. I have some additional changes to be made per a feature request asking to add an option to show in points, dollars or both per a configuration setting. I may look at setting some of the other feature requests but probably won't add them to this series of updates just to limit how many changes are made.

bmagistro’s picture

Status: Needs review » Fixed

Hello,

I've added this in to the cvs. Again thanks for your help developing this.

torgospizza’s picture

No problem - there is one more issue I found, in the uc_userpoints_product.install file, the functions are named userpoints_ubercart_install and userpoints_ubercart_uninstall. This throws a "cannot redeclare" fatal error. Changing them to uc_userpoints_product_(un)install will fix the issue. Sorry I forgot to mention it before - I had fixed it and forgotten :)

bmagistro’s picture

Thanks. I just committed...I should look over my code more and copy and paste less....

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.