【问题标题】:Sending an email each time a WooCommerce changes their address每次 WooCommerce 更改地址时发送电子邮件
【发布时间】:2020-04-13 20:52:11
【问题描述】:

我当前的代码是

// Send notification email when customer changes address.
add_action( 'woocommerce_customer_save_address','notify_admin_customer_address_change', 10, 2);
function notify_admin_customer_address_change( $user_id ) {

    // Set the $from_name, $from_email, and $to for your site.
    $from_name = 'Your From Name';
    $from_email = 'email@yourdomain.com';
    $to = 'email@somedomain.com';

    global $woocommerce, $current_user;

    // format email
    $message = 'Username: ' . $current_user->user_login . "\n";
    $message .= 'User Email: ' . $current_user->user_email . "\n";
    $message .= 'User First Name: ' . $current_user->user_firstname . "\n";
    $message .= 'User Last Name: ' . $current_user->user_lastname . "\n";
    $message .= "\n";
    $message .= "\n";
    $message .= "Billing Address:\n";

    // get the data from the profile after it's set by the customer save
    $woocommerce->customer->set_default_data();
    $message .= $woocommerce->customer->get_address() . "\n";
    $address_2 = $woocommerce->customer->get_address_2();
    if ($address_2 != '') {
        $message.= $address_2 . "\n";
    }
    $message .= $woocommerce->customer->get_city() . " ";
    $message .= $woocommerce->customer->get_state() . " ";
    $message .= $woocommerce->customer->get_postcode() . "\n";
    $message .= $woocommerce->customer->get_country() . "\n\n";
    $message .= "Shipping Address:\n";
    $message .= $woocommerce->customer->get_shipping_address() . "\n";
    $shipping_address_2 = $woocommerce->customer->get_shipping_address_2();
    if ($shipping_address_2 != '') {
        $message .= $shipping_address_2 . "\n";
    }
    $message .= $woocommerce->customer->get_shipping_city() . " ";
    $message .= $woocommerce->customer->get_shipping_state() . " ";
    $message .= $woocommerce->customer->get_shipping_postcode() . "\n";
    $message .= $woocommerce->customer->get_shipping_country();


    $headers = 'From: ' . $from_name . ' <' . $from_email . '>' . "\r\n";

    wp_mail($to, 'Customer Address Change Notice', $message, $headers);
}

代码的问题是,我收到通知,但地址不是他们的新地址,电子邮件中的地址是旧地址。 IE。落后了。

【问题讨论】:

    标签: jquery html css woocommerce


    【解决方案1】:

    您正在从会话中获取地址,而它仍在保存过程中。您可以尝试另一个钩子:

    do_action( 'woocommerce_after_save_address_validation', $user_id, $load_address, $address, $customer );
    

    所以你可以尝试像这样使用它:

    add_action( 'woocommerce_after_save_address_validation', 'send_address_notification', 30, 4);
    
    function send_address_notification($user_id, $load_address, $address, $customer){
    
    //... Use $address & $customer to build your message. ...
    
    }
    

    【讨论】:

    • 嘿感谢您的回复,我是编码新手,我复制了它仍然无法正常工作。你可以在我的原始代码中输入你的代码,所以我可以替换整个东西谢谢
    猜你喜欢
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 2018-02-17
    • 2014-04-30
    • 2020-09-10
    • 1970-01-01
    相关资源
    最近更新 更多