【问题标题】:Woocommerce Adding Multiple shipping methodsWoocommerce 添加多种运输方式
【发布时间】:2016-10-20 07:47:19
【问题描述】:

我使用下面的代码(由 woocommerce API 提供)添加自定义运输方式,它正在工作,但现在我想添加另一种运输方式,我尝试复制粘贴具有不同类名的相同代码,但它实际上不起作用第二种方法是替换第一种方法

我想知道如何创建另一种运输方式? 谢谢你

    function your_shipping_method_init() {
    if ( ! class_exists( 'WC_Your_Shipping_Method' ) ) {
        class WC_Your_Shipping_Method extends WC_Shipping_Method {
            /**
             * Constructor for your shipping class
             *
             * @access public
             * @return void
             */
            public function __construct() {
                $this->id                 = 'vip_rate'; // Id for your shipping method. Should be uunique.
                $this->method_title       = __( 'VIP Shipping Rate' );  // Title shown in admin
                $this->method_description = __( '$35 flate rate' ); // Description shown in admin

                $this->enabled            = "yes"; // This can be added as an setting but for this example its forced enabled
                $this->title              = "VIP Shipping rate"; // This can be added as an setting but for this example its forced.

                $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 ) {


                $cost=35; 

                $rate = array(
                    'id' => $this->id,
                    'label' => $this->title,
                    'cost' => round($cost,2),
                    '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[] = 'WC_Your_Shipping_Method';
    return $methods;
}

add_filter( 'woocommerce_shipping_methods', 'add_your_shipping_method' );

【问题讨论】:

    标签: wordpress woocommerce shipping


    【解决方案1】:

    我遇到了同样的问题,我的解决方案是一个从 WC_Shipping_Method 扩展的新类,并在那里保留所有相同的代码,我创建了 3 个新类来扩展新类,任何一个都有自己的 ID 和方法类型

    它不是最好的解决方案,但它比重复 N 次相同的类代码更简洁

    【讨论】:

      【解决方案2】:

      好的,我已经通过重命名类名成功地添加了另一个 Shipping 方法。以前我可能做错了什么

      但是我想知道是否有更好的方法,因为我已经复制粘贴了整个代码块两次,我的背景不在 OOP 中但是我认为这不是做这件事的正确方法

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-08-22
        • 2015-12-07
        • 1970-01-01
        • 2014-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多