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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

thirdender’s picture

This 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 :-)

thirdender’s picture

Status: Active » Needs review
FileSize
6.91 KB

Fixed the problem with the operands not appearing on the option set edit form (feel like an idiot for not seeing this before).

poulou’s picture

I have approached it a little different, but the main idea is the same..
I've added only one operator "*"

* operator

poulou’s picture

second try for the patch.. :)

primerg’s picture

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

thirdender’s picture

That makes it confusing for the coders then :-p The math is such that price * 1 is just price, so to get 140% you have to do price * 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…

d.pagkratis’s picture

Issue summary: View changes

I agree with thirdender . Good job thirdender. Nice solution.

eidoscom’s picture

Hello, 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:

//Get the unit price
$unit_price = $line_item_wrapper->commerce_unit_price->amount->value();
switch ($attribute['set_details'][$field_name]['options'][$selected_option]['price_op']) {
case 'percent':
        $commerce_attribute_adjustment += $price/100 * $unit_price['amount'] * 10;
        break;
case 'minus':
        $commerce_attribute_adjustment -= $price;
        break;
case 'plus':
default:
        $commerce_attribute_adjustment += $price;
        break;
}

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:

if($option['price'] !== 0 and $display){
                  if($option['price_op'] == 'percent'){ //modified
		    $percent = $option['price'];
		    if($option['price'] < 0){
		      $sign = '';
		    }else{
		      $sign = '+';
		    }
		    $options[$option_value] .= ' (' . $sign . $percent/100 . '%)';
		  }else
		    $options[$option_value] .= ' (' . $sign . commerce_currency_format($option['price'], $option['currency_code']) . ')';
                }

With this solution, you can wrote 40% and -40% and is calculated propertly.

Hope this can help ;)

bisonbleu’s picture

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

Rocinant’s picture

Hello,

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!

bisonbleu’s picture

@Rocinant, can you test with plus (+) instead of multiply (*) to see if CPA works properly when multiple attributes are applied to a line item ?

Rocinant’s picture

Hi Bisonblue, Just tested it (on the page I mentioned) and CPA works perfectly with multiple attributes and plus (+) instead of multiply (*)

bisonbleu’s picture

FileSize
243.17 KB

I tested with 2 attributes of type multiply (*) and it works for me.

CPA-multiply.png

bisonbleu’s picture

But 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 (+) or Multiply (*).

Rocinant’s picture

Hi 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 had 8*1.1 + 8*2 - 8 (or 8*2.1)
Concerning #14: I have tested the cart in our site and do not have duplicate items.

bisonbleu’s picture

@Rocinant, the math is perfectly good, no bug here. The per-item adjustments are always calculated on the base price.

Base price	   =  $8.00
Medium => 1.0 * 8$ =  $8.00
Green =>  0.1 * 8$ =  $0.80
	    Total  = $16.80

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.

Rocinant’s picture

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

neraprojects’s picture

Hello,

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.