If you use commerce_product_calculate_sell_price() inside hook_preprocess_node in your template.php similar to this:

  /**
   * Implements hook_preprocess_node().
   */
  function MYMODULE_preprocess_node(&$variables) {
    $node = $variables['node'];
    $node_wrapper = entity_metadata_wrapper('node', $node);
    $products = $node_wrapper->field_product->value();
    $price = commerce_product_calculate_sell_price($products[0]);
  }

Then the price array returned will have all price components doubled up because the product you supplied to commerce_product_calculate_sell_price() had its price already calculated. Meaning you get tax applied twice, discounts applied twice etc.

But if you do something like this:

  $product = commerce_product_load(1);
  $price = commerce_product_calculate_sell_price($products);

Then the price will be correct because the product you supplied didn't have the price calculated beforehand.

I was very surprised to discover this. I've always taken it for granted that the commerce_product_calculate_sell_price() returned an absolute price, but looking in the product_pricing module it seems to me you have to render the price field itself such that commerce_product_pricing_commerce_price_field_formatter_prepare_view() is invoked to be sure that the price is actually reset before the new components are applied. This explains why the "original" part of the price array is not always there for me either.

I very rarely render fields as I feel the overhead of cleaning up all the bloat in the output is too great. I prefer fetching things from the database and outputting them myself, using functions like commerce_product_calculate_sell_price() where I have to.

Am I doing something wrong here? Is this by design? Do you have to render price fields to ensure you're not getting the wrong price?

CommentFileSizeAuthor
#4 Screen Shot 2015-07-23 at 2.45.58 PM.png115.65 KBmglaman
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

TwiiK’s picture

Issue summary: View changes

Fixed some typos.

rszrama’s picture

Category: Bug report » Support request
Status: Active » Needs review
Issue tags: +sprint

I really believe this is by design, but it's worth further review.

mglaman’s picture

I'd mark this as design, too. The pricing calculation process assumes this only runs once per cycle so invoking it twice is unexpected. Rendering the field properly should work.

However, before doing so we should try replicating in Panels. I've never tried placing the entity field content type plugin more than once for "product: price". Giving that a run now to see if it has same issues. If not then its merely just choosing how you render the field (not directly invoking calculate sell price.)

mglaman’s picture

Issue summary: View changes
Status: Needs review » Closed (works as designed)
FileSize
115.65 KB

Screenshot of it working just fine in Panels:

The function commerce_product_calculate_sell_price () is expecting an item that hasn't been calculated. If you have a loaded entity available render it as expected, especially in in the node build process which it belongs.