Can`t find equal issue, so if duplicated - sorry...

in views with exposed filter for making possible filter by price

$handler->display->display_options['filters']['field_price_amount']['table'] = 'field_data_field_price';

data stored at the field_data_field_price if USD - as cents (50$ stored as 5000)
Using exposed filter we have to enter 5000 for finding content with 50$ in the field

Is this a bug? or do I have to make own handler, form alter for rewriting entered price in exposed filter form into commerce way?

Comments

andypost’s picture

Subscribe

rszrama’s picture

I think we'd just need a custom handler that handles the conversion when the exposed filter is submitted. I haven't done any development of exposed filters before, though, so I'm not sure exactly what it requires.

podarok’s picture

Priority: Critical » Normal

rewriting sql query helps me
but this fix works only for particular view with a custom name and custom field name

/**
 * Stub hook documentation
 *
 * This hook should be placed in MODULENAME.views.inc and it will be auto-loaded.
 * MODULENAME.views.inc must be in the directory specified by the 'path' key
 * returned by MODULENAME_views_api(), or the same directory as the .module
 * file, if 'path' is unspecified.
 *
 */
function bm_views_query_alter(&$view, &$query) {
  // example code here
if ($view->name == 'flat_podbor'){
 //watchdog('views alter', print_r($view->name, TRUE));
 //watchdog('views sql alter', print_r($query->where[0]['conditions'], TRUE));
foreach ($query->where[0]['conditions'] as $key => $value){
 if ($value['field'] == 'field_data_field_price.field_price_amount'){
  $query->where[0]['conditions'][$key]['value'][0] = $value['value'][0] * 100;
  $query->where[0]['conditions'][$key]['value'][1] = $value['value'][1] * 100;
}
}
}
}
rszrama’s picture

Title: price value views handler as exposed filter » Add an exposed filter handler for price amounts
Category: support » bug

This also doesn't take into consideration the fact that different currencies will need different multipliers (i.e. some will be * 10, some will be * 1). Your alter will work for currencies like USD or EUR, though. We still need a general exposed filter handler that uses commerce_currency_amount_to_decimal().

rszrama’s picture

Hmm, speaking of which, there isn't an easy way to determine the currency to use when doing the transformation. So I think what we'll have to do is just add a currency code select list to the form; unfortunately this just won't support multi-currency product lists.

podarok’s picture

Yes, I know that
But As I know - most currencies have 100 "points" in 1 "item"
Possibly we have to store that information somewhere for conversion?

I don`t know a universal solution but this snippet can help in particular

Possibly we have to hack new views price handler with an optional option for koef multiplier for every enabled currency

EndEd’s picture

subscribe

cardentey’s picture

subscribe

dazz’s picture

subscribe

msmithcti’s picture

Has any progress been made with this issue? Trying to use the price exposed filter in views to allow users to filter by price. Can't really show users the unformatted price. Is #3 still the best solution at the moment?

podarok’s picture

Yes
#3 work in production for me
And I have no any working code for a better solution yet

msmithcti’s picture

I'm trying to get #3 to work but it's just failing silently. This is what I've done:

  1. Created a module with a .info file, a .module file and a .views.inc file - all in the same directory.
  2. In the .module file I've implemented hook_views_api() like so:
    function exposed_price_format_views_api() {
      return array(
        'api' => 3,
      );
    }
    
  3. I've then pasted the code above (#3) into the .views.inc file and changed 'bm' to 'exposed_price_format' and changed '$view->name == 'flat_podbor'' to '$view->name == 'departments' | 'designers''

I'm not sure what's gone wrong, have I missed a machine name that needs changing?

Thanks in advance.

dpolant’s picture

StatusFileSize
new3.28 KB

Here is a patch for commerce that adds a better price filter handler and attaches it to the existing price filters. Remember to clear the cache after applying the patch.

With this patch, in the filter configuration for price fields, you can now specify a currency to use as a modifier for the amount users send through the input. The currency selector is not currently present on the value form that the user sees when performing the search.

Patch applies to latest 7.x-1.x-dev.

msmithcti’s picture

I've applied the patch in #13 and cleared the cache but I can't see an option to specify the currency and the field on the exposed filter is still only taking the 4000 rather than 40.

You say the patch applies to 7.x.1.x-dev, I'm using 7.x.1.1 - is there something in the dev version that is specifically needed and that's the reason it's not working? I'm assuming that's the case?

dpolant’s picture

It sounds like the views hooks aren't getting loaded. Few questions:

1) did you use git to apply the patch?
2) are you using commerce kickstart?
3) did the patch give any errors when you applied it?

The patch modifies the .module and .info of commerce_product_pricing, and adds includes/views with a .views.inc and a handlers directory for the new handler. It applies ok for me on dev, and most of the new code blocks seem like they would work on 1.1.

BTW make sure you only have one version of commerce installed. (e.g. if you're using commerce kickstart make sure there isn't a commerce codebase in sites/all/modules).

msmithcti’s picture

Thanks for getting back to me. I'm using commerce kickstart with the module files moved to sites/all/modules as suggested on the distributions page. I initially used Netbeans to apply to patch - tends to work OK with simple patches but doesn't seem to work when files are being added etc. I've now used git to apply the patch (msysgit) and I'm getting some errors with the .info file:

$ git apply -v decimalprice-1219762.patch
decimalprice-1219762.patch:58: trailing whitespace.

decimalprice-1219762.patch:66: trailing whitespace.

decimalprice-1219762.patch:70: trailing whitespace.

decimalprice-1219762.patch:72: trailing whitespace.

decimalprice-1219762.patch:75: trailing whitespace.

Checking patch modules/product_pricing/commerce_product_pricing.info...
error: while searching for:
dependencies[] = rules
dependencies[] = rules_admin
core = 7.x

