在插件“高级自定义字段”的基础上,为 WooCommerce 用户的个人帐户创建了额外的字段。使用第二个“ACF for WooCommerce”插件,我将这些字段放在编辑帐户页面上。
如果用户填写了字段或对其进行了编辑,管理员将收到一封包含这些字段值的电子邮件。以下代码负责通知:
if (!class_exists('WooCommerceNotifyChanges')) {
class WooCommerceNotifyChanges {
function __construct() {
add_action('woocommerce_save_account_details', array($this, 'woocommerce_send_notification'), 15, 1);
}
function woocommerce_send_notification($user_id) {
$body = '';
$to = 'info@domain.com';
$subject = 'Edit profile data';
update_meta_cache('user', $user_id); // delete cache
$user = new WP_User($user_id);
$user_name = $user->user_login;
$body .= '<table>';
$body .= '<tr><td>'.__("Profile"). '</td></tr>';
$body .= '<tr><td>Login: </td><td>'.$user_name. '</td></tr>';
$body .= '<tr><td>First Name: </td><td>'.$user->billing_first_name. '</td></tr>';
$body .= '<tr><td>Last Name: </td><td>'.$user->billing_last_name. '</td></tr>';
$body .= '<tr><td>Phone: </td><td>'.get_user_meta($user_id, 'field_5b4640119354c', $single=true). '</td></tr>'; //text field
$body .= '<tr><td>Age: </td><td>'.get_user_meta($user_id, 'field_5b462d304b101', $single=true). '</td></tr>'; //text field
$body .= '<tr><td>Family: </td><td>'.get_user_meta($user_id, 'field_5b4bd7d9f0031', $single=true). '</td></tr>'; // selector
$body .= '<tr><td>What style do you prefer? </td><td>'.get_user_meta($user_id, 'field_5b47917a378ed', $single=true). '</td></tr>'; // checkbox
$body .= '</table>';
//set content type as HTML
$headers = array('Content-Type: text/html; charset=UTF-8;');
//send email
if (wp_mail($to, $subject, $body, $headers)) {
//echo 'email sent';
} else {
//echo 'email NOT sent';
}
//exit();
}
}
new WooCommerceNotifyChanges();
}
当用户编辑这些字段时,旧的、明显缓存的数据会发送到管理员电子邮件。当您在不编辑的情况下重新发送时,您将收到正确的字段数据。
我放了一行代码:
update_meta_cache('user', $user_id);
但是还是不行,旧数据还是来了。显然,我做错了什么。
所有字段的数据都正确存储在数据库中,并在发给管理员的电子邮件中正确显示。复选框的问题。
在这种情况下,“你喜欢什么风格?”显示三个复选框“经典”、“休闲”和“运动”。用户选择了“休闲”复选框。此字段的值已正确存储在数据库中。
但是在给管理员的电子邮件中,没有这个值,只有“Array”这个词。
告诉我怎么解决?
感谢任何帮助。
最佳答案
1)缓存问题:我终于在“ACF for WooCommerce”插件源代码中找到了问题。
You need to use a higher priority value as the plugin uses a priority of 100
因此您将在您的代码中替换(在构造函数中):
add_action('woocommerce_save_account_details', array($this, 'woocommerce_send_notification'), 15, 1);
按优先级值 150 (或更多) 例如:
add_action('woocommerce_save_account_details', array($this, 'woocommerce_send_notification'), 150, 1);
它应该最终解决您的虚假“缓存”问题(感谢 Kashalo)。
2)“您喜欢哪种风格?”的复选框字段。
您需要将数组转换为以逗号分隔值的字符串……
使用以下内容:
function woocommerce_send_notification($user_id) {
// Recipient
$to = 'info@domain.com';
$subject = 'Edit profile data';
$user = new WP_User($user_id);
$user_name = $user->user_login;
$phone = get_user_meta( $user_id, 'field_5b4640119354c', true ); // Phone - text field
$age = get_user_meta( $user_id, 'field_5b462d304b101', true ); // Age- text field
$family = get_user_meta( $user_id, 'field_5b4bd7d9f0031', true ); // Family - select field
$fstyle = get_user_meta( $user_id, 'field_5b47917a378ed', true ); // Favorited styles - Multiple Checkboxes
// Convert the array to a coma separated list (Favorited styles)
$fstyle = implode( ', ', $fstyle );
// Body output
$body = '<h3>'. __("Profile") .'</h3>
<table>
<tr><td>Login: </td><td>'. $user->user_login .'</td></tr>
<tr><td>First Name: </td><td>'. $user->billing_first_name .'</td></tr>
<tr><td>Last Name: </td><td>'. $user->billing_last_name .'</td></tr>
<tr><td>Phone: </td><td>'. $phone .'</td></tr>
<tr><td>Age: </td><td>'. $age .'</td></tr>
<tr><td>Family: </td><td>'. $family .'</td></tr>
<tr><td>What style do you prefer? </td><td>'. $fstyle .'</td></tr>
</table>';
//set content type as HTML
$headers = array('Content-Type: text/html; charset=UTF-8;');
//send email
$sent = wp_mail( $to, $subject, $body, $headers );
}
Testing and trying to reproduce your issue is not possible within the provided information and code in this question, as nobody can guess your ACF settings and related context.
Using Advanced Custom fields is not a good idea for that, when doing multiple customizations. You are loosing so much time, trying to find out what is wrong since a while.
The best way is to hand code custom fields without using a plugin as developers do.
在下面的代码中我是:
Now as you dont give the option values for the "Family" select field and for the "Preferred style" checkboxes, you will have to set the desired values in the settings section in each array.
When testing this code you will have to disable the related ACF fields.
完整的功能代码(没有ACF):
if ( ! class_exists('WC_Additional_Account_fields') ) {
class WC_Additional_Account_fields {
// 1. Constructor
function __construct() {
## A. Settings
// The text domain
$this->text_domain = 'aafields';
// The current user WP_User object
$this->user = wp_get_current_user();
// The Admin email
$this->admin_email = get_option('admin_email');
// The array of options for "Family" and "preferred style"
$this->fields_options = array(
// FAMILY available options array
'family' => array(
__("No childs", $this->text_domain ),
__("One child", $this->text_domain ),
__("2 childs", $this->text_domain ),
__("3 or more childs", $this->text_domain ),
),
// PREFERRED STYLE available options array
'preferred_styles' => array(
__("Style 1", $this->text_domain ),
__("Style 2", $this->text_domain ),
__("Style 3", $this->text_domain ),
__("Style 4", $this->text_domain ),
),
);
## B. Hooking functions
add_action('woocommerce_edit_account_form', array($this, 'add_other_fields_in_edit_account_form') );
add_action('woocommerce_save_account_details', array($this, 'save_other_fields_in_edit_account_form'), 15, 1);
add_action('woocommerce_save_account_details_errors', array($this, 'validate_other_fields_in_edit_account_form'), 15, 1 );
add_filter('woocommerce_customer_meta_fields', array($this, 'add_customer_meta_fields_to_admin_user_list'), 15, 1 );
}
// 1. Displaying additional account fields (+ jQuery script to handle checkboxes as radio buttons)
public function add_other_fields_in_edit_account_form() {
$text_domain = $this->text_domain;
$user = $this->user;
$options = $this->fields_options;
$phone = isset($user->phone) ? $user->phone : '';
$age = isset($user->billing_age) ? $user->billing_age : '';
$family = isset($user->billing_family) ? $user->billing_family : '';
$pstyle = isset($user->billing_pref_style) ? $user->billing_pref_style : '';
// Inline styles
?>
<style>
#billing_pref_style_field span.label {padding:0 12px 0 6px;}
div.clear {clear:both;margin-bottom:1.4em;}
</style>
<?php
// Fields 1 and 2
?>
<p class="woocommerce-form-row woocommerce-form-row--first form-row form-row-first">
<label for="phone"><?php _e( "Phone number", $text_domain ); ?><span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="phone" id="phone" value="<?php echo $phone; ?>" />
</p>
<p class="woocommerce-form-row woocommerce-form-row--last form-row form-row-last">
<label for="billing_age"><?php _e( "Age", $text_domain ); ?><span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="billing_age" id="billing_age" value="<?php echo $age; ?>" />
</p>
<?php
// Fields 3 and 4 (Select field + Grouped checkboxes)
?>
<p class="woocommerce-form-row woocommerce-form-row--last form-row form-row-first">
<label for="billing_family"><?php _e( "Family", $text_domain ); ?><span class="required">*</span></label>
<select id="billing_family" class="woocommerce-Select woocommerce-Select--option select" name="billing_family">
<option value=""><?php _e( "Select a value", $text_domain ); ?></option>
<?php
foreach( $options['family'] as $option ) :
$selected = ( $family == $option ) ? ' selected="selected"' : '';
echo '<option value="' . $option . '"' . $selected . '>' . $option . '</option>';
endforeach;
?>
</select>
</p>
<p id="billing_pref_style_field" class="woocommerce-form-row woocommerce-form-row--last form-row form-row-last">
<label for="preferred_styles"><?php _e( "What style do you prefer? ", $text_domain ); ?><span class="required">*</span></label>
<?php
foreach( $options['preferred_styles'] as $key => $option ) :
$checked = $pstyle == $option ? ' checked="checked"' : '';
echo '<span><input type="checkbox" class="woocommerce-Input woocommerce-Input--checkbox input-checkbox" name="pref_style" value="' . $option . '"' . $checked . '><span class="label">' . $option . '</span></span>';
endforeach;
?>
</select>
<?php $value = ! empty($pstyle) ? $pstyle : $options['preferred_styles'][0];
// Hidden field that catch the active checkbox value ?>
<input type="hidden" name="billing_pref_style" value="<?php echo $value; ?>">
</p>
<div class="clear"></div>
<?php
// jQuery code: Enabling grouped checkboxes and passing the chosen value to the hidden field
?>
<script type="text/javascript">
jQuery(function($){
var a = '#billing_pref_style_field', b = a+' input[type="checkbox"]',
c = a+' input[type="hidden"]';
$(b+'[value="'+$(c).val()+'"]').attr('checked', true);
$(b).click( function(){
var d = $(this).val();
$(c).val(d);
$(b).each( function( i, v ) {
if( d != $(v).val() ){
$(v).prop('checked', false);
} else {
$(v).prop('checked', true);
}
});
});
});
</script>
<?php
}
// 2. Additional account fields validation
public function validate_other_fields_in_edit_account_form( $args ){
if ( isset($_POST['phone']) && empty($_POST['phone']) )
$args->add( 'error', __( 'Please fill in the "Phone" field', 'woocommerce' ),'');
if ( isset($_POST['billing_age']) && empty($_POST['billing_age']) )
$args->add( 'error', __( 'Please fill in the "Age" field', 'woocommerce' ),'');
if ( isset($_POST['billing_family']) && empty($_POST['billing_family']) )
$args->add( 'error', __( 'Please choose a value for the "Family" field', 'woocommerce' ),'');
if ( isset($_POST['billing_pref_style']) && empty($_POST['billing_pref_style']) )
$args->add( 'error', __( 'Please choose a "Preferred style"', 'woocommerce' ),'');
}
// 3. Save custom additional fields value + Send custom email
public function save_other_fields_in_edit_account_form( $user_id ) {
$text_domain = $this->text_domain;
$send_notification = false;
if( isset( $_POST['phone'] ) ){
update_user_meta( $user_id, 'phone', sanitize_text_field( $_POST['phone'] ) );
update_user_meta( $user_id, 'billing_phone', sanitize_text_field( $_POST['phone'] ) );
if( ! empty( $_POST['phone'] ) )
$send_notification = true;
}
// For Favorite color 2
if( isset( $_POST['billing_age'] ) ){
update_user_meta( $user_id, 'billing_age', sanitize_text_field( $_POST['billing_age'] ) );
if( ! empty( $_POST['billing_age'] ) )
$send_notification = true;
}
// For Billing email (added related to your comment)
if( isset( $_POST['billing_family'] ) ){
update_user_meta( $user_id, 'billing_family', esc_attr( $_POST['billing_family'] ) );
if( ! empty( $_POST['billing_family'] ) )
$send_notification = true;
}
// For Billing email (added related to your comment)
if( isset( $_POST['billing_pref_style'] ) ){
update_user_meta( $user_id, 'billing_pref_style', esc_attr( $_POST['billing_pref_style'] ) );
if( ! empty( $_POST['billing_pref_style'] ) )
$send_notification = true;
}
if( $send_notification ){
$user = new WP_User($user_id);
$to = $this->admin_email;
$subject = __('Edited profile data for: ') . $user->billing_first_name . ' ' . $user->billing_last_name;
$body = '<h3>' . __("Profile", $text_domain ) . '</h3>
<table>
<tr><th align="left">' . __("Login:", $text_domain ) . '</th><td>'. $user->user_login . '</td></tr>
<tr><th align="left">' . __("First Name:", $text_domain ) . ' </th><td>' . $user->billing_first_name . '</td></tr>
<tr><th align="left">' . __("Last Name:", $text_domain ) . ' </th><td>' . $user->billing_last_name . '</td></tr>
<tr><th align="left">' . __("Phone:", $text_domain ) . ' </th><td>' . $user->phone . '</td></tr>
<tr><th align="left">' . __("Age:", $text_domain ) . ' </th><td>' . $user->billing_age . '</td></tr>
<tr><th align="left">' . __("Family:", $text_domain ) . ' </th><td>' . $user->billing_family . '</td></tr>
<tr><th align="left">' . __("Preferred style", $text_domain ) . ' </th><td>' . $user->billing_pref_style . '</td></tr>
</table>';
//set content type as HTML
$headers = array('Content-Type: text/html; charset=UTF-8;');
//send email
wp_mail($to, $subject, $body, $headers);
}
}
// 4. Add the additional fields in admin user list in the billing section
public function add_customer_meta_fields_to_admin_user_list( $args ) {
$domain = $this->text_domain;
$options = $this->fields_options;
// Age
$args['billing']['fields']['billing_age'] = array(
'label' => __( "Age", $text_domain ),
'description' => '',
);
// Family
$args['billing']['fields']['billing_family'] = array(
'label' => __( 'Family', 'woocommerce' ),
'description' => '',
'type' => 'select',
'options' => array( '' => __( 'Select a value', $domain ) ) + array_combine($options['family'], $options['family']),
);
// Preferred styles
$args['billing']['fields']['billing_pref_style'] = array(
'label' => __( 'Preferred styles', 'woocommerce' ),
'description' => '',
'type' => 'select',
'options' => array( '' => __( 'Select a value', $domain ) ) + array_combine($options['preferred_styles'], $options['prefered_styles']),
);
return $args;
}
}
new WC_Additional_Account_fields();
}
代码进入您的事件子主题(事件主题)的 function.php 文件。经过测试并有效。
The phone field updates both user phone and user billing phone at the same time.
我的帐户 > 帐户详细信息部分的附加字段:
字段验证:
电子邮件通知(关于“保存更改”)充满了新保存的数据:
最后是在计费部分下的管理员用户页面中显示的字段:
关于php - 在 Woocommerce 3 中保存时发送带有附加帐户字段值的自定义电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52643003/
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
rails中是否有任何规定允许站点的所有AJAXPOST请求在没有authenticity_token的情况下通过?我有一个调用Controller方法的JqueryPOSTajax调用,但我没有在其中放置任何真实性代码,但调用成功。我的ApplicationController确实有'request_forgery_protection'并且我已经改变了config.action_controller.consider_all_requests_local在我的environments/development.rb中为false我还搜索了我的代码以确保我没有重载ajaxSend来发送
我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些
有这些railscast。http://railscasts.com/episodes/218-making-generators-in-rails-3有了这个,你就会知道如何创建样式表和脚手架生成器。http://railscasts.com/episodes/216-generators-in-rails-3通过这个,您可以了解如何添加一些文件来修改脚手架View。我想把两者结合起来。我想创建一个生成器,它也可以创建脚手架View。有点像RyanBates漂亮的生成器或web_app_themegem(https://github.com/pilu/web-app-theme)。我
是否有简单的方法来更改默认ISO格式(yyyy-mm-dd)的ActiveAdmin日期过滤器显示格式? 最佳答案 您可以像这样为日期选择器提供额外的选项,而不是覆盖js:=f.input:my_date,as::datepicker,datepicker_options:{dateFormat:"mm/dd/yy"} 关于ruby-on-rails-事件管理员日期过滤器日期格式自定义,我们在StackOverflow上找到一个类似的问题: https://s
我的工作要求我为某些测试自动生成电子邮件。我一直在四处寻找,但未能找到可以快速实现的合理解决方案。它需要在outlook而不是其他邮件服务器中,因为我们有一些奇怪的身份验证规则,我们需要保存草稿而不是仅仅发送邮件的选项。显然win32ole可以做到这一点,但我找不到任何相当简单的例子。 最佳答案 假设存储了Outlook凭据并且您设置为自动登录到Outlook,WIN32OLE可以很好地完成此操作:require'win32ole'outlook=WIN32OLE.new('Outlook.Application')message=
我们目前正在为ROR3.2开发自定义cms引擎。在这个过程中,我们希望成为我们的rails应用程序中的一等公民的几个类类型起源,这意味着它们应该驻留在应用程序的app文件夹下,它是插件。目前我们有以下类型:数据源数据类型查看我在app文件夹下创建了多个目录来保存这些:应用/数据源应用/数据类型应用/View更多类型将随之而来,我有点担心应用程序文件夹被这么多目录污染。因此,我想将它们移动到一个子目录/模块中,该子目录/模块包含cms定义的所有类型。所有类都应位于MyCms命名空间内,目录布局应如下所示:应用程序/my_cms/data_source应用程序/my_cms/data_ty
如何使此根路径转到:“/dashboard”而不仅仅是http://example.com?root:to=>'dashboard#index',:constraints=>lambda{|req|!req.session[:user_id].blank?} 最佳答案 您可以通过以下方式实现:root:to=>redirect('/dashboard')match'/dashboard',:to=>"dashboard#index",:constraints=>lambda{|req|!req.session[:user_id].b