2015-09-10 194 views
3

我正在嘗試向我的Woocommerce套件添加新的送貨方式。以編程方式向Woocommerce添加新的送貨方式

基本上我試圖根據用戶角色對訂單應用兩種不同的運費。我使用統一費率作爲其中之一,但需要定製一個。

我試過the shipping api來創建一個新的類,但似乎沒有出現。

<?php namespace MySite; 

use WC_Shipping_Method; 

    function your_shipping_method_init() { 
     if (! class_exists('CustomShipping')) { 

      class CustomShipping extends WC_Shipping_Method { 
       /** 
       * Constructor for your shipping class 
       * 
       * @access public 
       * @return void 
       */ 
       public function __construct() { 
        $this->id     = 'your_shipping_method'; // Id for your shipping method. Should be uunique. 
        $this->method_title  = __('Your Shipping Method'); // Title shown in admin 
        $this->method_description = __('Description of your shipping method'); // Description shown in admin 

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

        $this->init(); 
       } 

       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' => '10.99', 
         '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[] = 'CustomShipping'; 
     return $methods; 
    } 

    add_filter('woocommerce_shipping_methods', 'add_your_shipping_method'); 
} 

我使用Composer來加載我的類。

難道我不應該看到Woocommerce Shipping設置中的新選項嗎?任何指導讚賞。

另一種方法是攔截會話數據,然後在那裏更改運費和稅金。但這似乎並不奏效。我在哪裏可以找到有關運送信息來源的更多數據;何時何地被叫?

回答

2

你的代碼有兩個問題。首先,您要在附加到它的函數中添加woocommerce_shipping_init的動作(因此它永遠不會被調用),第二個是您沒有使用您指定的名稱空間namespace MySite

如果您檢查代碼的縮進,它的所有內容都包含在init函數中,第一個問題更容易看出。

第二個問題在您的日誌中顯而易見,一旦WooCommerce嘗試加載該類,但由於第一個問題,它不會執行,類似如下錯誤。

PHP Fatal error: Class 'WC_Your_Shipping_Method' not found in /wp-content/plugins/woocommerce/includes/class-wc-shipping.php on line 136

PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'add_your_shipping_method' not found or invalid function name in /wp-includes/plugin.php on line 213

我刪除您的實現代碼,但以下這個測試在開發網站,它表現爲在WooCommerce設置的送貨方式。

/* 
    Plugin Name: WooCommerce Your Shipping Method 
    Description: Test shipping method 
    Version: 1.0 
*/ 

namespace MySite; 

use WC_Shipping_Method; 

function your_shipping_method_init() { 
    if (! class_exists('WC_Your_Shipping_Method')) { 

     class WC_Your_Shipping_Method extends WC_Shipping_Method { 
      // ... all your code for the implementation 
     } 
    } 
} // <-- note that the function is closed before the add_action('woocommerce_shipping_init') 

// note "MySite\" prepended to the attached function 
add_action('woocommerce_shipping_init', 'MySite\your_shipping_method_init'); 

// note "MySite\" prepended to the shipping method class name 
function add_your_shipping_method($methods) { 
    $methods[] = 'MySite\WC_Your_Shipping_Method'; 
    return $methods; 
} 

// note "MySite\" prepended to the attached function 
add_filter('woocommerce_shipping_methods', 'MySite\add_your_shipping_method'); 
+0

啊,是的,我的行動是嵌套的。 @doublesharp我已經實現了你的代碼,看起來好像'your_shipping_method_init'從來沒有被調用過。死亡和傾倒在裏面什麼都沒有做。我的'add_action'和'add_filter'調用不應該在這個文件裏面嗎? – Tuesdave

+0

你把這些代碼放在哪裏?唯一的區別是我添加了一個頭文件,使其成爲一個插件,然後啓用它,否則它在我的測試網站上正常工作。你也可以把它放入'mu-plugins'中。 – doublesharp

+0

我使用插件頭更新了示例代碼。創建一個名爲'/ wp-content/plugins/my-shipping /'的文件夾並將文件放入其中,然後在插件中啓用「WooCommerce Your Shipping Method」,它應顯示在WooCommerce設置中。 – doublesharp

-1

我不知道你的代碼的確切問題,但其他航運插件不喜歡它的下方,所以我可能會複製它們:

首先把你的方法在另一個文件classes/class-wc-your-shipping-method.php

if (! class_exists('WC_Your_Shipping_Method')) { 

    class WC_Your_Shipping_Method extends WC_Shipping_Method { 
     // your class stuff truncated for brevity 
    } 

} 

則包括航運類文件從某處你的插件:

function so_32509983_init() { 
    include_once('classes/class-wc-your-shipping-method.php'); 
} 
add_action('woocommerce_shipping_init', 'so_32509983_init'); 

終於說出WooCommerce來初始化方法,它的活動方法之一s:

function so_32509983_add_method($methods) { 
    $methods[] = 'WC_Your_Shipping_Method'; 
    return $methods; 
} 
add_filter('woocommerce_shipping_methods', 'so_32509983_add_method'); 
+0

函數內部的類很好,問題是'add_action()'調用在它所連接的函數內部,所以它永遠不會被調用。您還必須在類\函數中指定一個名稱空間,例如'$ methods [] ='MySite \ WC_Your_Shipping_Method''。 – doublesharp

+0

我想我不知道名稱空間。 Sans命名空間,這正是UPS和USPS功能的工作原理,所以它確實回答了「如何向WooCommerce添加新的運輸方式」的問題。 – helgatheviking

+0

排序,但它不回答被問的問題。主要問題是在掛鉤函數中的add操作調用。 – doublesharp

相關問題