Change the subtotal amount in commerce_email_order_items

First, sorry for my poor English, and the faults you will probably found.

Globally, what I am trying to do , is to add the shipping amount into the ‘base_price’ component price, also labelled subtotal.

So far, it’s working correctly on the site itself with the use of the hook hook_commerce_price_formatted_components_alter(). To give you an idea, here is the code that handle it.

function base_price_shipping_commerce_price_formatted_components_alter(&$components, $price, $entity) {     // We modify $variables to "merge" base_price and shipping if ( isset($components['base_price']) && !empty($components['base_price']) &&     isset($components['flat_rate_expedition_internationale']) && !empty($components['flat_rate_expedition_internationale'])) {     // We calculate the new price with included expedition     $price_with_shipping_amount = $components['base_price']['price']['amount'] +         $components['flat_rate_expedition_internationale']['price']['amount'];     // We format this price with the used currency     $price_with_shipping_formatted = commerce_currency_format($price_with_shipping_amount, $components['base_price']['price']['currency_code']);     // We update the display of base_price component with the new price     $components['base_price']['price']['amount'] = $price_with_shipping_amount;     $components['base_price']['formatted_price'] = $price_with_shipping_formatted;     // We unset the display of shipping     //unset($components['flat_rate_expedition_internationale']); } } 

Now the problem is to do the same thing with the token [commerce-order:commerce-email-order-items]. I know it’s generated with commerce_email_order_items() and by commerce_email_prepare_table() specially. But I don’t get to understand how to make it work, or where to make the change.

Directly and overide the function commerce_email_prepare_table() ? It will probably be around the ligne 174 in the commerce_email.module file :

$data = $wrapper->commerce_order_total->data->value();  if (!empty($data['components'])) {     foreach ($data['components'] as $key => &$component) {       if ($data['components'][$key]['name'] == 'base_price') {     $rows[] = array(       'data' => array(         ' ',         array('data' => t('Subtotal:'), 'style' => array('font-weight: bold; text-align: right;')),         array('data' => number_format(commerce_currency_amount_to_decimal($data['components'][$key]['price']['amount'], $currency_code), 2), 'style' => array('font-weight: bold; text-align: left;')),       )     );   } 

Another possibility is to change the amount of the ‘base_price’ component, not on a formatted level (which just change it before display), but at the “root”, for real. I don’t know if I am clear enough. But after days of research and try to understood the Price API, I still don’t get it, and which hook or function to use : http://www.drupalcontrib.org/api/drupal/contributions%21commerce%21modules%21price%21commerce_price.module/7

Which way do you think is the best ? Thank you for reading. Pierre.

Sponsored by SupremePR
This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Change the subtotal amount in commerce_email_order_items

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.