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



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

我的插件是基于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_ProductsIncludesInit;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/* Some uses is hear */
use Siawood_ProductsIncludesAdminWC_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();
}
}

为了确保Wooccommerce插件已经加载,所以我可以从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_ProductsIncludesAdmin;
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()方法时,我得到了这个错误:

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

这很奇怪,因为我把它挂到了wooccommerce_loaded或plugin_loaded,直到它在加载Wooccommerce后能够加载,但它不起作用。

我该如何解决这个问题?

与其在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_ProductsIncludesAdminWC_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_ProductsIncludesAdmin;
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();

最新更新