I talked with bojanz from Commerce Guys about my idea on buying userpoints with Commerce and he suggested that this should be possible by just using Rules.
I tried it, and it does work. However, setting up the rule is not exactly trivial (it requires a rule component and various conditions to verify the necessary fields exist), so I exported my configuration as a feature.
The feature contains currently:
- A Points product product type
- That has a field called field_userpoints, which is a simple integer field
- Rules configuration that grants the amount of points entered into that field of a product when the order is paid.
It works, but I am still thinking that it might make sense to provide a custom userpoints field, that would allow to enter the amount of points + the category. It would however make the rules integration harder I guess. Or at least we'd have to provide a custom rules action to get the points from the product.
| Comment | File | Size | Author |
|---|---|---|---|
| buy_points.patch | 10.54 KB | berdir | |
| #10 | commerce_userpoints.buy_feature_refresh_1270290_10.patch | 10.57 KB | rfay |
| #31 | BuyUserPoints4.jpg | 124.88 KB | John Ching |
| #31 | BuyUserPoints3.jpg | 114.62 KB | John Ching |
| #31 | BuyUserPoints2.jpg | 124.32 KB | John Ching |
Comments
Comment #1
sonictruth commentedThis sounds great and I'd love to see such a thing get off the ground. I'm having a few issues though. I installed the patch but I'm getting this error:
I'm pretty sure it's because there is no commerce_product_type available to make a feature with. Is there some other module that enables Features integration for Commerce? or is this something that Commerce should do out of the box?
thanks
Comment #2
berdirYou are probably missing http://drupal.org/project/commerce_features.
The feature should depend on that, I'll fix that in the next version.
Comment #3
sonictruth commentedThat would be it ;) thanks
Comment #4
berdirNote that this is just a proof of concept that was quickly clicked together.
Any feedback would be welcome.
Comment #5
kclarkson commented@Berdir,
I just tested this patch and it works great !!! Thanks for doing this.
The only confusing part was during the Points Product creation that "druplicons" were the amount "userpoints", they are purchasing on this points product.
After I took a second look I actually like the term druplicons :)
Comment #6
liupascal commentedHey sorry i haven't included this patch in the release yet. I'm trying to keep it as simple as possible right now for code review and full project application. Will be applied right after validation.
Comment #7
rfay@liupascal Don't forget that with git you can put things in other branches; it will not affect your application at all. Just make clear in your application what branch is to be reviewed.
Comment #8
berdirNo need to hurry.
The druplicoins confusion is exactly one of the things that I want to fix before this is commited.
The reason for that is that I have renamed Points to Druplicoins to test the points branding feature of Userpoints on my local test setup.
I want this to support that feature too and also be translatable before commiting it. I need to investigate if this is possible with Features or if we want to expose a custom userpoints field for example (which wouldn't necessarly need to live here, could also be a part of userpoints.module or another project).
Comment #9
pcambraI can confirm what Berdir comments in #2, commerce_features should be included as a dependency, this now happens automatically as #1286512: Add commerce_features as a dependency
Comment #10
rfayHere's a refresh of the OP patch. It adds commerce_features as a dependency, changes the package to Commerce (contrib) and removes the version number, all irrelevant things.
This works for me.
Comment #11
Coyote commentedForgive me if I'm missing something.
I've just installed the patch by rfay.
I created a points product, assigned it a price of zero, points (druplicon) value of one added it to my cart, and checked out.
Checkout appeared to complete properly, but no points were awarded to the account.
There appears to be an active checkout rule for adding points from a product, but nothing happened.
Have I left out a step somewhere?
Comment #12
Coyote commentedNever mind. Silly me - it's looking for the Druplicons term in the taxonomy, which I forgot to add in, despite the prior discussion which suggested that would be necessary!
Comment #13
gregglesSeems like this could be in README.txt and maybe a hook_requirements? I won't mark needs work for that alone.
Comment #14
berdir@greggles: Sure, that would be one thing. However, this module shouldn't hardcode the points names or that it uses a specific category - Instead, the category should be configurable and it should build upon the built-in branding features of Userpoints.
Doing that would however require to create a special field type that can automatically uses the correct, branded names in the UI and allow to configure the category for which the points are.
Comment #15
gregglesWhen choosing between "something that can be committed and provide value right now, but require extra configuration" and "something which requires extra dev work which may or may not happen" I strongly suggest maintainers go for the commit and push the remaining bits to a follow-up patch.
Comment #16
unimarkt commentedIsn't it easy to fix with Rules? (Perhap my solution is too easy.. ) But:
Just added a product with SKU "PROD-3" for 30USD and
added the rule in the picture..
Comment #17
berdirThe patch is a feature that does built on top of Rules as well but additionally to your rule does a) provide a field (on a special product type) which allows to enter the amount of points that a product is worth which prevents you from having to set up multiple rules and b) also works when you buy the product multiple times in a single order.
Comment #18
mxa055 commentedI noticed that the component "Add points from product" does not account for line item quantity. So if I make a product with say 10 points and add 3 of them in my cart, after completing the order I only get 10 points instead of 30. I tried modifying the component by calculating the actual value of user points each line item should provide by multiplying line-item:quantity with line-item:commerce-product:field-userpoints, but unfortunately this value is not accessible inside the "Grant points to a user" action.
Any ideas for a fix?
Comment #19
webdrips commented@mxa055, ever find a solution?
Comment #20
berdirShould be as simple as adding an additional action to the tasks that multiples the price with the line item quantity and then uses that for the userpoints action.
Comment #21
Rc1585 commentedBerdir can you please expand on this.
I created an action that multiplies the commerce order quantity by "1" so I get the total quantity for the product.
The problem I have is when I create an action "grant userpoints to a user" i am forced to enter a number into the field "how many points would you like to add or remove". How do you suggest changing the number of points being added/removed to not reflect a manually input number but a number that is based on the product quantity?
Thanks!
Comment #22
berdirSwitch to data selection (button below) and select your variable?
Comment #23
Rc1585 commentedThanks, I've tried switching to data selector but the variable isn't showing up. Just to confirm, what should the variable be?
Comment #24
berdirWhatever you said it to be before :) Provide an export of your rule and I'll have a look.
Comment #25
Rc1585 commentedIt may be that I installed the patch wrong.
What is the easiest way to install the patch you've created?
Comment #26
Rc1585 commentedWhat file within the commerce userpoints folder should I patch?
Comment #27
somimi commentedSlightly off-topic, but I've been trying for days to use Rules to grant points to users when they purchase points - except I'm using Ubercart. Any help with this would be mightily appreciated - I can't seem to figure out how to access the CCK integer field I've created in the Product node for points amount to purchase, and I'm truly out of ideas.
Comment #28
dgastudio commentedSimple and working rule
replace 'coin' with your 'money' product type:
Comment #29
zdean commented#28 works for me. Thanks, @kervi!
Comment #30
dready2011 commentedAs a reply to #18, the rule not taking qauntity field into account:
After struggling for a while, I think the reason that you do not get access to the line-item:quantity field, is that it is not of type integer.
I was able to multiply the number of druplicons with the lineitem:quantity field in the following way.
step 1:
Under actions, add an action of type 'convert datatype' and convert [line-item:quantity] to int. Give some name for the variable.
Move this action to the top.
Step 2:
Add another action of type 'Calculate a value'. As Input Value 1 choose the variable declared in step 1. Choose * as operator and line-item:commerce-product:field-userpoints as Input Value 2. Choose a name for the variable.
Move this one below the convert datatype action.
Step 3:
In action 'grant points to a user', use the variable declared in step 2 for the Points data selector.
Who knows it might be useful for someone, here my exported rule
Comment #31
John Ching commentedI have just been though painstaking 3-4 days (pain.. lots of pain) trying let users buy points on my site, that takes the Quantity of the items in the shopping cart into calculation. The advises above all didn't work for me, maybe because they are all so outdated already. I have finally done it and I am going to share the steps in detail here, so that others won't have to go through the same amount of paiiinn that I went through.
Step 1: Event. Create an Event "Completing the checkout process"
Step 2: Conditions. Create "Order contains a particular product". Data selector = commerce order. Product SKU = BuyCredits (whatever you have named your product-type SKU). Operator is >=. Quantity 1
Step 3a: Actions. Convert Data Type. Target Type = Integer. Value To Convert = commerce-order:commerce-order-total:amount. Conversion Variable name = conversion_result.
Step 3b: Actions. Calculate a Value. Data Selector = conversion result (result of step 3a). Operator = (*). Input Value 2 Value = 0.01 (eg. if 1 Point = USD1). Variable Name = result.
Step 3c: Actions. Grant Points to a user. User Data Selector = site:current-user. Points Data Selector = result (result of step3b). Points Category = General. Operation Value = Buy Points (or any other name to describe this item). Moderate = Use the site default.
FYI I am using the latest modules as of this date Drupal 7.39, commerce-7.x-1.x-dev, ctools-7.x-1.x-dev, views-7.x-3.x-dev, Userpoints 7.x-1.1+0-dev, Userpoints rules integration 7.x-1.1+0-dev, Commerce Userpoints 7.x-1.x-dev.
Comment #32
socialnicheguru commentedEdit:
what is the patch in #10 for?
Comment #33
paean99 commentedIt makes some corrections to @Berdir original patch (It was automatically hidden because of new files added on others comments, i think).
@Berdir patch:
So after applying the patch you will have to enable the new feature to test it out.
Here are the dependencies (of @Berdir patch, see the changes on #10 by @rfay for more):
Comment #34
socialnicheguru commentedOk. so this new patch is similar to this sandbox module: https://www.drupal.org/sandbox/thomjjames/1515550
Comment #35
paean99 commentedThanks for the link, i didn't know about it.
They have some similarities in what they try to accomplish, but they do it in different ways.
The sandboxes is a module that:
So one just has to create a new rules to see the new action.
I don't think it depends on 'Commerce userpoints', but just on the normal commerce and userpoints modules.
While the patch is just a feature (use the Feature module) that shows how one can do a similar thing with the normal userpoints rules without new code.
That is, if my memory is not failing. I used it some time ago. ;)
Comment #36
dalegrebey commentedSell User Points using Drupal 7 Commerce
#28 Worked for me. To clarify, I:
What that does, in effect, is allows a user to Add a User Points product to the shopping cart, then set the quantity of how many User Points they want to buy, calculates the total (I did 1 User Point = 1 penny) and allows them to checkout. On successful checkout, it grants the User the quantity of User Points they selected and charges their card accordingly.
The default commerce max quantity only allows you to have 4 characters, i.e., quantity of 9,999 max. I also overrode this to allow users to buy, for example, 20,000 User Points. That code is listed below.
#28 RULE
ALTER