I'm trying to achieve a pricing functionality in which product/line item's price is configured in an external entity. So that I can have one product which can be priced by multiple entities depending on which is being viewed.

This external entity is a "host" entity or a product display which has two fields: product reference and a price field (float).

Now, when customer visits this host entity page and adds a line item to cart it should change the price according to host entity price field.

At first I tried this with Rules but it was just so inconsistent that I decided to look other options. So here's a piece code that I think could solve this:

    // Alter the price in list and single product page.
function commerce_dynamic_pricing_commerce_product_calculate_sell_price_line_item_alter($line_item){

    if ($node_wrapper = entity_metadata_wrapper('node',_commerce_dynamic_pricing_menu_get_any_object()) ) {

        if ($node_wrapper->type->value() == "price_list") {

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

            dsm($line_item_wrapper->commerce_unit_price->amount->value(). 'Old price');

            foreach ($node_wrapper->field_collection_licenses as $key => $license) {
                if ($line_item_wrapper->commerce_product->product_id->value() === $license->field_license->product_id->value()) {


            dsm($line_item_wrapper->commerce_unit_price->amount->value(),'New price');

The problem with this code is that the price isn't changed/updated. I'm not sure that how the price should be modified.

1 Answer 1


So this first alter hook handles price on product display.

But price needs to be handled again with another hook: commerce_dynamic_pricing_commerce_cart_line_item_refresh. This handles prices regarding current line items in cart.

I also needed to avoid combining same type of line items into "quantity groups".This works well in:

    $line_item->data['context']['add_to_cart_combine'] = FALSE;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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