admin管理员组文章数量:1388877
I've a problem with my child theme. I first had all my stuff and hooks for WooCommerce inside my child themes function.php
file. Everything worked fine until I moved it into a class which get's loaded with an autoloader. For example I've added custom notices when a user saves the account details page:
<?php
namespace App;
defined( 'ABSPATH' ) || exit;
/**
* Account class
*/
class Account {
/**
* @var Account
*/
private static ?Account $instance = null;
/**
* Get instance of class
*
* @return Account
*/
public static function get_instance(): Account {
if ( self::$instance === null ) {
self::$instance = new self;
}
return self::$instance;
}
public function __construct() {
$this->register();
}
private function register(): void {
add_action( 'woocommerce_save_account_details', [ $this, 'save_edit_account_form' ], 12, 1 );
}
/**
* Save account form
*
* @param $user_id int
*/
public function save_edit_account_form( $user_id ): void {
wc_clear_notices();
wc_add_notice( 'Test' ); //Gets no longer displayed anymore
exit( wp_safe_redirect( wc_get_account_endpoint_url( 'settings' ) ) );
}
}
I'm just loading the class in my functions.php
file like this:
Account::get_instance();
I can verify that the class get's loaded and the hook get's fired. There are no error messages. I'm just expecting this output on my account page like before moving the hook and everything into my class.
When I put the hook directly into my functions.php
file of my child theme:
add_action( 'woocommerce_save_account_details', 'save_edit_account_form', 12, 1 );
function save_edit_account_form( $user_id ): void {
wc_clear_notices();
wc_add_notice( 'Test' );
wp_safe_redirect( wc_get_account_endpoint_url( 'settings' ) );
exit;
}
But putting everything into my functions.php
would end in 90000+ lines of code. Thats why I created classes for a lot of my functions.
Does someone has an idea why this happens?
本文标签: WooCommerce function not working correctly after moving from the functionsphp into a class
版权声明:本文标题:WooCommerce function not working correctly after moving from the functions.php into a class 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744582583a2614009.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论