Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I'm hoping to do some complex calculations of option pricing by adding or subtracting a percent of the base price based on a selected option.
Comment | File | Size | Author |
---|---|---|---|
#13 | CPA-multiply.png | 243.17 KB | bisonbleu |
#4 | commerce_pricing_attributes_modified_fixed.patch | 4.38 KB | poulou |
#3 | commerce_pricing_attributes_modified.patch | 4.53 KB | poulou |
#3 | ss_pa_modified.png | 47.72 KB | poulou |
#2 | support_percent_calculations-2090615-2.patch | 6.91 KB | thirdender |
Comments
Comment #1
thirdender CreditAttribution: thirdender commentedThis is my first attempt… It seems to work, but I'm looking for some feedback. It adds two new operands, bringing the list of operations to "+", "-", "+%", and "-%".
Right now the new operands added only appear on the variation edit form, not on the option set edit form. I'm looking into what might be causing that.
Any ideas, feedback, etc welcome :-)
Comment #2
thirdender CreditAttribution: thirdender commentedFixed the problem with the operands not appearing on the option set edit form (feel like an idiot for not seeing this before).
Comment #3
poulou CreditAttribution: poulou commentedI have approached it a little different, but the main idea is the same..
I've added only one operator "*"
Comment #4
poulou CreditAttribution: poulou commentedsecond try for the patch.. :)
Comment #5
primerg CreditAttribution: primerg commentedI tried the patch and so far it is working.
the * makes the calculation simpler but the way it works now is a bit confusing. I suggest the following:
0 = 0%
.4 = +40%
-.4 = -40%
1 = +100%
-1 = -100%
thoughts?
Comment #6
thirdender CreditAttribution: thirdender commentedThat makes it confusing for the coders then :-p The math is such that
price * 1
is justprice
, so to get 140% you have to doprice * 1.4
. What about changing the operator to just%
and letting the user enter whole numbers? Entering 40 would translate to a multiplication by 1.4. Entering -40 would translate into a multiplication by .6. It'd be a simple matter to convert a whole percentage to a decimal for multiplication in the PHP.I think it would also make sense to prevent numbers below -100. I'm not sure how Drupal Commerce responds to items with a negative price…
Comment #7
d.pagkratis CreditAttribution: d.pagkratis commentedI agree with thirdender . Good job thirdender. Nice solution.
Comment #8
eidoscomHello, I was trying to do something very similar and what I did is name the new operand as "percent" that is a minor change and then calculate the values like this:
There are some places where the calculations are not made
if $price<=0
writted as( if ($price > 0) )
. So what I did is change the expression( if ($price > 0) )
by( if ($price !== 0) )
.All is working.
I programmed the "Show attribute price" like this:
With this solution, you can wrote 40% and -40% and is calculated propertly.
Hope this can help ;)
Comment #9
bisonbleu CreditAttribution: bisonbleu commentedMy use case calls for a series of options:
- 1 unit = price
- 7 units = 3.5 x price
- 30 units = 10 x price
- 60 units = 12 x price
Patch in #4 makes it simple to setup these options. Thanks @poulou.
Note this patch (it's 2 years old) doesn't apply cleanly to the current 7.x-1.x-dev: "Hunk #2 FAILED at 1062.". But it is relatively easy to manually apply changes to commerce_pricing_attributes.module.
Also applied this patch that makes it possible to display the selected option e.g. 7 units in the checkout.
Comment #10
Rocinant CreditAttribution: Rocinant commentedHello,
I am using the multiplication patch by Poulou for a webshop for digital prints. When we apply one multiplication attribute to a product everything works perfectly. But if different attributes are multiplied by each other (e.g. product x color x size), it seems that the patch doesn't calculate as expected. It does :
(product price x attribute 1) + (product price x attibute 2) - (product price)
where one would expect
product price x attribute 1 x attribute 2.
Does anyone have a suggestion how to correct this?
The formula is in line 102 at commerce_pricing_attributes.rules.inc:
$commerce_attribute_adjustment -= $line_item_wrapper->commerce_unit_price->amount->value() - ($line_item_wrapper->commerce_unit_price->amount->value() * $price / 100);
(An example can be viewed at http://weloverecycled.nl.greenhostpreview.nl/ding)
Thanks in advance!
Comment #11
bisonbleu CreditAttribution: bisonbleu commented@Rocinant, can you test with
plus (+)
instead ofmultiply (*)
to see if CPA works properly when multiple attributes are applied to a line item ?Comment #12
Rocinant CreditAttribution: Rocinant commentedHi Bisonblue, Just tested it (on the page I mentioned) and CPA works perfectly with multiple attributes and plus (+) instead of multiply (*)
Comment #13
bisonbleu CreditAttribution: bisonbleu commentedI tested with 2 attributes of type
multiply (*)
and it works for me.Comment #14
bisonbleu CreditAttribution: bisonbleu commentedBut I'm seeing another problem: although I only have one item in the cart, the same item is displayed twice - once for each CP attribute? Clicking on Remove for any instance removes both. Order total is correct.
So, it appears that CPA is broken when multiple attributes are used. And it makes no difference whether you're using
Plus (+)
orMultiply (*)
.Comment #15
Rocinant CreditAttribution: Rocinant commentedHi Bisonblue, It seems you have run into the same math bug as I did: with the mugs you would expect a price of
8*1.1*2=17,6 CAD
instead of the 16,80 which CPA calculates now. 16,80 is just as I had8*1.1 + 8*2 - 8
(or8*2.1
)Concerning #14: I have tested the cart in our site and do not have duplicate items.
Comment #16
bisonbleu CreditAttribution: bisonbleu commented@Rocinant, the math is perfectly good, no bug here. The
per-item
adjustments are always calculated on the base price.So if the math doesn't work out the way you want, try changing the values you use for price calculation or the CAP mode to
per-order
.I'll will try to do more test in a few days.
Comment #17
Rocinant CreditAttribution: Rocinant commented@bisonblue, you are right, it is a somewhat different approach than I was used to with Ubercart+Custom Price Calculation, but this probably works out fine. Thanks for your help.
Comment #18
neraprojects CreditAttribution: neraprojects commentedHello,
This addition is very useful for many, but i really think we need a new module.
I think many develop this in private and don't share but it's ok in the end.
I'l try to make this in a new module that support commerce 2.0, because i get a lot of ajax errors, is not stable and when i add to Cart Form the order is not kept, and the default is not kept either.
Thank you.