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