0

What is the best way to calculate the price from selected line item field (List field or maybe taxonomy) in final price ?

Example: we sell T-shirt. We define a color (list) as line item field, (red, blue, black) Then we want drupal to calculate the price with multiplying to the value specified for selected color. Red = 4 blue = 5 black = 6 The concept is that we don't want to make different product for each option, instead we want to use line item field (Select list) to calculate the price. Maybe for T-shirt doesn't make sense but it fits in our need.

How we can accomplish this and what is the best way of doing it ? Rules or own module ? How we can specify a value for each color option?

I am more interested in coding approach, because there are a lot of different options and we don't want to make many rules for those.

  • Why did you add the field to the line item type and not to the product ( as a product attribute ) – David Thomas Jan 27 '13 at 21:39
  • The main scenario is more complex than this, we have many attributes and options which doesn't make sense to make a product for each different option match. it makes more sense to just calculate it with line_item_field(). I already solved the problem, will send the solution after 3 hours. I will be happy to hear your opinions about my solution. Thanks – Narek Terhovanesian Jan 27 '13 at 23:33
0

The only reason I used following solution, was because we didn't need to make a product type for variety of options we had, and we had a lot of options which didn't make sense to make it as individual product.

I used rules, if any one find better solution let me know, hope it will help someone.

1) defined an action function to multiply the price to a coefficient

2) checked to see what option has been selected and proper coefficient has been applied

/**
 * Rules action: multiply the unit price by some amount.
 */
function modulename_unit_price_multiply($line_item, $amount, $component_name, $round_mode) {
  if (is_numeric($amount)) {       
    // Coefficient variable
    $cf = '1';
    if(isset($line_item->field_color[LANGUAGE_NONE][0]['value'])){
        $selected_color = $line_item->field_color[LANGUAGE_NONE][0]['value'];
        switch ($selected_color){
            case 'red': 
                $cf *= 2;
                break;
            case 'black':
                $cf *= 3;
                break;
           case 'blue':
                $cf *= 4;
                break;
            }          
    } else {
        $cf *= '1';
    }  
    $wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
    $unit_price = commerce_price_wrapper_value($wrapper, 'commerce_unit_price', TRUE);

    // Calculate the updated amount and create a price array representing the
    // difference between it and the current amount.
    $current_amount = $unit_price['amount'];
    $updated_amount = commerce_round($round_mode, $current_amount * $amount * $cf);

    $difference = array(
      'amount' => $updated_amount - $current_amount,
      'currency_code' => $unit_price['currency_code'],
      'data' => array(),
    );

    // Set the amount of the unit price and add the difference as a component.
    $wrapper->commerce_unit_price->amount = $updated_amount;

    $wrapper->commerce_unit_price->data = commerce_price_component_add(
      $wrapper->commerce_unit_price->value(),
      $component_name,
      $difference,
      TRUE
    );
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.