WooCommerce 运费与支付方式高级定制:按地区/重量/商品数量动态计算运费和自定义支付网关
WooCommerce 提供了灵活的运费和支付方式配置,但默认选项往往无法覆盖所有业务场景。比如,你想根据用户的收货地区、购物车总重量、商品数量甚至天气情况来动态调整运费;或者你想接入一个非主流的本地支付网关,这些都需要通过代码进行深度定制。
运费定制的核心是 WooCommerce 的“运费区”(Shipping Zones)和“运费方式”(Shipping Methods)系统。你可以通过 woocommerce_package_rates 过滤器来修改已有的运费方式,或者在代码中注册一个全新的运费方式。
动态修改现有运费 是最常见的需求。例如,你想在订单金额超过一定数额时免运费,或者根据购物车商品总重量增加额外费用:
add_filter('woocommerce_package_rates', 'dynamic_shipping_cost', 10, 2);
function dynamic_shipping_cost($rates, $package) {
$total_weight = WC()->cart->get_cart_contents_weight();
$total_price = WC()->cart->get_cart_total();
foreach ($rates as $rate_key => $rate) {
// 如果总重量超过 50kg,增加 20 元附加费
if ($total_weight > 50) {
$rates[$rate_key]->cost = $rate->cost + 20;
}
// 订单满 500 元免运费
if ($total_price >= 500) {
$rates[$rate_key]->cost = 0;
}
}
return $rates;
}
注册自定义运费方式 需要创建一个继承 WC_Shipping_Method 的类,并实现 init 和 calculate_shipping 方法。在 calculate_shipping 中你可以写任何业务逻辑,比如根据天气 API 动态调整费用,或者根据用户所在城市使用不同的计费规则:
add_action('woocommerce_shipping_init', 'register_custom_shipping_method');
function register_custom_shipping_method() {
class WC_Shipping_Custom_Dynamic extends WC_Shipping_Method {
public function __construct($instance_id = 0) {
$this->id = 'custom_dynamic';
$this->title = '动态运费';
$this->init();
}
public function init() {
$this->init_form_fields();
$this->init_settings();
add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options'));
}
public function calculate_shipping($package = array()) {
$cost = 10; // 基础运费
// 根据重量、地区、商品数量等动态计算
$weight = WC()->cart->get_cart_contents_weight();
if ($weight > 20) $cost += 15;
if ($weight > 50) $cost += 30;
$rate = array(
'id' => $this->id,
'label' => '动态运费',
'cost' => $cost,
'package' => $package,
);
$this->add_rate($rate);
}
}
}
add_filter('woocommerce_shipping_methods', 'add_custom_shipping_method');
function add_custom_shipping_method($methods) {
$methods['custom_dynamic'] = 'WC_Shipping_Custom_Dynamic';
return $methods;
}
支付方式的定制 同样重要。WooCommerce 默认支持 PayPal、Stripe、银行转账等,但如果你需要接入本地支付平台(如支付宝、微信支付、或其他地区性支付网关),就需要自定义支付网关。
自定义支付网关同样需要创建一个类,继承 WC_Payment_Gateway。核心方法包括 init_form_fields(定义设置项)、process_payment(处理支付逻辑)和 validate_fields(前端验证):
add_action('plugins_loaded', 'init_custom_payment_gateway');
function init_custom_payment_gateway() {
class WC_Gateway_Custom_Pay extends WC_Payment_Gateway {
public function __construct() {
$this->id = 'custom_pay';
$this->method_title = '自定义支付';
$this->title = '自定义支付';
$this->init_form_fields();
$this->init_settings();
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
}
public function init_form_fields() {
$this->form_fields = array(
'enabled' => array('title' => '启用', 'type' => 'checkbox', 'default' => 'no'),
'api_key' => array('title' => 'API 密钥', 'type' => 'text'),
);
}
public function process_payment($order_id) {
$order = wc_get_order($order_id);
// 调用第三方支付 API
// 如果成功,重定向到感谢页
return array(
'result' => 'success',
'redirect' => $this->get_return_url($order),
);
}
}
}
add_filter('woocommerce_payment_gateways', 'add_custom_payment_gateway');
function add_custom_payment_gateway($gateways) {
$gateways[] = 'WC_Gateway_Custom_Pay';
return $gateways;
}
运费和支付方式的定制,是 WooCommerce 电商项目中最常见的定制需求之一。掌握这两种定制方法,就能让 WooCommerce 适应绝大多数地区的物流和支付习惯,无论你的业务在哪个国家或地区运营,都能提供顺畅的结算体验。

