【问题标题】:Adding multiple shipping methods (supporting zones) to woocommerce site向 woocommerce 网站添加多种运输方式(支持区域)
【发布时间】:2017-01-23 05:54:46
【问题描述】:

为了让我的客户网站与第三方集成,我需要创建具有唯一名称的多种运输方式(统一费率)。我按照 woocommerce 教程创建了一个插件来添加运输区域,然后找到了一些说明以使其支持区域并且它起作用了。我现在正在尝试让插件创建多个运输区域,但尽管它们似乎出现在区域中 - 单击它们不会将它们添加到区域中。

e.g. they appear here but don't do anything when clicked

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

function your_shipping_method_init() {
    if ( ! class_exists( 'WA_Metro_Shipping_Method' ) ) {
        class WA_Metro_Shipping_Method extends WC_Shipping_Method {
            /**
             * Constructor for your shipping class
             *
             * @access public
             * @return void
             */
            public function __construct( $instance_id = 0 ) {
                $this->id                 = 'wa_metro_flat'; // Id for your shipping method. Should be uunique.
                $this->method_title       = __( 'WA Metro Flat Rate' );  // Title shown in admin
                $this->method_description = __( 'Flat rate shipping for WA Metro Postcodes' ); // Description shown in admin

                $this->enabled            = "yes"; // This can be added as an setting but for this example its forced enabled
                $this->title              = "WA Metro Flat Rate"; // This can be added as an setting but for this example its forced.

                $this->instance_id = absint( $instance_id );

                $this->supports  = array(
                   'shipping-zones',
                    'instance-settings',
                    'instance-settings-modal',
                 );

                $this->init();
            }

            /**
             * Init your settings
             *
             * @access public
             * @return void
             */
            function init() {
                // Load the settings API
                $this->init_form_fields(); // This is part of the settings API. Override the method to add your own settings
                $this->init_settings(); // This is part of the settings API. Loads settings you previously init.

                // Save settings in admin if you have any defined
                add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
            }


            public function is_available( $package ){
                return true;
            }
            /**
             * calculate_shipping function.
             *
             * @access public
             * @param mixed $package
             * @return void
             */
            public function calculate_shipping( $package ) {
                $rate = array(
                    'id' => $this->id,
                    'label' => $this->title,
                    'cost' => '8.59',
                    'calc_tax' => 'per_item'
                );

                // Register the rate
                $this->add_rate( $rate );
            }
        }
    }

    /***************************************************/

    if ( ! class_exists( 'WA_Regional_Shipping_Method' ) ) {
        class WA_Regional_Shipping_Method extends WC_Shipping_Method {
            /**
             * Constructor for your shipping class
             *
             * @access public
             * @return void
             */
            public function __construct( $instance_id = 0 ) {
                $this->id                 = 'vic_metro_flat'; // Id for your shipping method. Should be uunique.
                $this->method_title       = __( 'VIC Metro Flat Rate' );  // Title shown in admin
                $this->method_description = __( 'Flat rate shipping for VIC Metro Postcodes' ); // Description shown in admin

                $this->enabled            = "yes"; // This can be added as an setting but for this example its forced enabled
                $this->title              = "VIC Metro Flat Rate"; // This can be added as an setting but for this example its forced.

                $this->instance_id = absint( $instance_id );

                $this->supports  = array(
                   'shipping-zones',
                    'instance-settings',
                    'instance-settings-modal',
                 );

                $this->init();
            }

            /**
             * Init your settings
             *
             * @access public
             * @return void
             */
            function init() {
                // Load the settings API
                $this->init_form_fields(); // This is part of the settings API. Override the method to add your own settings
                $this->init_settings(); // This is part of the settings API. Loads settings you previously init.

                // Save settings in admin if you have any defined
                add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
            }

            public function is_available( $package ){
                return true;
            }

            /**
             * calculate_shipping function.
             *
             * @access public
             * @param mixed $package
             * @return void
             */
            public function calculate_shipping( $package ) {
                $rate = array(
                    'id' => $this->id,
                    'label' => $this->title,
                    'cost' => '8.59',
                    'calc_tax' => 'per_item'
                );

                // Register the rate
                $this->add_rate( $rate );
            }
        }
    }



    /***************************************************/

    if ( ! class_exists( 'VIC_Metro_Shipping_Method' ) ) {
        class VIC_Metro_Shipping_Method extends WC_Shipping_Method {
            /**
             * Constructor for your shipping class
             *
             * @access public
             * @return void
             */
            public function __construct( $instance_id = 0 ) {
                $this->id                 = 'wa_regional_flat'; // Id for your shipping method. Should be uunique.
                $this->method_title       = __( 'WA Regional Flat Rate' );  // Title shown in admin
                $this->method_description = __( 'Flat rate shipping for WA Metro Postcodes' ); // Description shown in admin

                $this->enabled            = "yes"; // This can be added as an setting but for this example its forced enabled
                $this->title              = "WA Regional Flat Rate"; // This can be added as an setting but for this example its forced.

                $this->instance_id = absint( $instance_id );

                $this->supports  = array(
                   'shipping-zones',
                    'instance-settings',
                    'instance-settings-modal',
                 );

                $this->init();
            }

            /**
             * Init your settings
             *
             * @access public
             * @return void
             */
            function init() {
                // Load the settings API
                $this->init_form_fields(); // This is part of the settings API. Override the method to add your own settings
                $this->init_settings(); // This is part of the settings API. Loads settings you previously init.

                // Save settings in admin if you have any defined
                add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
            }

            /**
             * calculate_shipping function.
             *
             * @access public
             * @param mixed $package
             * @return void
             */
            public function calculate_shipping( $package ) {
                $rate = array(
                    'id' => $this->id,
                    'label' => $this->title,
                    'cost' => '18.57',
                    'calc_tax' => 'per_item'
                );

                // Register the rate
                $this->add_rate( $rate );
            }
        }
    }

    /***************************************************/
}

add_action( 'woocommerce_shipping_init', 'your_shipping_method_init' );

function add_your_shipping_method( $methods ) {
    $methods['your_shipping_method'] = 'WA_Metro_Shipping_Method';
    $methods['your_shipping_method_2'] = 'WA_Regional_Shipping_Method';
    $methods['your_shipping_method_3'] = 'VIC_Metro_Shipping_Method';
    return $methods;
}

add_filter( 'woocommerce_shipping_methods', 'add_your_shipping_method' );

}

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    在阅读了这篇博文中的 cmets 后,我明白了为什么它不起作用

    https://wpruby.com/shipping-method-for-woocommerce-development-tutorial/

    基本上我在哪里设置方法的 id

    $this->id                 = 'wa_metro_flat';
    

    我没有使用正确的方法名,我使用的是函数名

    $methods['your_shipping_method'] = 'WA_Metro_Shipping_Method';
    

    应该是

    $methods['your_shipping_method'] = 'wa_metro_flat';
    

    【讨论】:

    • 谢谢你,这对我帮助很大!
    • 谢谢,我没有遇到这个问题,但问题中的原始代码帮助我设置了区域方法!
    【解决方案2】:

    其实应该是的

    $methods['wa_metro_flat'] = 'WA_Metro_Shipping_Method';
    

    【讨论】:

      猜你喜欢
      • 2016-10-20
      • 1970-01-01
      • 2015-12-07
      • 2015-12-06
      • 2018-05-05
      • 1970-01-01
      • 2020-08-22
      • 2018-09-19
      • 1970-01-01
      相关资源
      最近更新 更多