主题的“ inc”文件夹中的woocommerce.php文件中的覆盖函数

时间:2019-03-09 15:29:24

标签: php wordpress filter woocommerce hook-woocommerce

我一直在寻找答案,但是我被困住了。对不起,我一点都不了解php。

在我的theme / 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())的内容,但我自己无法解决。

你能帮我吗?

1 个答案:

答案 0 :(得分:3)

由于这是一个过滤器挂钩,因此在具有更高优先级的挂钩的重命名函数中使用代码应替换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文件中。这应该起作用。