You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Drupal7 Commerce Product Extra Charge模块实现多选项总价计算

问题修复方案

原因说明

现有代码在计算附加费用时,仅读取了多选选项的第一个选中项ID,未遍历所有选中选项,因此只能累加第一项的费用。需要修改commerce_option_extra_charge_commerce_cart_line_item_refresh钩子的实现逻辑,遍历所有选中的分类术语累加费用。

修改后完整代码

<?php

/**
 * @file
 * Main module file implementing drupal (commerce) hooks.
 */

/**
 * Implements hook_form_commerce_cart_add_to_cart_form_alter
 */
function commerce_option_extra_charge_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state, $form_id) {
  if(!isset($form_state['commerce_option']))
    return;
  foreach(element_children($form_state['commerce_option']) as $option_field_name) {
    foreach(element_children($form[$option_field_name]) as $option_index) {
      foreach(element_children($form[$option_field_name][$option_index]) as $taxonomy_field_name) {
        $lang = $form[$option_field_name][$option_index][$taxonomy_field_name]['#language'];
        if (isset($form[$option_field_name][$option_index][$taxonomy_field_name][$lang]['#options'])) {
          foreach($form[$option_field_name][$option_index][$taxonomy_field_name][$lang]['#options'] as $tid => &$name) {
            $term = taxonomy_term_load($tid);
            if ($term !== FALSE) {
              foreach($term as $term_key => $term_element) {
                if (field_info_field($term_key)['type'] == 'commerce_price' && isset($term_element[key($term_element)][0]['amount'])) {
                  $currency = commerce_currency_load($term_element[key($term_element)][0]['currency_code']);
                  $extra_charge = number_format((float)$term_element[key($term_element)][0]['amount']/100, /*$currency['decimals']*/2, $currency['decimal_separator'], '');
                  $name .= ' (' . ($extra_charge >= 0 ? '+' : '') . $extra_charge . ' ' . $currency['symbol'] . ')';
                }
              }
            }
          }
        }
      }
    }
  }
}

/**
 * Implements hook_commerce_cart_line_item_refresh
 */
function commerce_option_extra_charge_commerce_cart_line_item_refresh($line_item, $order_wrapper) {
  foreach($line_item as $key => $element) {
    if (!commerce_option_set_load_by_name($key))
      continue;
    $chosen_option = unserialize($line_item->$key);
    $field_name = key($chosen_option);
    // 获取所有选中的选项
    $selected_options = $chosen_option[$field_name][key($chosen_option[$field_name])];
    // 遍历所有选中的选项累加费用
    foreach ($selected_options as $option) {
      $term = taxonomy_term_load($option['tid']);
      if (!$term) {
        continue;
      }
      foreach($term as $term_key => $term_element) {
        if (field_info_field($term_key)['type'] == 'commerce_price' && isset($term_element[key($term_element)][0]['amount'])) {
          $extra_charge = (float)$term_element[key($term_element)][0]['amount'];
          $line_item->commerce_unit_price[key($term_element)]['0']['amount'] = (float)$line_item->commerce_unit_price[key($term_element)]['0']['amount'] + $extra_charge;
        }
      }
    }
  }
  $lang = key($line_item->commerce_unit_price);
  if ($line_item->commerce_unit_price[$lang]['0']['amount'] < 0) {
    $line_item->commerce_unit_price[$lang]['0']['amount'] = 0;
    watchdog('commerce_option_extra_charge', t('The price calculation in commerce_option_extra_charge_commerce_cart_line_item_refresh() came out negative. Therefore the final price was set to 0.'));
  }
}

生效操作

修改完成后清空Drupal站点缓存,再测试多选商品加购逻辑,即可实现所有选中项的费用累加效果。

内容的提问来源于stack exchange,提问作者Damn-Deal-Done

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 05:27:04