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()) {
$line_item_wrapper->commerce_unit_price->amount->set($license->field_license_price->value());
}
$line_item_wrapper->save();
}
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.