diff --git a/custom_commerce_price_table/custom_commerce_price_table.info b/custom_commerce_price_table/custom_commerce_price_table.info new file mode 100644 index 0000000..e3cf10e --- /dev/null +++ b/custom_commerce_price_table/custom_commerce_price_table.info @@ -0,0 +1,7 @@ +name = Commerce price table tax support +description = Tax support for price table field +package = Commerce (contrib) +core = 7.x + +dependencies[] = commerce_price_table +dependencies[] = commerce_tax \ No newline at end of file diff --git a/custom_commerce_price_table/custom_commerce_price_table.module b/custom_commerce_price_table/custom_commerce_price_table.module new file mode 100644 index 0000000..a20545c --- /dev/null +++ b/custom_commerce_price_table/custom_commerce_price_table.module @@ -0,0 +1,74 @@ +type) as $field_name => $instance) { + // Load the instance's field data. + $field = field_info_field($instance['field_name']); + + // If the instance is of a price field... + if ($field['type'] == 'commerce_price_table' && !empty($product->{$field_name})) { + // Check to see if the product has specified an included tax. + foreach ($product->{$field_name} as $langcode => $items) { + foreach ($items as $delta => $item) { + // If it specifies a tax and we can load it... + if (!empty($item['data']['include_tax']) && $tax_rate = commerce_tax_rate_load($item['data']['include_tax'])) { + // Clean the price's components array, as we must start with a + // blank slate to rebuild the components for inclusive taxes. + $item['data']['components'] = array(); + + // Reverse apply the tax. + $tax_amount = $item['amount'] - ($item['amount'] / (1 + $tax_rate['rate'])); + $tax_amount = commerce_tax_rate_round_amount($tax_rate, $tax_amount); + + // Add a base price to the data array. + $component = array( + 'amount' => $item['amount'] - $tax_amount, + 'currency_code' => $item['currency_code'], + 'data' => array(), + ); + + $item['data'] = commerce_price_component_add($item, 'base_price', $component, TRUE); + + // Add the tax to the data array. + $component['amount'] = $tax_amount; + $component['data']['tax_rate'] = $tax_rate; + + $item['data'] = commerce_price_component_add($item, $tax_rate['price_component'], $component, TRUE); + + // Set the new item on the product entity. + $product->{$field_name}[$langcode][$delta] = $item; + } + } + } + } + } + } + } +} + +/** + * Implements hook_field_widget_form_alter(). + * + * Alter price widgets on the product form to have tax inclusive price entry. + * This hook was added in Drupal 7.8, so entering prices including VAT will + * require at least that version. + */ +function custom_commerce_price_table_field_widget_form_alter(&$element, &$form_state, $context) { + // Act on widgets for fields of type commerce_price on commerce_products. + if ($context['field']['type'] == 'commerce_price_table' && $context['instance']['entity_type'] == 'commerce_product') { + $context['field']['type'] = 'commerce_price'; + commerce_tax_field_widget_form_alter($element, $form_state, $context); + } +} \ No newline at end of file -- 1.7.8.msysgit.0