2017-07-18 2 views
6

私は新しい配送方法を作成し、配送ゾーンのサポートを与えました。しかし、ドロップダウンからメソッドを選択してゾーンに追加すると、「選択されたメソッドリスト」には表示されません。WooCommerce 3でカスタム作業配送方法を追加する方法

私が実証するscreencast gifを記録:

screencast gif

それは働いていない理由を私は私の人生のために把握することはできません。標準的な方法(Screencast GIF)の1つを選択すると正常に動作します

ここで何が起こっているのか、またそれを動作させる方法は誰でも知っていますか?

ここで私はfrom this official thread: Shipping Method APIを持っているコードです:

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

    function request_a_shipping_quote_init() { 
     if (! class_exists('WC_Request_Shipping_Quote_Method')) { 
      class WC_Request_Shipping_Quote_Method extends WC_Shipping_Method { 
       /** 
       * Constructor for your shipping class 
       * 
       * @access public 
       * @return void 
       */ 
       public function __construct() { 
        $this->id     = 'request_a_shipping_quote'; // Id for your shipping method. Should be uunique. 
        $this->method_title  = __('Request a Shipping Quote'); // Title shown in admin 
        $this->method_description = __('Shipping method to be used where the exact shipping amount needs to be quoted'); // Description shown in admin 

        $this->title = "Request a Shipping Quote"; // This can be added as an setting but for this example its forced. 

        $this->supports = array(
         'shipping-zones' 
        ); 

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

       function init_form_fields() { 

        $this->form_fields = array(

         'enabled' => array(
          'title'  => __('Enable', 'dc_raq'), 
          'type'  => 'checkbox', 
          'description' => __('Enable this shipping method.', 'dc_raq'), 
          'default'  => 'yes' 
         ), 

         'title' => array(
          'title'  => __('Title', 'dc_raq'), 
          'type'  => 'text', 
          'description' => __('Title to be displayed on site', 'dc_raq'), 
          'default'  => __('Request a Quote', 'dc_raq') 
         ), 

        ); 

       } 

       /** 
       * calculate_shipping function. 
       * 
       * @access public 
       * 
       * @param mixed $package 
       * 
       * @return void 
       */ 

       public function calculate_shipping($packages = array()) { 
        $rate = array(
         'id'  => $this->id, 
         'label' => $this->title, 
         'cost'  => '0.00', 
         'calc_tax' => 'per_item' 
        ); 

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

    add_action('woocommerce_shipping_init', 'request_a_shipping_quote_init'); 

    function request_shipping_quote_shipping_method($methods) { 
     $methods['request_shipping_quote_shipping_method'] = 'WC_Request_Shipping_Quote_Method'; 

     return $methods; 
    } 

    add_filter('woocommerce_shipping_methods', 'request_shipping_quote_shipping_method'); 
} 
+0

//wordpress.org/support/topic/official-custom-shipping-class-doesnt-work-anymore-shipping-method-api/ – LoicTheAztec

+0

** WC_Shipping_Methodの 'calculate_shipping()'コアメソッド**と競合しています。**あなたのコードのプラグインで定義されているもの...それはsolvする必要がある点ですed。 **この厳しい基準が守られているから**:WC_Request_Shipping_Shipping_Quote_Method :: calculate_shipping()の宣言は、/www/wp-content/plugins/request_shipping_quote_method.phpのWC_Shipping_Method :: calculate_shipping($ package = Array)と互換性があります。 18 * – LoicTheAztec

+0

@LoicTheAztecこれで成功しましたか? – omukiguy

答えて

2

"woocommerce_shipping_methods"のメソッドキーは、あなたのケースでは電子の配送方法ID

: あなたが

function request_shipping_quote_shipping_method($methods) { 
    $methods['request_shipping_quote_shipping_method'] = 'WC_Request_Shipping_Quote_Method'; 

    return $methods; 
} 

add_filter('woocommerce_shipping_methods', 'request_shipping_quote_shipping_method'); 

を変更する必要があります。https::これは、この国連が支援スレッドに答え参照してください3+ woocommerceではもはや機能していない

function request_shipping_quote_shipping_method($methods) { 
    $methods['request_a_shipping_quote'] = 'WC_Request_Shipping_Quote_Method'; 

    return $methods; 
} 

add_filter('woocommerce_shipping_methods', 'request_shipping_quote_shipping_method'); 
+0

ちょうどこの返信が見つかりました - それを修正!答えてくれてありがとう、非常に感謝しています – jhob101

3

変更が

パブリック関数calculate_shipping($パッケージ=配列())この行に次の行

public function calculate_shipping($package) { 

{

+0

ありがとう。私はそのコードを実装しましたが、それはまだ動作しません。サイトは現在リモートであるため、エラーがスローされるのを確認できず、ローカルで作業してエラーが表示されるかどうかを確認します。 – jhob101

+0

したがって、 '$ package = array()'を追加するとエラーはスローされませんが、振る舞いは変わりません。配送方法は配送方法リストに追加されません。 – jhob101

+0

どのようなエラーが表示されますか? bcoz今、あなたは関数内のパッケージをどのように扱っているのかチェックしなければなりません。今はパッケージの配列です。 – Alice

関連する問題