在Woocommerce中发送自定义通知编辑订单页面重复问题 [英] Send custom notification In Woocommerce edit order pages repetition issue

查看:74
本文介绍了在Woocommerce中发送自定义通知编辑订单页面重复问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为woocommerce创建了用于订单跟踪的电子邮件通知程序插件。一切正常,当我保存跟踪号时,它会向客户发送电子邮件。

I created "email notifier for order tracking" plugin for woocommerce. All works fine and when I save tracking number, it sends an email to customer.

但是当我在订单页面中保存另一个字段时,它将再次发送电子邮件。我该如何解决?

But when I save another field in order page, it sends email again. How can I fix it?

add_action('add_meta_boxes', 'kargo_takip');
function kargo_takip() {
    add_meta_box('kargo_takip_meta_box', 'Kargo Takip', 'kargo_takip_meta_box_ekle', 'shop_order', 'side', 'high');
}

function kargo_takip_meta_box_ekle() {
    global $post;

    $meta_field_data = get_post_meta($post->ID, '_kargo_takip', true) ? get_post_meta($post->ID, '_kargo_takip', true) : '';

    echo '
        <input type="hidden" name="kargo_takip" value="' . wp_create_nonce() . '">
        <p style="border-bottom:solid 1px #eee;padding-bottom:13px;">
        <input type="text" style="width:250px;";" name="kargo_takibi" placeholder="' . $meta_field_data . '" value="' . $meta_field_data . '"></p>
        ';
}

我正在使用以下方式保存跟踪代码:

And i am saving track code with this:

add_action('save_post', 'kargo_takip_kaydet', 10, 1);

function kargo_takip_kaydet($post_id) {

    if (!isset($_POST['kargo_takip'])) {
        return $post_id;
    }
    $nonce = $_REQUEST['kargo_takip'];

    if (!wp_verify_nonce($nonce)) {
        return $post_id;
    }

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
    }

    if ('page' == $_POST['post_type']) {

        if (!current_user_can('edit_page', $post_id)) {
            return $post_id;
        }
    } else {

        if (!current_user_can('edit_post', $post_id)) {
            return $post_id;
        }
    }

    $kargo_takip_no = $_POST['kargo_takibi'];

    $order     = wc_get_order($post_id);
    $useremail = $order->get_billing_email();

    update_post_meta($post_id, '_kargo_takip', $kargo_takip_no);
    wp_mail($useremail, "Your order picked up", "Your track code: " . $kargo_takip_no);
}


推荐答案

更新2

为避免每次保存订单时重复发送电子邮件通知,您必须检查跟踪代码是否尚未保存在数据库中以及提交的跟踪代码跟踪代码字段不能为空。

To avoid repetitive email notifications each time you save the order, you will have to check that tracking code is not saved yet in database and that submitted tracking code field is not empty.

为了允许更改跟踪代码并通过电子邮件发送,我们会将保存的值与提交的值进行比较,以作为补充条件(OR),检查提交的值不为空。

To allow the tracking code to be changed and emailed, we will compare the saved value to the submitted value as an additional condition (OR) in the if statement, checking that the submitted value is not empty.

我已经完全重新查看了您的代码:

I have revisited completely your code:

add_action('add_meta_boxes', 'kargo_takip');
function kargo_takip() {
    add_meta_box('kargo_takip_meta_box', 'Kargo Takip', 'kargo_takip_meta_box_ekle', 'shop_order', 'side', 'high');
}

function kargo_takip_meta_box_ekle( $post ) {
    $kargo_takip = get_post_meta( $post->ID, '_kargo_takip', true ); // Get the saved field value
    $value       = $kargo_takip ? $kargo_takip : ''; // Set the field value
    $pholder     = $kargo_takip ? '' : __('Enter the tracking number'); // The field placeholder
    ?>
    <p style="border-bottom:solid 1px #eee; padding-bottom:13px;">
        <input type="text" style="width:100%;" name="kargo_takibi" placeholder="<?php echo $pholder; ?>" value="<?php echo $value; ?>">
        <input type="hidden"  name="kargo_takibi_nonce" value="<?php echo wp_create_nonce(); ?>">
    </p>
    <?php
}

// Only for shop order
add_action('save_post_shop_order', 'kargo_takip_kaydet', 50, 3 );
function kargo_takip_kaydet( $post_id, $post, $update ) {
    // If our field is empty we exit
    if ( empty( $_POST['kargo_takibi'] ) )  return $post_id;

    $post_kargo_takibi = sanitize_text_field( $_POST['kargo_takibi'] ); // Posted field value
    $meta_kargo_takip = get_post_meta( $post_id, '_kargo_takip', true ); // Saved field value

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST[ 'kargo_takibi_nonce' ] ) ) return $post_id;

    // Checking that is not an autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return $post_id;

    // Check the user’s permissions (for 'shop_manager' and 'administrator' user roles)
    if ( ! current_user_can( 'edit_shop_order', $post_id ) ) return $post_id;

    // If tracking code is not saved yet in database and tracking code field is not empty
    if ( ( $meta_kargo_takip != $post_kargo_takibi && $meta_kargo_takip && $post_kargo_takibi ) ||
       ( ! $meta_kargo_takip && $post_kargo_takibi ) )
    {
        // Customer billing email address
        $email = get_post_meta( $post_id, '_billing_email', true );
        // Send email
        wp_mail( $email, "Your order picked up", "Your track code: " . $post_kargo_takibi );
        // Save the Cargo tracking code data in database for this order
        update_post_meta( $post_id, '_kargo_takip', $post_kargo_takibi );
    }
}

代码进入function.php文件

代码已经过测试,可以在所有可能的情况下工作,避免重复发送电子邮件通知。

Code is tested and works in all possible cases avoiding repetitive email notifications.

这篇关于在Woocommerce中发送自定义通知编辑订单页面重复问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