2013-08-30 49 views
0

我創建了一個送貨方式附加於WooCommerce發現:http://pastebin.com/SCsbDnnnPHP致命錯誤:類「WC_Shipping_Method」沒有自定義類插件

但是,它不工作,給出了這樣的輸出:PHP致命錯誤:

Class 'WC_Shipping_Method' not found in class-wc-shipping-per-product.php, on line 44. 

爲什麼不找到這個類?我甚至已經包括了兩個文件,以確保,但不走:

include ('/woocommerce/woocommerce.php'); 

include_once('classes/abstracts/abstract-wc-shipping-method.php'); 

而且我已經檢查了這另一個問題,但我可能是不同的,這就是爲什麼我添加了一個新問題:WooCommerce Extending WC_Shipping_Method error: Class 'WC_Shipping_Method' not found

+0

這不是對包括 - 這是關於掛鉤到正確的行動和過濾器鉤子) - 見下文 –

回答

0

的您發佈的代碼是從WooCommerce docs逐字記錄的,它已損壞(!)。

我設法使其工作是這樣的:

if (in_array('woocommerce/woocommerce.php', get_option('active_plugins'))) 
    add_filter('woocommerce_shipping_methods', 'add_shippin_per_product_method'); 

function add_shippin_per_product_method($methods) 
{ 
    // Class definition 
    include('class-wc-shipping-per-product.php'); 
    $methods[] = 'WC_Shipping_Per_Product'; 
    return $methods; 
} 
+0

真棒我的回答!我會嘗試一下。 –

+0

謝謝@brasofilo,但這並沒有幫助。我仍然得到:PHP致命錯誤:未在/ var/www/daniel/test/wp-content/plugins/class-wc-shipping-per-product/class-wc-shipping-per-product中找到Class'WC_Shipping_Method'。 php on line 43,referer:http://test.danielholm.se/wp-admin/plugins.php 最新編號:http://pastebin.com/rrhxzaSw –

2

也許我誤解,但:

你的插件目錄名是:class-wc-shipping-per-productplugins目錄。

所以,要包括woocommerce.phpabstract-wc-shipping-method.php(有plugins/woocommerce目錄內),你應該達到一個目錄和:

文件:的wp-content /插件/類-WC-航運針對每個產品/班-wc-S熱等靜壓每product.php

include_once('../woocommerce/woocommerce.php'); 
include_once('../woocommerce/classes/abstracts/abstract-wc-shipping-method.php'); 
+0

似乎沒有辦法... –

1

我這個最近掙扎太 - 網絡上的各種片段是非常容易引起誤解。這是我這麼遠,它似乎工作...

add_action('woocommerce_shipping_init', 'yourprefix_woocommerce_init_shipping_rate'); 
add_filter('woocommerce_shipping_methods', array('WC_Shipping_XXX_Custom_Rate', 'add_shipping_method')); 

function yourprefix_woocommerce_init_shipping_rate() 
{ 

    class WC_Shipping_XXX_Custom_Rate extends WC_Shipping_Method 
    { 

     static function add_shipping_method($methods) 
     { 
      $methods[] = 'WC_Shipping_XXX_Custom_Rate'; 
      return $methods; 
     } 

     function __construct() 
     { 
      $this->id = 'yourprefix_custom_shipping'; 
      $this->method_title = __('yourprefix Custom Shipping', 'woocommerce'); 
      $this->title = "yourprefix CUSTOM SHIPPING TITLE"; // Displays on the main shipping page 
      $this->enabled = true; 
      $this->init(); 
     } 

     function init() 
     { 
      $this->init_settings(); 
     } 

     function is_available($package) 
     { 
      if ($this->enabled === "no") 
       return false; 
      return apply_filters('woocommerce_shipping_' . $this->id . '_is_available', true); 
     } 
+1

I已經報道了WooCommerce文檔頁面上的遺漏(http://docs.woothemes.com/document/shipping-method-api/),希望它能在那裏得到更新! –

0

通過WooCommerce和其他各種插件的源代碼後看,這是我想出了。在我shipping-plugin.php文件:

add_action('plugins_loaded', 'load_plugins', 0); 
function load_plugins() { 
    require_once(plugin_dir_path(__FILE__) . 'class-custom-shipping.php'); 
} 

add_filter('woocommerce_shipping_methods', array('Custom_Shipping','woocommerce_shipping_methods')); 

的關鍵在於預防類未找到錯誤只包括我的自定義航運類WooCoommerce插件已經被加載後。在我class-custom-shipping.php文件,我有:

class Custom_Shipping extends WC_Shipping_Method { 

    public static function woocommerce_shipping_methods($methods) { 
     $methods[] = 'Custom_Shipping'; return $methods; 
    } 

    public function __construct() { ... } 
    // other methods 
} 
相關問題