0

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.

0

Well, after re-examining the code of the commerce_email.module, I had an epiphany, and finally get it.

Here is the code before my change (starting line 169) :

$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;')),
      )
    );
  }

And now with my change :

$data = $wrapper->commerce_order_total->data->value();
 if (!empty($data['components'])) {
  foreach ($data['components'] as $key => &$component) {
         if ($data['components'][$key]['name'] == 'flat_rate_expedition_internationale' || $data['components'][$key]['name'] == 'flat_rate_expedition_outremerre' ) {
            $amount_expedition = $data['components'][$key]['price']['amount'];
          }

  }
  foreach ($data['components'] as $key => &$component) {
      if ($data['components'][$key]['name'] == 'base_price') {

          $amount_base_price = $data['components'][$key]['price']['amount'];
            if (isset($amount_expedition)) {
                $amount_base_price += $amount_expedition;
            }
          $rows[] = array(
              'data' => array(
                  ' ',
                  array('data' => t('Prix HT :'), 'style' => array('font-weight: bold; text-align: right; margin-top: 10px')),
                  array('data' => number_format(commerce_currency_amount_to_decimal($amount_base_price, $currency_code), 2), 'style' => array('font-weight: bold; text-align: right;')),
              )
          );
      }

It's not a great solution, and I will try to make a full module, to select dynamically the components to add to the base_price. Until then, this will do the job.

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.