问题:致命错误:使用命名空间时找不到类 'WC_Settings_Page'

我有扩展 WC_Settings_Page 的奇怪问题。我想用它来添加设置选项卡(以及 Woocommerce 设置部分中的一些部分。

我的插件基于 OOP,我的所有文件都有相关的命名空间。我有一个核心类,如下所示(如果你需要查看完整的结构,你可以在我的githubrepo 中找到它:

<?php
/**
 * The file that defines the core plugin class
 *
 * A class definition that includes attributes and functions used across both the
 * public-facing side of the site and the admin area.
 *
 * @package    Siawood_Products
 * @author     Mehdi Soltani Neshan <soltani.n.mehdi@gmail.com>
 * @license    https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3
 * @link       https://wpwebmaster.ir
 * @since      1.0.0
 */

namespace Siawood_Products\Includes\Init;

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
/* Some uses is hear */
use Siawood_Products\Includes\Admin\WC_Siawood_Setting_Tab1;
class Core implements Action_Hook_Interface, Filter_Hook_Interface {
/*constructor of class and other things is here.....*/

    public function init_core() {
            /* FYI: I checked before is woocommerce is active or not, if active, I call init_core() */
            add_action( 'plugins_loaded', [ $this, 'add_setting_page' ] );
    }

    public function add_setting_page(  ) {
        new WC_Siawood_Setting_Tab1();
    }

}

为了确保加载了 Woocommerce 插件,所以我可以使用 fromwoocommerce_loaded钩子(我也用init检查了它,但结果是一样的。

WC_Siawood_Setting_Tab1类是:

<?php
/**
 * WC_Siawood_Setting_Tab Class File
 *
 * This file creates admin setting tab for this plugin
 *
 * @package    Siawood_Products
 * @author     Mehdi Soltani Neshan <soltani.n.mehdi@gmail.com>
 * @license    https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3
 * @link       https://wpwebmaster.ir
 * @since      1.0.1
 */
namespace Siawood_Products\Includes\Admin;

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * WC_Siawood_Setting_Tab Class File
 *
 * This file creates admin setting tab for this plugin
 *
 * @package    Siawood_Products
 * @author     Mehdi Soltani Neshan <soltani.n.mehdi@gmail.com>
 * @link       https://wpwebmaster.ir
 *
 */
class WC_Siawood_Setting_Tab1 extends \WC_Settings_Page {


    /**
     * Constructor.
     *
     * @version 1.3.0
     * @since   1.0.0
     */
    function __construct() {
        $this->id    = 'my_settings';
        $this->label = __( 'my label', 'my-textdomain-woocommerce' );
        parent::__construct();

    }
}




所以当我运行我的插件并调用init_core()方法时,我得到了这个错误:

致命错误:第 29 行的 ...\wp-content\plugins\siawood-products\includes\admin\class-wc-siawood-setting-tab1.php 中找不到类 'WC_Settings_Page'

这很奇怪,因为我将它连接到 woocommerce_loaded 或 plugin_loaded 直到它能够在加载 Woocommerce 后加载,但它不起作用。

我该如何解决这个问题?

解答

我不会在plugins_loaded上初始化类,而是执行以下操作:

    public function init_core() {
        add_filter( 'woocommerce_get_settings_pages', [ $this, 'add_setting_page' ] );
    }

    public function add_setting_page( $settings ) {
        $settings[] = include_once path_to_your_file .  '/class-wc-siawood_setting_tab1 .php'
        return $settings;
    }

为了完整性:

    use Siawood_Products\Includes\Admin\WC_Siawood_Setting_Tab1;
    class Core implements Action_Hook_Interface, Filter_Hook_Interface {
    /*constructor of class and other things is here.....*/

        public function init_core() {
                add_action( 'woocommerce_get_settings_pages', [ $this, 'add_setting_page' ] );
        }

        public function add_setting_page(  ) {
            $settings[] = include_once $path_to_your_file .  '/class-wc-siawood_setting_tab1 .php'
            return $settings;
        }

    }

并且在您的其他类文件中,该类必须自行初始化

    namespace Siawood_Products\Includes\Admin;

    if ( ! defined( 'ABSPATH' ) ) {
        exit;
    }

    class WC_Siawood_Setting_Tab1 extends \WC_Settings_Page {


        /**
         * Constructor.
         *
         * @version 1.3.0
         * @since   1.0.0
         */
        function __construct() {
            $this->id    = 'my_settings';
            $this->label = __( 'my label', 'my-textdomain-woocommerce' );
            parent::__construct();

        }
    }

    return new WC_Siawood_Setting_Tab1();
Logo

WooCommerce社区为您提供最前沿的新闻资讯和知识内容

更多推荐