duandou1903 2017-01-23 05:54
浏览 224
已采纳

向woocommerce网站添加多种送货方式(支持区域)

For my clients site to integrate with a third party I need to create multiple shipping methods (flat rate) with unique names. I followed the woocommerce turorial to create a plugin to add a shipping zone and then found some instructions to make it support zones and it worked. I'm now trying to make the plugin create multiple shipping zones but although they seem to appear in zones - clicking on them doesn't add them to the zone.

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

}

  • 写回答

2条回答 默认 最新

  • dongshi2458 2017-01-25 03:59
    关注

    I've figured out why it wasn't working after reading the comments on this blog post

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

    Basically where I was setting the id for the method

    $this->id                 = 'wa_metro_flat';
    

    I wasn't using the correct method name in this bit, I was using the function name

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

    It should be

    $methods['your_shipping_method'] = 'wa_metro_flat';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看