【问题标题】:Fatal error: Class 'WC_Settings_Page' not found when use namespace致命错误:使用命名空间时找不到类“WC_Settings_Page”
【发布时间】:2020-04-10 17:39:11
【问题描述】:

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

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

<?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 插件,所以我可以使用来自 woocommerce_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 后加载但它不起作用。

我该如何解决这个问题?

【问题讨论】:

    标签: wordpress woocommerce hook-woocommerce


    【解决方案1】:

    我不会在 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();
    

    【讨论】:

    • 非常感谢您的帮助。它工作正常。只需要在其文件中初始化类。非常感谢
    猜你喜欢
    • 2017-05-01
    • 2020-01-28
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-19
    • 2019-08-11
    • 1970-01-01
    相关资源
    最近更新 更多