【发布时间】:2014-09-05 15:14:17
【问题描述】:
我读到“除非原始函数要被覆盖,否则不。这是基本的 PHP。”
但我正在尝试编辑 WooCommerce 插件而不编辑插件文件,以便可以在不丢失更改的情况下更新插件。
我已经设置了一个子主题并在functions.php中做了以下但它不起作用...
functions.php - 子主题
<?php
require_once(WP_PLUGIN_DIR . '/woocommerce/includes/class-wc-form-handler.php');
class child_WC_Form_Handler extends WC_Form_Handler {
public function process_login() {
parent::process_login();
.
.
.
if ( is_email( $_POST['username'] ) && apply_filters( 'woocommerce_get_username_from_email', true ) ) {
$user = get_user_by( 'email', $_POST['username'] );
if ( isset( $user->user_login ) ) {
$creds['user_login'] = $user->user_login;
} else {
throw new Exception( '<strong>' . __( 'Error', 'woocommerce' ) . ':</strong> ' . __( 'MESSAGE TO BE REPLACED', 'woocommerce' ) );
}
} else {
$creds['user_login'] = $_POST['username'];
}
.
.
.
}
}
?>
class-wc-form-handler.php - 原始函数所在的位置
<?php
class WC_Form_Handler {
.
.
.
public function process_login() {
.
.
.
if ( is_email( $_POST['username'] ) && apply_filters( 'woocommerce_get_username_from_email', true ) ) {
$user = get_user_by( 'email', $_POST['username'] );
if ( isset( $user->user_login ) ) {
$creds['user_login'] = $user->user_login;
} else {
throw new Exception( '<strong>' . __( 'Error', 'woocommerce' ) . ':</strong> ' . __( 'A user could not be found with this email address.', 'woocommerce' ) );
}
} else {
$creds['user_login'] = $_POST['username'];
}
.
.
.
}
.
.
.
}
?>
有没有办法解决这个问题?我想更改登录错误的异常消息。我用“要替换的消息”突出显示了我要更改的消息。
【问题讨论】:
-
您要覆盖什么功能?您可以采取不同的方法。
-
我想覆盖异常的输出消息。我应该编辑问题中的代码吗?
-
嗨,是的,请更新它并在您要更改的消息中包含异常的详细信息。通常可以过滤 Woocommerce 异常消息。
-
我认为 WC_Form_handler 中的大多数异常都会传递给 wc_add_notice,然后调用 apply_filters 以便更改输出。
-
哦,对了!真的吗?无论如何,我已将代码添加到我的问题中
标签: php wordpress woocommerce