簡體   English   中英

woocommerce 自定義運輸方式未出現在運輸區

[英]woocommerce custom shipping method not appearing in 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;
}

它應該解決問題。 如果您需要調試它或想了解更多,請查看:

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

看着

function add_shipping_method($type)

簡而言之:您在woocommerce_shipping_init鈎子中注冊類的 id 必須與類 id 屬性匹配(您使用$this->id = 'launch_shipping';設置$this->id = 'launch_shipping';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM