WooCommerce扩展WC_Shipping_Method错误:找不到类'WC_Shipping_Method'

时间:2013-02-20 04:14:16

标签: wordpress-plugin woocommerce extending-classes

标题解释得很好。我正在尝试通过扩展课程来添加新的送货方式(基于成本的运费)。这是插件体(其余部分在注释中并包含插件信息):

class WC_Cost_Based extends WC_Shipping_Method {
    function __construct() {
        $this->id = 'cost-based';
        $this->method_title = __('Cost-Based', 'woocommerce');
    }

    function calculate_shipping() {
        $rate = array(
            'id' => $this->id,
            'label' => $this->title,
            'cost' => '10.99',
            'calc_tax' => 'per_item'
            );
    // Register the rate
        $this->add_rate( $rate );
    }

}

function add_cost_based_method( $methods ) {
    $methods[] = 'WC_Cost_Based';
    return $methods;
}

add_filter('woocommerce_shipping_methods', 'add_cost_based_method');

该文件保存在... / wp-content / cost-based

知道为什么会出现这个错误?

2 个答案:

答案 0 :(得分:0)

我在尝试遵循woocommerce指令/教程时遇到了同样的问题。看来他们已经离开了一些东西。

首先,为了解决您的问题,我发现我需要将woocommerce.php包含在我的插件类的顶部。这将为新的货运类提供对其需要扩展的WC_Shipping_Method的访问权限。可能有一种更优雅的方式只包括依赖类。对我来说包括以下内容:

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

其次,我发现我还必须在__construct()方法中设置至少两个以上的属性。他们是:

 $this->title = "YOUR TITLE";  // Displays on the main shipping page
 $this->enabled = true;

第三,我还需要(至少):

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

希望它有所帮助!

西蒙

答案 1 :(得分:0)

导航至-https://github.com/woocommerce/woocommerce/blob/master/includes/abstracts/abstract-wc-shipping-method.php

将所有内容复制到php文件中,并替换为在以下位置找到的文件-abstract-wc-shipping-method.php-  wp-content / plugins / woocommerce / includes / abstracts /

相关问题