When creating a product, the "Include tax in this price" selector next to the Price field defaults to "None". In many cases (particularly in Europe), it would be preferable to have this field default to the most commonly used VAT rate. E.g., if a store is selling clothes in Sweden, this field would always be set to "25% VAT". I've looked for a way to set the default value of this field, but haven't found any such setting. Perhaps I'm just overlooking it somewhere. If not, this would be a time-saving improvement, and would also avoid getting the VAT wrong by simply forgetting to set this field when creating a new product. Many other fields and settings have default values, so it would seem natural this would have such a default setting as well.

-JM

Comments

rszrama’s picture

Status: Active » Closed (won't fix)

Hmm... the problem is that the natural place for such a setting would be on a form that is inaccessible for locked widgets. See, we add that select list by altering the price widget when it's attached to a product. Because that price field is locked (i.e. required and cannot be deleted), its field settings form is inaccessible. This is poor for other reasons (you can change the widget but not update widget settings once you change either), and it also prevents us from putting a setting there. We don't have a space for global settings like this either, and I'm not sure I want to introduce one.

What a contributed module could do is alter the product edit form after commerce_tax_field_attach_form() has had a chance to alter the price widget's form elements. You'd just have to look for the commerce_price element in the array and drill down through its children arrays to see if it includes the include_tax element. I don't think there's a good way to make this a generic feature in core (esp. b/c if you have multiple VATs it would be just as easy to forget to change it to the right one), but on a site-by-site basis, this would be fairly trivial to implement.

Will that work for you?

TheWizz’s picture

Thanks for your response Ryan (and on a Saturday ;-). Yes, some form alteration could do the trick. However, I may want this to work also when using other methods to create products, such as the product feeds importer. Perhaps a lower level hook is needed. I'm already hooking hook_entity_presave for the commerce_product entity type in order to assign other default values (e.g., an auto-incremented barcode when none was specified), so I guess I could quite easily do the default VAT here too.

The hardest part is where to stick the default value to use, as you say. Anyway, thanks for letting me know I'm not just overlooking a setting somewhere. I'll figure it out.

rszrama’s picture

If you look at commerce_tax_price_field_validate() you can see that we're actually adjusting the form components array of the price field when we validate the form after the user hits save. You could include similar logic in your import script, but yeah, we still wouldn't have a default inclusive tax rate. I'm worried in general about Feeds integration, as it's bitten us elsewhere where it doesn't use the actual API for deleting entities (meaning hook_*_delete() hooks weren't being properly called). The probably thing to do is to get that module to use actual APIs if available.

rszrama’s picture

Also, Damien and I were discussing changing the way this works. At present it calculates VAT at the time of save and stores it in the price field's components array. The problem is if the tax rate changes, all those component arrays will be incorrect for the current tax rate value... so our thought is to store in the data array what tax should be included and calculate it on load instead of presave. This may affect what you do here.

TheWizz’s picture

Is this a change that has been decided, or is it still up in the air which way to go?

I can see pros and cons with both methods. Currently, the way price components such as VAT are stored is the same in line items and products AFAIK. This uniformity has some advantages. OTOH, should you change to calculate price including VAT on load, that would apply to products only, but likely not to line items (since those should have the VAT of the day the sale was made, and not be re-calculated on load). I'd say either way works for products. VAT rates changes very seldomly in most countries, I would say. And when it does, many stores would need to go over the prices anyway, and not just have them re-computed (so as to maintain all those x9.95 prices, which is the incl VAT price in most eropean countries). My 2 eurocents, anyway. ;-).

-JM

eme’s picture

Here is the quick fix that can be implemented in any contrib module (change "my_module" into the name of your module) :

/**
 * Implements hook_field_widget_form_alter().
 *
 * Alter price widgets on the product form to have tax inclusive price by default
 */
function my_module_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' && $context['instance']['entity_type'] == 'commerce_product') {
		if (isset($element['include_tax'])) {
			$element['include_tax']['#default_value'] = 'tva';
                        //if(-----) : define your condition here if you do not want the input to appear for certain users.
			//  $element['include_tax']['#access'] = FALSE;
		}
	}
}

Note : you can also have you own condition and make it disappear.

Is it a conf for commerce extra ?

candelas’s picture

@eme thanks for the code, it works perfect :)

yenidem’s picture

this fix was workin before I updated commerce kickstart to version 7x.2.8. this is does not work at this moment. (I already applied same fix after updated commerce kickstart.)

please advice.

MrPaulDriver’s picture

If not a setting, I do think there is a case for making this available as a module - placing an additional option at admin/commerce/config/taxes.

In the case of the UK at least, the majority of VAT registered businesses would prefer this to be the default setting.

One less thing to be bothered with when adding product variations can not be a bad thing.

robotjox’s picture

yep, this doesn't work any more, but it can be done with a simple form_alter:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
    switch ($form_id) {
        case 'commerce_product_ui_product_form':
            $form['commerce_price']['und']['0']['include_tax']['#default_value'] = 'your_tax_setting';
            //hide the tax dropdown if you like
            $form['commerce_price']['und']['0']['include_tax']['#access'] = FALSE;
        break;
  }
}
drifter’s picture

Thanks! For Commerce Kickstart's inline product forms, use this:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
    switch ($form_id) {
        case 'commerce_product_ui_product_form':
            $form['commerce_price']['und']['0']['include_tax']['#default_value'] = 'your_tax_setting';
            //hide the tax dropdown if you like
            $form['commerce_price']['und']['0']['include_tax']['#access'] = FALSE;
        break;
        case 'product_display_node_form':
            $form['field_product']['und']['form']['commerce_price']['und']['0']['include_tax']['#default_value'] = 'your_tax_setting';
            //hide the tax dropdown if you like
            $form['field_product']['und']['form']['commerce_price']['und']['0']['include_tax']['#access'] = FALSE;
        break;
  }
}
MrPaulDriver’s picture

Re: #11
Does something need to inserted in place of 'your_tax_setting' ?

pslcbs’s picture

Issue summary: View changes

#6 Works perfect, thanks!!!

MrPaulDriver’s picture

This is now possible with Commerce Default Tax Rate module

kevster’s picture

Thx @drifter - #11 worked for me!