2016-06-18 7 views
0

私はカスタム配送方法を追加するために下記のコードを使用しました(woocommerce APIによって提供されています)、別の配送方法を追加したいと思います。実際には動作しません。第2の方法は最初のものを置き換えます。Woocommerce複数の配送方法を追加する

私は別の配送方法をどのように作成できますか? はありがとう

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

答えて

0

私はそれをやっていくつかのより良い方法があったかどうかを知りたいしかし、私は

何か間違ったことをやってすることができるクラスname.Previouslyの名前を変更することにより、別の発送方法を追加することに成功したOKコードを2回貼り付けてコピーしているので、私の背景はOOPではありませんが、これを行う正しい方法ではないと思います。

0

私は同じ問題を抱えています。 WC_Shipping_Methodから拡張し、そこに同じコードをすべて保持して、新しいクラスを3つの新しいクラスに拡張しました、自分のIDと型名のあるもの

それは最高の解決策ではないが、重複したものよりもN回同じクラスのコード

関連する問題