error: patch failed: modules/product_pricing/commerce_product_pricing.info:8
error: modules/product_pricing/commerce_product_pricing.info: patch does not apply
Checking patch modules/product_pricing/commerce_product_pricing.module...
Checking patch modules/product_pricing/includes/views/commerce_product_pricing.views.inc...
Checking patch modules/product_pricing/includes/views/handlers/commerce_product_pricing_price_handler_filter.inc...

It may be me doing something wrong - I've not got a massive amount of experience working in git!

Thanks in advance.

dpolant’s picture

Looks like the patch just doesn't apply to 1.1, it's saying that some of the context can't be found. Probably if you edit the patch manually and remove the changes to the .info it will apply, and then you can simply add the changes back in to your code.

In the meantime, welcoming any testing of #13 on 1.x dev.

mmncs’s picture

Is that to the dev version from 2011-Dec-10?

Thanks.

dpolant’s picture

Patch #13 was rolled on november 29, but it should work on the most current dev. Let me know if it doesn't and i'll remake it.

madurax86’s picture

I could not apply the patch through git, so I applied it manually here's the new patch I got against commit f168aff274a8ae8b7e0ab5cf7439630cca4d398e after I patched it.

diff --git a/modules/product_pricing/commerce_product_pricing.info b/modules/product_pricing/commerce_product_pricing.info
index 9c8654d..0621256 100644
--- a/modules/product_pricing/commerce_product_pricing.info
+++ b/modules/product_pricing/commerce_product_pricing.info
@@ -8,3 +8,4 @@ dependencies[] = entity
 dependencies[] = rules
 dependencies[] = rules_admin
 core = 7.x
+files[] = includes/views/handlers/commerce_product_pricing_price_handler_filter.inc
diff --git a/modules/product_pricing/commerce_product_pricing.module b/modules/product_pricing/commerce_product_pricing.module
index 852d60d..60cf212 100644
--- a/modules/product_pricing/commerce_product_pricing.module
+++ b/modules/product_pricing/commerce_product_pricing.module
@@ -485,3 +485,13 @@ function commerce_product_batch_pre_calculate_sell_prices_finished($success, $re
 
   drupal_set_message($message);
 }
+
+/*
+ * Implements hook_views_api().
+ */
+function commerce_product_pricing_views_api(){
+  return array(
+    'api' => 3,
+    'path' => drupal_get_path('module', 'commerce_product_pricing') . '/includes/views'
+  );
+}
diff --git a/modules/product_pricing/includes/views/commerce_product_pricing.views.inc b/modules/product_pricing/includes/views/commerce_product_pricing.views.inc
new file mode 100644
index 0000000..7532385
--- /dev/null
+++ b/modules/product_pricing/includes/views/commerce_product_pricing.views.inc
@@ -0,0 +1,9 @@
+<?php
+
+/*
+ * Implements hook_views_data_alter().
+ */
+function commerce_product_pricing_views_data_alter(&$data){
+  $data['field_data_commerce_price']['commerce_price_amount']['filter']['handler'] = 'commerce_product_pricing_price_handler_filter';
+  $data['field_data_commerce_unit_price']['commerce_unit_price_amount']['filter']['handler'] = 'commerce_product_pricing_price_handler_filter';
+}
diff --git a/modules/product_pricing/includes/views/handlers/commerce_product_pricing_price_handler_filter.inc b/modules/product_pricing/includes/views/handlers/commerce_product_pricing_price_handler_filter.inc
new file mode 100644
index 0000000..6a52ac9
--- /dev/null
+++ b/modules/product_pricing/includes/views/handlers/commerce_product_pricing_price_handler_filter.inc
@@ -0,0 +1,36 @@
+<?php
+
+/**
+ * Filter handler to convert price filter to use decimal + currency
+ */
+class commerce_product_pricing_price_handler_filter extends views_handler_filter_numeric {
+  function options_form(&$form, &$form_state) {
+    parent::options_form($form, $form_state);
+    
+    $form['currency'] = array(
+      '#type' => 'select',
+      '#title' => t('Currency'),
+      '#description' => t('Pick a currency to use for price comparisons'),
+      '#options' => commerce_currency_code_options_list()
+    );
+  }
+  
+  function option_definition(){
+    $options = parent::option_definition();
+    $currencies = commerce_currency_code_options_list();
+    
+    $options['currency'] = array_combine($currencies, array('default' => ''));
+    
+    return $options;
+  }
+  
+  function query(){
+    
+    // convert user input to amount based on selected currency
+    $amount = commerce_currency_decimal_to_amount($this->value['value'], $this->options['currency']);
+    $this->value['value'] = $amount;
+    
+    parent::query();
+  }
+}
+

Sorry for having it in the post, I can't find the attach button on edit mode.

The issue is still there for getting in between values, can someone take a look?

amateescu’s picture

Status: Active » Needs review
StatusFileSize
new3.38 KB

It seems that a lot of people are asking for a proper solution here, so here's a new patch that adds this filter in commerce_price.module, where it belongs :)

rszrama’s picture

Category: bug » feature
Status: Needs review » Fixed

Cool, thanks amateescu. Tested it and works fine for me, including for price ranges, and yes it needed to be in the Price module. : )

Committed!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

tonylegrone’s picture

Can anyone explain why this method was added in the patch to remove regular expressions?

  function operators() {
    $operators = parent::operators();

    // Remove the regular expression operator.
    unset($operators['regular_expression']);

    return $operators;
  }
rszrama’s picture

Not sure; I'm guessing we just couldn't support it.

Pnoia’s picture

Is the fix implemented in the newest version of commerce? I still seem to have this issue..

*edit*

I upgraded to newest version, but needed to create a new view

Thanks!
Arne