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?

Comments

matthensley created an issue. See original summary.

jkellendonk’s picture

I 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.

solanas’s picture

Same 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.

neograph734’s picture

Title: Role doesn't work if cart isn't set » Role condition doesn't work if cart isn't set
Version: 7.x-1.0-rc3 » 7.x-1.x-dev
Status: Active » Reviewed & tested by the community

I can confirm the patch solves the problem for me as well.

thekenshow’s picture

Same issue, was resolved by this patch.

braindrift’s picture

Patch works for me too

joelpittet’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs steps to reproduce

We 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?

braindrift’s picture

If there is no order there is no owner of the order

braindrift’s picture

Status: Needs work » Needs review
neograph734’s picture

@joelpittet, as explained in the issue:

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.

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.

braindrift’s picture

Important to know is that the discount with the role condition should be a 'Product discount' not 'Order discount'

joelpittet’s picture

Ok, 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.

neograph734’s picture

Ok, so you want to give a discount even though there is no order or products added to the cart?

Yes 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.

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.

Good point, I'll see if I can simulate that and find any anomalies.

neograph734’s picture

I 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.

braindrift’s picture

Status: Needs review » Reviewed & tested by the community
joelpittet’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new1.43 KB

Thank 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.

neograph734’s picture

Did you apply pricing rules/simulate checkout on the order as one final step?

I 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:

// This won't affect anonymous users because they will have 0 as their owner.

I suppose you meant something like:

// This won't affect orders of anonymous users because they will have 0 as their owner.

joelpittet’s picture

Thanks @Neograph734, I'll change before commit.

admin/commerce/config/order there is a checkbox called "Enable the local action link on order edit forms to simulate checkout completion". That would let you try that functionality.

neograph734’s picture

@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?

  • joelpittet committed 7ec9f62 on 7.x-1.x
    Issue #2663058 by joelpittet, jkellendonk, Neograph734: Role condition...
joelpittet’s picture

Status: Needs review » Fixed

Yes exactly, I've committed this to the -dev branch. Thanks for your help.

neograph734’s picture

No problem. Thanks for getting it in! :)

Status: Fixed » Closed (fixed)

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