【问题标题】:Override function in woocommerce.php file inside theme's "inc" folder覆盖主题的“inc”文件夹内的 woocommerce.php 文件中的函数
【发布时间】:2019-07-31 09:52:37
【问题描述】:

我一直在寻找答案,但我被困住了。对不起,我根本不懂php。

在我的主题/inc/functions/ 文件夹中有一个名为 woocommerce.php 的文件

Here is a link to the complete code

我想替换这部分代码(从第 1067 行到第 1080 行)

add_filter( 'woocommerce_registration_errors', 'registration_errors_validation', 10, 3 );

function registration_errors_validation( $reg_errors, $sanitized_user_login, $user_email ) {
global $porto_settings, $woocommerce;
if ( isset( $porto_settings['reg-form-info'] ) && 'full' == $porto_settings['reg-form-info'] && 'no' === get_option( 'woocommerce_registration_generate_password' ) ) {
    extract( $_POST );
    if ( strcmp( $password, $confirm_password ) !== 0 ) {
        return new WP_Error( 'registration-error', __( 'Passwords do not match.', 'porto' ) );
    }
    return $reg_errors;
}
return $reg_errors;
}

有了这个:

add_filter('woocommerce_registration_errors', 'registration_errors_validation', 10,3);

function registration_errors_validation($reg_errors, $sanitized_user_login, $user_email) {
global $porto_settings, $woocommerce;
if( isset( $porto_settings['reg-form-info'] ) && $porto_settings['reg-form-info'] == 'full' && 'no' === get_option( 'woocommerce_registration_generate_password' ) ){
    extract( $_POST );
    if ( strcmp( $posted['account_password'], $posted['account_confirm_password'] ) !== 0 ) {
        return new WP_Error( 'registration-error', __( 'Passwords do not match.', 'porto' ) );
    }
    return $reg_errors;
}
return $reg_errors;
}

我尝试将修改后的代码粘贴到主题的functions.php 中,但我收到一个致命错误选项,提示无法重新声明函数registration_errors_validation。我还用inc/functions/ 子文件夹创建了一个子主题,并将修改后的woocommerce.php 文件复制到那里。最后,我还复制了子根文件夹中的woocommerce.php 文件。 没有任何效果。

我已经阅读了在开头添加 if( !function_exists() ) 的内容,但我自己无法解决。

你能帮帮我吗?

【问题讨论】:

    标签: php wordpress filter woocommerce hook-woocommerce


    【解决方案1】:

    由于这是一个过滤器挂钩,因此在具有更高优先级挂钩的重命名函数中使用您的代码应该替换 Porto 主题的过滤数据……试试这个:

    add_filter( 'woocommerce_registration_errors', 'custom_registration_errors_validation', 20, 3 );
    function custom_registration_errors_validation( $reg_errors, $sanitized_user_login, $user_email ) {
        global $porto_settings;
    
        if ( isset( $porto_settings['reg-form-info'] ) && 'full' == $porto_settings['reg-form-info'] && 'no' === get_option( 'woocommerce_registration_generate_password' ) ) {
            extract( $_POST );
            if ( strcmp( $posted['account_password'], $posted['account_confirm_password'] ) !== 0 ) {
                return new WP_Error( 'registration-error', __( 'Passwords do not match.', 'porto' ) );
            }
        }
        return $reg_errors;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。这应该可以。

    【讨论】:

    • LoicTheAztec:谢谢你的回答。虽然没有出现致命错误,但代码没有按预期工作。它什么都不做。
    • @user3741840 对于过滤器挂钩,这是正确的方法……现在您可能需要定义 $porto_settings 变量,因为此代码用完了相关插件,对此我真的不知道: 你最好问问波尔图的支持,在那里开一个帖子
    • LoicTheAztec:事实上你的代码有效!!我犯了一个非常愚蠢的错误并急忙回答。为了确定,我做了不同的测试,然后意识到它是有效的。非常感谢!
    猜你喜欢
    • 2018-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多