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

自定义WooCommerce配送方法无法显示在结账页求助

解决WooCommerce自定义自提配送方法不显示在结账页的问题

问题根源分析

你的代码能在后台正常添加配送方法,但结账页不显示,核心问题有3点:

  1. 自定义配送类未调用父类WC_Shipping_Method的构造方法,导致WooCommerce无法初始化核心逻辑
  2. calculate_shipping方法硬编码配送费用为0,未读取后台配置的费用值
  3. 配送标题固定写死,未加载后台设置的自定义标题

修正后的完整代码

add_filter('woocommerce_shipping_methods', 'add_local_pickup_shipping_method');
function add_local_pickup_shipping_method($methods) {
    $methods['local_shipping'] = 'Local_Shipping_Method';
    return $methods;
}

class Local_Shipping_Method extends WC_Shipping_Method {
    public function __construct($instance_id = 0) {
        // 必须调用父类构造,初始化WooCommerce配送核心逻辑
        parent::__construct($instance_id);
        
        $this->id = 'local_shipping';
        $this->domain = 'local_shipping';
        $this->method_title = __('自提服务', $this->domain);
        $this->method_description = __('WooCommerce门店自提功能', $this->domain);
        $this->supports = array(
            'shipping-zones',
            'instance-settings',
            'instance-settings-modal',
        );

        // 初始化配置字段与设置值
        $this->init_form_fields();
        $this->init_settings();

        // 读取后台配置
        $this->enabled = $this->get_option('enabled');
        $this->title = $this->get_option('title');
        $this->tax_status = $this->get_option('tax_status');
        $this->cost = $this->get_option('cost', 0);

        // 绑定配置保存钩子(父类已处理,可保留或移除)
        add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options'));
    }

    // 定义后台配置字段
    public function init_form_fields() {
        $this->instance_form_fields = array(
            'enabled' => array(
                'title'         => __('启用/禁用'),
                'type'          => 'checkbox',
                'label'         => __('启用该配送方式'),
                'default'       => 'yes',
            ),
            'title' => array(
                'title'         => __('配送方式标题'),
                'type'          => 'text',
                'description'   => __('用户结账时看到的显示标题'),
                'default'       => __('门店自提'),
                'desc_tip'      => true
            ),
            'tax_status' => array(
                'title'   => __('税费状态', 'woocommerce'),
                'type'    => 'select',
                'class'   => 'wc-enhanced-select',
                'default' => 'taxable',
                'options' => array(
                    'taxable' => __('需征税', 'woocommerce'),
                    'none'    => _x('无税费', 'Tax status', 'woocommerce'),
                ),
            ),
            'cost' => array(
                'title'       => __('自提费用', 'woocommerce'),
                'type'        => 'text',
                'placeholder' => '0',
                'description' => __('自提服务的可选收费金额', 'woocommerce'),
                'default'     => '',
                'desc_tip'    => true,
            ),
            // 扩展:免费自提阈值设置
            'free_threshold' => array(
                'title'       => __('免费自提阈值', 'woocommerce'),
                'type'        => 'text',
                'placeholder' => '0',
                'description' => __('订单金额超过此值时,免自提费用', 'woocommerce'),
                'default'     => '',
                'desc_tip'    => true,
            ),
            // 扩展:自提地址展示
            'pickup_address' => array(
                'title'       => __('自提地址', 'woocommerce'),
                'type'        => 'textarea',
                'description' => __('结账时显示的自提地址信息', 'woocommerce'),
                'default'     => __('门店地址:XX市XX区XX路XX号', 'woocommerce'),
                'desc_tip'    => true,
            ),
        );
    }

    public function calculate_shipping($package = array()) {
        // 计算实际费用:订单满阈值免自提费
        $order_subtotal = WC()->cart->subtotal;
        $free_threshold = $this->get_option('free_threshold', 0);
        $actual_cost = ($order_subtotal >= $free_threshold && $free_threshold > 0) ? 0 : $this->cost;

        // 组合配送选项显示内容(含自提地址)
        $rate_label = $this->title;
        $pickup_address = $this->get_option('pickup_address');
        if (!empty($pickup_address)) {
            $rate_label .= '<br><small>' . $pickup_address . '</small>';
        }

        // 添加配送费率,包含税费计算
        $this->add_rate(array(
            'id'    => $this->id . $this->instance_id,
            'label' => $rate_label,
            'cost'  => $actual_cost,
            'taxes' => ($this->tax_status === 'taxable') ? WC_Tax::calc_shipping_tax($actual_cost, WC()->customer->get_shipping_tax_rate()) : array(),
        ));
    }
}

代码扩展说明

  1. 免费自提阈值:后台可设置订单金额门槛,达标自动免自提费
  2. 自提地址展示:结账页配送选项直接显示门店地址,减少用户疑问
  3. 税费自动计算:根据后台配置的税费状态,自动计算并添加对应税费
  4. 规范配置结构:将字段定义移到init_form_fields方法,符合WooCommerce开发规范

验证步骤

  1. 将代码添加到主题functions.php或自定义插件中
  2. 进入WooCommerce后台 > 设置 > 配送区域,确保该方法已启用并分配到对应区域
  3. 模拟下单,检查结账页是否正常显示自提选项,费用与地址是否正确

内容的提问来源于stack exchange,提问作者Nikolas

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 05:45:39