
Zen-cart中的运费估算出现在购物车页面,它只能根据购物车中的重量估算运费,而无法手动输入重量进行估算。手动输入重量可以快速比较各种运输方式的价格。
这里的这个功能从Zen-cart默认的运费估算移植而来,精简了一些代码。核心代码:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | if(isset($_POST[‘action’]) && ($_POST[‘action’] == ‘submit’)){ if(isset($_POST[‘shipping_weight’])){ $_SESSION[‘shipping_weight’] = (int)$_POST[‘shipping_weight’]; } if (isset($_POST[‘zone_country_id’])){ $_SESSION[‘zone_country_id’] = $_POST[‘zone_country_id’]; } zen_redirect(zen_href_link(‘shipping_estimator’)); exit; } if (isset($_SESSION[‘zone_country_id’])){ $country_info = zen_get_countries($_SESSION[‘zone_country_id’],true); $order->delivery = array(‘country’ => array(‘id’ => $_SESSION[‘zone_country_id’], ‘title’ => $country_info[‘countries_name’], ‘iso_code_2’ => $country_info[‘countries_iso_code_2’], ‘iso_code_3’ => $country_info[‘countries_iso_code_3’]), ‘country_id’ => $_SESSION[‘zone_country_id’]); } else { $country_info = zen_get_countries(STORE_COUNTRY,true); $order->delivery = array(‘country’ => array(‘id’ => STORE_COUNTRY, ‘title’ => $country_info[‘countries_name’], ‘iso_code_2’ => $country_info[‘countries_iso_code_2’], ‘iso_code_3’ => $country_info[‘countries_iso_code_3′]),’country_id’ => STORE_COUNTRY); } $total_weight = 10; if(isset($_SESSION[‘shipping_weight’])){ $total_weight = (int)$_SESSION[‘shipping_weight’]; } /////// require(DIR_WS_CLASSES . ‘shipping.php’); $shipping_modules = new shipping; $quotes = $shipping_modules->quote(); $qs = array(); foreach($quotes as $q){ if($q[‘id’] == ‘freeshipper’){ continue; }else{ $qs[] = $q; } } $quotes = $qs; /////// // set selections for displaying $selected_country = $order->delivery[‘country’][‘id’]; $free_shipping = false; $show_in = ‘shipping_estimator’; |
接下来就是一个表单输出。
注意问题:从代码可以看出,我把freeshipper这种运输方式给过滤掉了。默认,如果购物车中没有产品或产品重量为零,这个运输方式就可能被激活。而我不想去修改freeshipper的逻辑,所以这里把它过滤掉。另外,每个运费模块的类的构造函数中都有判断模块是否启用的代码逻辑:
| 1 2 3 | if (zen_get_shipping_enabled($this->code) ) { $this->enabled = ((MODULE_SHIPPING_HK_POST_AIR_MAIL_STATUS == ‘True’) ? true : false); } |
当前运输方式是否启用,由zen_get_shipping_enabled()函数控制,这个函数根据购物车的内容控制运费模块是否启用,很明显,对于手动输入重量来计算运费的情况,是一大限制,可以修改这个函数的逻辑,不过我更加倾向修改我的自定义运费模块来解决这个问题:
| 1 2 3 4 5 | //…. global $order, $db, $total_weight; if (zen_get_shipping_enabled($this->code) || ($total_weight > 0)) { $this->enabled = ((MODULE_SHIPPING_HK_POST_AIR_MAIL_STATUS == ‘True’) ? true : false); } |
从外部获取$total_weight,在运费模块初始化时,重量确定,然后判断这个重量,只要大于零模块就总是启用。
