Let me know if you think this would be a good addition to your module: Since individual products/skus can have different sale prices tied to one product display ($3 for small but $5 for large), I'm thinking it might be nice to have a formatter to display the range, so in a View the product display could have a price of "$3-$5".

Comments

akalata’s picture

Status: Active » Needs review
StatusFileSize
new3.88 KB

And here's a patch. It works, but I'm sure it's missing some bits and isn't completely following "best practices".

Heather51’s picture

This would be a great addition to the extra price formatters module for drupal commerce. I have product display views using taxonomy and price ranges make perfect sense where a product has several options each with a different price. I applied the patch to the latest dev. The formatter is available in the product price field but on viewing the page display I get this error:

Notice: Undefined property: stdClass::$field_product in commerce_extra_price_get_range() (line 58 of /home/clients/websites/w_wfstes/public_html/wfstes/sites/all/modules/commerce_extra_price_formatters/includes/extra_functions.inc).
Warning: Invalid argument supplied for foreach() in commerce_extra_price_get_range() (line 60 of /home/clients/websites/w_wfstes/public_html/wfstes/sites/all/modules/commerce_extra_price_formatters/includes/extra_functions.inc).
Notice: Undefined offset: 0 in commerce_extra_price_get_range() (line 106 of /home/clients/websites/w_wfstes/public_html/wfstes/sites/all/modules/commerce_extra_price_formatters/includes/extra_functions.inc).
Notice: Undefined offset: -1 in commerce_extra_price_get_range() (line 107 of /home/clients/websites/w_wfstes/public_html/wfstes/sites/all/modules/commerce_extra_price_formatters/includes/extra_functions.inc).

Any help on this would be much appreciated.

joachim’s picture

