Send custom notification In Woocommerce edit order pages repetition issue

自闭症网瘾萝莉.ら 提交于 2019-12-07 11:36:25

Update 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.

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

Code goes in function.php file of the active child theme (or active theme).

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!