I noticed that commerce_price_table_field_widget_form() copies parts of commerce_price_field_widget_form(), but not the part for the currency_code field. This makes commerce_price_table unable to work in a multi-currency environment.

Why not just take the element created by commerce_price_field_widget_form() and add the additional fields for min_qty and max_qty to it? This results in a much smaller widget form function, and as far as I see multi currency support works out of the box:

/**   
 * Implements hook_field_widget_form().
 */ 
function commerce_price_table_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  if ($instance['widget']['type'] == 'commerce_price_table_multiple') {
    // Get a standard widget field from module 'price'.
    $priceInstance = $instance;
    $priceInstance['widget']['type'] = 'commerce_price_full';
    $element = commerce_price_field_widget_form($form, $form_state, $field, $priceInstance, $langcode, $items, $delta, $element);

    // Replace validator and add commerce_price_table styles.
    $element['#element_validate'] = array('commerce_price_table_field_widget_validate');
    $element['#attached']['css'][] = drupal_get_path('module', 'commerce_price_table') . '/theme/commerce_price_table.css';
    
    // Add our own fields.
    $element['min_qty'] = array(
      '#type' => 'textfield',
      '#title' => t('Min Qty'),
      '#description' => t('Minimum quantity for the price range.'),
      '#default_value' => isset($items[$delta]['min_qty']) ? $items[$delta]['min_qty'] : 0,
      '#size' => 10,
    );
    $element['max_qty'] = array(
      '#type' => 'textfield',
      '#title' => t('Max Qty'),
      '#description' => t('Maximum quantity for the price range. Use "-1" for unlimited quantity.'),
      '#default_value' => isset($items[$delta]['max_qty']) ? $items[$delta]['max_qty'] : 0,
      '#size' => 10,
    );
  }

  return $element;
}
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

pcambra’s picture

Status: Needs review » Active

That's interesting, I'm facing the same questions than in #1886080: Add VAT inclusive support for price table field why would you want a currency/tax per item of the table? wouldn't be the same for all of them?

Keeping in active to avoid confusing myself (NR = patch inside)

Cybso’s picture

why would you want a currency/tax per item of the table? wouldn't be the same for all of them?

Yes, of course. But currently the shop administrator is not able to create two or more price table products that uses different currencies.

Example:

"Product A (for EU members only)":

  • 0 - 10: 19,99 €
  • 11 - unl.: 14,99 €

"Product A (for members of other states)":

  • 0 - 10: 14,99 $
  • 11 - unl.: 9,99 $

A product that may require this kind of distinction could be anything that underlies special regulations within some parts of the world, for example wifi devices or FM transmitters.

In my case I'm having special currencies called 'on request' and 'not available' which are treated in a special way. In combination with another individual addition (using the 'alter' hooks from #1913368: Add hooks that allowes to alterate commerce_price_table's schema) this allows me have individual quantity discounts for members of different price lists, without having to change the product's fields when adding new price lists. Sounds a bit dirty, but until now it only requires a custom module and my proposed patches to work. And even the change above could be implemented by hook_form..._alter if necessary ;-)

pcambra’s picture

I completely understand the problem, but imagine you've got 10 or more elements within one widget (unlimited, it could grow larger), changing the currency or tax would be quite exhaustive, could you share a screenshot on how your widget looks with the code above?

Cybso’s picture

FileSize
38.78 KB

Of course. Please note that I've hidden max_qty and added a new 'price list' field to it. Does not have anything to do with the above, but just to clarify: each user can be a member of multiple price lists (taxomony terms) and hook_commerce_price_table_items_alter() from #1913368: Add hooks that allowes to alterate commerce_price_table's schema reduces the item set according to the lists the user is a member of).

Cybso’s picture

Works very good for me, so I decided to put this into a patch.

sinn’s picture

Awesome decision. Good work! Must be in the module.

But it doesn't solve problem with tax because tax doesn't support any other field types except commerce_price.

jsacksick’s picture

Status: Needs review » Needs work
+++ b/commerce_price_table.module
@@ -62,50 +62,17 @@ function commerce_price_table_field_formatter_info() {
+    $priceInstance = $instance;

$priceInstance should be $price_instance, variables are not camelCases, except in classes.

I think the main issue here is that the Commerce price table module doesn't actually expose the currency code setting, a hook_field_widget_settings_form() implementation is actually missing.

I don't think calling the callbacks from the commerce_price module is such a good idea (cause the code could potentially change there and we're artificially altering the widget type which doesn't look like the cleanest approach to me.

jsacksick’s picture

Title: Use commerce_price_field_widget_form for commerce_price_table_field_widget_form » Expose the currency code widget setting
Status: Needs work » Needs review
FileSize
1.09 KB