+++ b/includes/extra_functions.inc
@@ -47,4 +47,65 @@ function commerce_extra_price_no_decimal_currency_format($amount, $currency_code
+			// calculate final prices based on components
+			$components = array();
+			$weight = 0;
+			foreach ($item['data']['components'] as $key => $component) {
+				$component_type = commerce_price_component_type_load($component['name']);

Isn't there an API function to get the calculated price of a product?

I'm interested in having the price say "From £xxx" is there is a range, so that's something I could either add to this patch as a formatter option, or add as another formatter that makes use of commmerce_extra_price_get_range().

jount’s picture

Hi, akalata, and thanks for a great patch!

One thing to note. In file includes/extra_functions.inc :

function commerce_extra_price_get_range($entity, $instance) {
	$product_ids = $entity->display_context['entity']->field_product[LANGUAGE_NONE];
...
}

I had the same problem with notices and warnings. There is a hardcoded "field_product" property in commerce_extra_price_get_range() function, which is causing that warning messages. Heather51, it seems you just have to change "field_product" in the code to your name variant of the product reference field to get price ranges displaying correctly.

Heather51’s picture

Hi jount - thanks for the tip. And thanks to akalata for creating this much needed patch.

I hadnt noticed that my product reference field was actually called field_product_options. Made the changes as per #4 - No more warnings.

However I am having an issue with the low price in the range. I have a 10% tax on products. The high price range of all products is correct but the low range is showing only the tax amount. ie for a product display where the lowest price in the range should be $4.50 inclusive of tax, the low price is showing as $0.41 which is actually the tax amount included in the full price.

Not sure why the high price would be ok but not the low. Any help here would be fantastic.

Heather51’s picture

Adding to #5 there is definitely an issue here with the tax component. If I add some test products which have no tax component the price range works as expected. If I have tax included in the price of the products the the tax component gets chosen as the lowest price in the range on the product display node.

Anyone know how to resolve this?

ikos’s picture

Thanks everyone for your efforts on this patch.

I am just looking at it now with a view to getting it into the latest dev.

I can also see the problem in my test site reported in #5 so I'll try to resolve that first.

ikos’s picture

StatusFileSize
new4.01 KB

OK I have attached an alternative patch.

This is a modified version of the original patch in #1 but it doesn't try to calculate the components as in theory you don't need to - you only need the calculated price to show the range in this way.

This approach also eliminates the tax issue mentioned in #5.

I have also delayed the formatted of the currency until the end to improve the sorting.

Please let me know if this works for you.

kind regards

Richard

Heather51’s picture

Hi ikos. Thanks for the great work you're doing here.

I've applied the patch in #8 to the latest dev version and am still having the same issue with the low price being the tax element of the lowest price in the range. I played with removing taxes from test products again. When all products in the group had no tax everything worked as expected. As soon as any product in the group had a tax element I ran into the same problem.

Im using display suite with a custom view mode which contains the price range formatter. Im also using taxonomy with taxonomy menu to group the products which are included in the price range.

Would really appreciate it if you could get a chance to have another look at this some time.

infines’s picture

Issue summary: View changes
Status: Needs review » Needs work
sam152’s picture

This patch isn't present in the dev version of the module? Was this never pushed out?

  • I also got a large amount of errors after apply the patch.
  • The code added does not conform with the coding standards.
  • Does the patch assume a specific field name, "field_product"?
  • Does the patch assume a specific language, "LANGUAGE_NONE"?

I think it might need some work before being applied.

sam152’s picture

StatusFileSize
new3.71 KB

Here is my patch which solves a few of the above issues. It doesn't have any hard-coded languages or fields, but it doesn't do any logic on the components of the prices. It did work for my use-case however.

bloomt’s picture

Was anyone successful with this?

I am trying to make a view that displays product nodes that may or may not have multiple product variations or product references. If the product node only has one product reference then I want it to display the one price if the product node has multiple references I want it to show the low to high price range?

Any help would be great.

bmunslow’s picture

Patch #12 did the job nicely for me, although the patch didn't apply correctly for the file extra_functions.inc, so I had to apply it manually.

Other than that, very neat.

discipolo’s picture

Status: Needs work » Needs review
StatusFileSize
new3.95 KB

in that case here is a reroll

VSZ’s picture

Many thanks for the patch #15. Works fine for me.
But I actually need price range with no decimals. How can I combine this two formatters?

VSZ’s picture

I would like to draw your attention to the fact that the price range option does not take into account status of a product.
Let's say we have two product variants referenced by one product display node. One of them is $5 and another one is $10. The price range will show $5-$10. If you disable one variant the price range will still show $5-$10 range.

I suggest to modify function commerce_extra_price_get_range($entity, $instance) in extra_functions.inc to check product status berore adding it's price to the list.

It will look like this:

function commerce_extra_price_get_range($entity, $instance) {
  // Get the product display.
  $product_display = $entity->display_context['entity'];
  // Get all of the fields which reference a product.
  $fields = commerce_info_fields('commerce_product_reference');
  // Check which one is applicable to the product display.
  foreach ($fields as $field_name => $field_instance) {
    $product_ids = field_get_items('node', $product_display, $field_name);
    if ($product_ids) {
      break;
    }
  }

  // Loop through all of the products attached to our product display.
  $prices = array();
  foreach ($product_ids as $delta => $value) {
    // Load the product and the price associated with that product.
    $product = commerce_product_load($value['product_id']);
   //Check the status of the product and take into account only active products
    $status = field_get_items('commerce_product', $product, 'status');
    if ($status) {
    $price = field_get_items('commerce_product', $product, 'commerce_price');
    $prices[$price[0]['amount']] = $price;
    }
  }

  if (count($prices) == 1) {
    return array('single' => array_shift(array_shift($prices)));
  }
  else {
    $price_keys = array_keys($prices);
    return array(
      'min' => $prices[min($price_keys)][0],
      'max' => $prices[max($price_keys)][0],
    );
  }
}
brennino’s picture

Thanks for the beautiful work, can some maintainer apply the patch and create a new release? The last release of this module is dated 2012 and it's a pity that a great work like the one in this page can't be released!

monstrfolk’s picture

Does not seem to work with inline entity form. Thinking it is a problem with inline entity forms.

Get an error saying display_context['entity'] does not exist.

Checked and display_context['entity'] is not created for every product created with inline entity form. Only the first item created contains display_context['entity'].

mellowtothemax’s picture

Not working for me either

following errors

Notice: Undefined property: stdClass::$display_context in commerce_extra_price_get_range() (line 57 of /sites/all/modules/commerce_extra_price_formatters/includes/extra_functions.inc).

and

EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids() (line 7879 of includes/common.inc).

hockey2112’s picture

I am also experiencing the "EntityMalformedException" error. Any fix/alternative solutions?