dongmang3961
2018-10-04 09:07
浏览 76
已采纳

在Woocommerce 3中保存时,发送包含其他帐户字段值的自定义电子邮件

On the basis of the plugin “Advanced Custom Fields” additional fields were created for the personal account of WooCommerce users. Using the second "ACF for WooCommerce" plugin, I placed these fields on the edit-account page.

If the user has filled in the fields or edited them, the administrator will receive an email with the values of these fields. The following code is responsible for notifications:

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();
}

Here two errors occur:

  1. When a user edits these fields, old, apparently cached data is sent to the administrator email. When you re-send without editing, you will receive the correct field data.

    I put a line of code:

    update_meta_cache('user', $user_id);
    

    But it does not work, the old data is still coming. Apparently, I did something wrong.

  2. The data of all fields are correctly stored in the database and also correctly displayed in an email to the administrator. The problem with checkboxes.

    In this case, the "What style do you prefer?" three checkboxes "Classic", "Casual" and "Sport" are displayed. The user selected the "Casual" checkbox. The value of this field is correctly stored in the database.

    But in the email to the administrator, instead of this value, only the word “Array” comes.

Tell me how to fix it?

Any help is appreciated.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题