Dynamic pricing of products/line items with a "pricing entity" 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-&gt;type-&gt;value() == "price_list") {

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

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

            foreach ($node_wrapper-&gt;field_collection_licenses as $key =&gt; $license) {
                if ($line_item_wrapper-&gt;commerce_product-&gt;product_id-&gt;value() === $license-&gt;field_license-&gt;product_id-&gt;value()) {
                                            $line_item_wrapper-&gt;commerce_unit_price-&gt;amount-&gt;set($license-&gt;field_license_price-&gt;value());
                }

                $line_item_wrapper-&gt;save();
            }

            dsm($line_item_wrapper-&gt;commerce_unit_price-&gt;amount-&gt;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.
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-&gt;data['context']['add_to_cart_combine'] = FALSE;