6

Im working on an integration module where I'm trying to import products externally and into Drupal Commerce. I revceive the product object externally like this: 2692604 (Object) stdClass id (Integer) 2692604 revision (Integer) 2 currencyCode (String, 3 characters ) NOK vatType (Integer) 3 name (String, 14 characters ) Produktkatalog priceCurrency (Integer) 235 priceIncVatCurrency (Integer) 294 number (String, 4 characters ) 1003 cost (Integer) 230

I've found a code snippet from the commerce_example module where you can create a product programmatically like this:

//commerce_price[und][0][amount]
$price = array(LANGUAGE_NONE => array(0 => array(
  'amount' => $price * 100,
  'currency_code' => commerce_default_currency(),
)));
$form_state['values']['commerce_price'] = $price;

// Notify field widgets to save their field data
field_attach_submit('commerce_product', $new_product, $form, $form_state);

commerce_product_save($new_product);
return $new_product->product_id;

I have defined a tax type and a set of tax rates - including a mapping from the code found in the importing object.

I'm thinking the correct way of adding the tax part should be by modifying the code to something like this:

//commerce_price[und][0][amount]
$price = array(LANGUAGE_NONE => array(0 => array(
  'amount' => $price * 100,
  'currency_code' => $product_obj->vatType,
  'data' => array('include_tax' => 'MVA_hoy'),
)));
$form_state['values']['commerce_price'] = $price;

// Notify field widgets to save their field data
field_attach_submit('commerce_product', $new_product, $form, $form_state);

commerce_product_save($new_product);

Problem is that when I import the products, the tax rate is not set as I would expect it to. Investigating the newly created product (or an old loaded product) does not give me much clue either...

5

Found the solution it seems. It is apparently not enough to add a field under the data array, you also need to add the include_tax field on the same level as amount and currency:

//commerce_price[und][0][amount]
$price = array(LANGUAGE_NONE => array(0 => array(
  'amount' => $price * 100,
  'currency_code' => $product_obj->vatType,
  'data' => array('include_tax' => 'MVA_hoy'),
  'include_tax' => 'MVA_hoy',
)));
$form_state['values']['commerce_price'] = $price;

Now, when importing products, the tax field is updated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.