diff --git a/commerce_price_table/commerce_price_table.module b/commerce_price_table/commerce_price_table.module index d4ab244..c482804 100644 --- a/commerce_price_table/commerce_price_table.module +++ b/commerce_price_table/commerce_price_table.module @@ -62,50 +62,16 @@ function commerce_price_table_field_formatter_info() { * Implements hook_field_widget_form(). */ function commerce_price_table_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { - // Use the default currency if the setting is not present. - if (empty($instance['widget']['settings']['currency_code']) || $instance['widget']['settings']['currency_code'] == 'default') { - $default_currency_code = NULL; - } - else { - $default_currency_code = $instance['widget']['settings']['currency_code']; - } - - // If a price has already been set for this instance prepare default values. - if (isset($items[$delta]['amount'])) { - $currency = commerce_currency_load($items[$delta]['currency_code']); - - // Round the default value. - $default_amount = commerce_currency_amount_to_decimal($items[$delta]['amount'], $currency['code']); - - // Run it through number_format() to add the decimal places in if necessary. - if (strpos($default_amount, '.') === FALSE || strpos($default_amount, '.') > strlen($default_amount) - $currency['decimals']) { - $default_amount = number_format($default_amount, $currency['decimals'], '.', ''); - } - - $default_currency_code = $items[$delta]['currency_code']; - } - else { - $default_amount = NULL; - } - - // Load the default currency for this instance. - $default_currency = commerce_currency_load($default_currency_code); - - $element['#attached']['css'][] = drupal_get_path('module', 'commerce_price_table') . '/theme/commerce_price_table.css'; - if ($instance['widget']['type'] == 'commerce_price_table_multiple') { - $element['amount'] = array( - '#type' => 'textfield', - '#title' => $element['#title'], - '#description' => t('Amount for the price.'), - '#default_value' => $default_amount, - '#size' => 10, - '#field_suffix' => $default_currency['code'], - ); - $element['currency_code'] = array( - '#type' => 'value', - '#default_value' => $default_currency['code'], - ); + // 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'), @@ -122,13 +88,6 @@ function commerce_price_table_field_widget_form(&$form, &$form_state, $field, $i ); } - $element['data'] = array( - '#type' => 'value', - '#default_value' => !empty($items[$delta]['data']) ? $items[$delta]['data'] : array('components' => array()), - ); - - $element['#element_validate'][] = 'commerce_price_table_field_widget_validate'; - return $element; } diff --git a/commerce_price_table/commerce_price_table.rules.inc b/commerce_price_table/commerce_price_table.rules.inc index a02dc68..2dfd408 100644 --- a/commerce_price_table/commerce_price_table.rules.inc +++ b/commerce_price_table/commerce_price_table.rules.inc @@ -42,20 +42,25 @@ function commerce_price_table_set_price($line_item, $quantity, $price_table) { $item = commerce_price_table_get_amount_qty($product, $quantity, $price_table); if (!empty($item)) { // Empty the price components to recalculate them. - $line_item->commerce_unit_price[LANGUAGE_NONE][0]['data']['components'] = array(); - $price = array( - 'amount' => $item['amount'], - 'currency_code' => $item['currency_code'], - 'data' => array(), - ); + if (empty($item['data']['components'])) { + $line_item->commerce_unit_price[LANGUAGE_NONE][0]['data']['components'] = array(); + $price = array( + 'amount' => $item['amount'], + 'currency_code' => $item['currency_code'], + 'data' => $item['data'], + ); - // Alter the base price to the current one. - $line_item_wrapper->commerce_unit_price->data = commerce_price_component_add( - $line_item_wrapper->commerce_unit_price->value(), - 'base_price', - $price, - TRUE - ); + // Alter the base price to the current one. + $line_item_wrapper->commerce_unit_price->data = commerce_price_component_add( + $line_item_wrapper->commerce_unit_price->value(), + 'base_price', + $price, + TRUE + ); + } + else { + $line_item_wrapper->commerce_unit_price->data = $item['data']; + } $line_item_wrapper->commerce_unit_price->amount = $item['amount']; $line_item_wrapper->commerce_unit_price->currency_code = $item['currency_code']; } -- 1.7.8.msysgit.0