This may be an edge case, but I've got a discount that is role based, and I'm trying to modify the price listing on the "add to cart" form based on whether or not that discount would be in effect for the user.
This module totally works for role based discount applications once the product is in the cart, but I was getting mixed results when trying to calculate whether or not it would be factored in using commerce_product_calculate_sell_price();
As it turns out, that's because if no "order" exists for the current session, the role check doesn't seem to know what user to use. After our wrapper check in commerce_discount_extra_user_has_role_build(), is there a reason why it wouldn't be sane to default down to the global $user?
| Comment | File | Size | Author |
|---|---|---|---|
| #16 | 2663058-16.patch | 1.43 KB | joelpittet |
| #2 | commerce_discount_extra_use_current_user_for_role_when_unset.diff | 531 bytes | jkellendonk |
Comments
Comment #2
jkellendonk commentedI also had this problem. It was unexpected behavior for me as well, especially considering that the user is not necessarily aware that they have a cart vs an empty cart. On the first visit without a cart, they will not see a discounted price because of this, and it deters them from adding to cart. I modified the module for my own purposes to get the behavior. My changes are below.
Comment #3
solanas commentedSame problem here my customers see the discounts applied by role while they have products in the cart but the checkout is finished (status order: Pending) the discounts are not applied in the products display or add-cart button.
Patch in #2 solve the problem for me.
Comment #4
neograph734I can confirm the patch solves the problem for me as well.
Comment #5
thekenshow commentedSame issue, was resolved by this patch.
Comment #6
braindrift commentedPatch works for me too
Comment #7
joelpittetWe can't use the global $user because this can be triggered by an admin or someone else. It needs to be the owner of the order that the UID comes from.
Can you provide steps to reproduce?
Comment #8
braindrift commentedIf there is no order there is no owner of the order
Comment #9
braindrift commentedComment #10
neograph734@joelpittet, as explained in the issue:
So to reproduce, make sure the logged in user has no commerce orders (at least not in the cart state). In this case the commerce price will be the regular price (no discount applied). Only after adding a product to the cart (creating an order in the process), products will start to show the discounted price.
This patch fixes it, by falling back to the logged in user if there is no cart. That allows for discounts to be applied to products if there is no cart order.
Comment #11
braindrift commentedImportant to know is that the discount with the role condition should be a 'Product discount' not 'Order discount'
Comment #12
joelpittetOk, so you want to give a discount even though there is no order or products added to the cart?
I think we still may need to check that we aren't letting an admin users on an anonymous order affect the discounts too. This would be typically by "Apply pricing rules" or simulate checkout.
Thanks for giving me the steps you are using to get this you two, that really helps.
Comment #13
neograph734Yes please. This would be for example useful in commerce sites where there is are certain levels of paid membership, and the higher paying members get more discount on the products. I would like the discounted price to show for all products (in my product view), even if the user has never added a product to his cart before.
Good point, I'll see if I can simulate that and find any anomalies.
Comment #14
neograph734I have just tried the following:
-----
Apply patch.
Create a product discount rule as described above and have it apply to users with the 'Administrator role'.
View a product to validate the patch works.
Now switch to a different user that does not have have above role (thus no discount).
Create an order by adding some products to the cart.
Switch back to the admin user.
Go to the order overview and edit the previously created order.
Clear the 'User information' vertical tab at the bottom.
Save the order.
Now view the order (still as admin) and notice the prices have not changed and are still the non-discounted prices.
-----
This makes sense, because after clearing the user information, commerce assigned the order to user 0. (Checked in the database.) The patch only works for orders that do not return an owner (
!isset($user))). I believe this is working as it should.Comment #15
braindrift commentedComment #16
joelpittetThank you very much for addressing my concerns @Neograph734. Did you apply pricing rules/simulate checkout on the order as one final step? Here's a new patch with the same logic just cleaned up and with comments.
@braindrift please give some indication that you tested it when changing the status to RTBC.
Comment #17
neograph734I don't think I can simulate checkout on someone else's order, so I suppose I have not. I could try if you can tell me how.
As for the patch, this sentence is a bit weird:
I suppose you meant something like:
Comment #18
joelpittetThanks @Neograph734, I'll change before commit.
admin/commerce/config/orderthere is a checkbox called "Enable the local action link on order edit forms to simulate checkout completion". That would let you try that functionality.Comment #19
neograph734@joelpittet, I tried the following:
If the regular user has a $100 product in his cart.
Admin has 50% discount on all orders.
Admin simulates checkout completion on user's cart.
All price fields on the regular user's order still show $100.
I suppose that is what you wanted to know?
Comment #21
joelpittetYes exactly, I've committed this to the -dev branch. Thanks for your help.
Comment #22
neograph734No problem. Thanks for getting it in! :)