1

I am experiencing rather odd bug.

In essence I am adding extra unit price component to the line item.

The price component is added perfectly and all seems to work however the unit "base_price" is being then changed to different value.

$line_item = commerce_line_item_load($line_item_id);

$wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);

$wrapper->field_tax_response->set($json);

$price = $line_item_wrapper->commerce_unit_price->value();
$new_components = array();

foreach ($price['data']['components'] as $key => $component) {
    if ($component['name'] == $component_name) {
        unset($price['data']['components'][$key]);
    }
}

$wrapper->commerce_unit_price = $price;

$current_amount = $line_item->commerce_unit_price['und'][0]['data']['components'][0]['price']['amount'];
$taxamount = round($current_amount * $amount);

$taxPrice = array(
    'amount' => $taxamount,
    'currency_code' => $line_item->commerce_unit_price['und'][0]['data']['components'][0]['price']['currency_code'],
    'data' => array(),
);

$line_item->commerce_unit_price['und'][0]['data']['components'][] = array(
    'name' => $component_name,
    'price' => $taxPrice,
    'included' => TRUE,
);

commerce_line_item_save($line_item);

this is roughly my code which adds extra price component that I have previously defined....

$ammount = usually decimal number like 0.07 $component_type = my component type name $json = is text string for future debugging...

I know that this code might seem a bit ugly and might be written in odd way but it does work apart that bit.....

After the custom rule is triggered and all is performed my line item is changed like this....

[commerce_unit_price] => Array
    (
        [und] => Array
            (
                [0] => Array
                    (
                        [amount] => 17900
                        [currency_code] => USD
                        [data] => Array
                            (
                                [components] => Array
                                    (
                                        [0] => Array
                                            (
                                                [name] => base_price
                                                [price] => Array
                                                    (
                                                        [amount] => 53700
                                                        [currency_code] => USD
                                                        [data] => Array
                                                            (
                                                                [components] => Array
                                                                    (
                                                                    )

                                                            )

                                                    )

                                                [included] => 1
                                            )

                                        [1] => Array
                                            (
                                                [name] => ca_tax
                                                [price] => Array
                                                    (
                                                        [amount] => 1253
                                                        [currency_code] => USD
                                                        [data] => Array
                                                            (
                                                            )

                                                    )

                                                [included] => 1
                                            )

                                    )

                            )

                    )

            )

    )

Thank you for any help that you can gives me... Somehow 'base_price' amount is changed from 17900 to 53700.... and 53700 is exactly what line_item_commerce_total['base_price'] should be.

Thank you!!!!

Your Answer

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

Browse other questions tagged or ask your own question.