Пользовательский способ доставки woocommerce не отображается в зоне доставки - PullRequest
1 голос
/ 26 февраля 2020

РЕДАКТИРОВАНИЕ - прогресс, достигнутый в исходном сообщении

Я создал заглушку плагина простого способа доставки (см. Код ниже).

Плагин зарегистрирован, и теперь появляется в раскрывающемся списке методов доставки, когда я создаю зону доставки. Однако, когда выбрано, пользовательское поле не отображается для зоны доставки (см. Рисунок)

<?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');

}

enter image description here

1 Ответ

1 голос
/ 05 марта 2020

Попробуйте это:

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 хуке, должен соответствовать свойству идентификатора класса (то, которое вы установили с помощью $this->id = 'launch_shipping';)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...