PHP致命错误:在自定义类插件中找不到类'WC_Shipping_Method'

时间:2013-08-30 15:10:47

标签: wordpress wordpress-plugin woocommerce

我已经为WooCommerce创建了一个送货方式插件:http://pastebin.com/SCsbDnnn

但是,它不起作用并提供此输出: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

4 个答案:

答案 0 :(得分:2)

也许我误解了,但是:

您的插件目录名称为class-wc-shipping-per-product目录中的plugins

所以,要包含woocommerce.phpabstract-wc-shipping-method.php(在plugins/woocommerce目录中),你应该在一个目录上:

文件: wp-content / plugins / class-wc-shipping-per-product / class-wc-s hipping-per-product.php

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

答案 1 :(得分: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);
        }

答案 2 :(得分: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;
}

答案 3 :(得分: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
}