woocommerce 自定义运输方式未出现在运输区 [英] woocommerce custom shipping method not appearing in shipping zone

查看:18
本文介绍了woocommerce 自定义运输方式未出现在运输区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已编辑 - 原始帖子的进展

我创建了一个简单的自定义运输方式插件存根(见下面的代码).

I have created a simple custom shipping method plugin stub (see code below).

插件已注册并且现在出现在我创建送货区域时的送货方式下拉列表中.但是,选择后,送货区域不会出现自定义字段(参见 gif)

The plugin is registered and now appears in the shipping method dropdown when I'm creating a shipping zone. However when selected the custom field doesn't appear for the shipping zone (see gif)

<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {

    function launch_shipping_method() {
        if (!class_exists('Launch_Shipping_Method')) {

            class Launch_Shipping_Method extends WC_Shipping_Method {

                public function __construct( $instance_id = 0 ) {
                    $this->id = 'launch_shipping';
                    $this->instance_id          = absint( $instance_id );
                    $this->method_title         = __('Launch Simple Shipping', 'launch_shipping');
                    $this->method_description   = __('Custom Simple Shipping Method', 'launch_shipping');
                    $this->supports             = array(
                        'shipping-zones',
                        'instance-settings',
                        'instance-settings-modal',
                    );

                    $this->init();
                }

                /**
                 * Initialize Launch Simple Shipping.
                 */
                public function init() {
                    // Load the settings.
                    $this->init_form_fields();
                    $this->init_settings();

                    // Define user set variables.
                    $this->title    = isset($this->settings['title']) ? $this->settings['title'] : __('Launch Shipping', 'launch_shipping');

                    add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options'));
                }

                /**
                 * Init form fields.
                 */
                public function init_form_fields() {
                    $this->form_fields = array(
                        'title'      => array(
                            'title'         => __( 'Title', 'launch_shipping' ),
                            'type'          => 'text',
                            'description'   => __( 'This controls the title which the user sees during checkout.', 'launch_shipping' ),
                            'default'       => $this->method_title,
                            'desc_tip'      => true,
                        )
                    );
                }

                /**
                 * Get setting form fields for instances of this shipping method within zones.
                 *
                 * @return array
                 */
                public function get_instance_form_fields() {
                    return parent::get_instance_form_fields();
                }

                /**
                 * Always return shipping method is available
                 *
                 * @param array $package Shipping package.
                 * @return bool
                 */
                public function is_available( $package ) {
                    $is_available = true;
                    return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package, $this );
                }

                /**
                 * Free shipping rate applied for this method.
                 *
                 * @uses WC_Shipping_Method::add_rate()
                 *
                 * @param array $package Shipping package.
                 */
                public function calculate_shipping( $package = array() ) {
                    $this->add_rate(
                        array(
                            'label'   => $this->title,
                            'cost'    => 0,
                            'taxes'   => false,
                            'package' => $package,
                        )
                    );
                }
            }
        }
    }
    add_action('woocommerce_shipping_init', 'Launch_Shipping_Method');

    function add_launch_shipping_method($methods) {
    $methods[] = 'launch_shipping_method';
    return $methods;
    }
    add_filter('woocommerce_shipping_methods', 'add_launch_shipping_method');

}

推荐答案

试试这个:

function add_launch_shipping_method($methods) {
    $methods['launch_shipping'] = 'launch_shipping_method';
    return $methods;
}

它应该可以解决问题.如果您需要调试它或想了解更多,请查看:

It should fix things. If you need to debug it or want to understand more have a look at:

wp-content/plugins/woocommerce/includes/class-wc-shipping-zone.php

看看

function add_shipping_method($type)

简而言之:您在 woocommerce_shipping_init 钩子中注册类的 id 必须与类 id 属性相匹配(您使用 $this->id = 'launch_shipping';)

Briefly: the id you register the class with in your woocommerce_shipping_init hook must match the class id property (the one you set with $this->id = 'launch_shipping';)

这篇关于woocommerce 自定义运输方式未出现在运输区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