phpwordpresswoocommercemetadatahook-woocommerce

Add some text to specific WooCommerce email notifications based on order metadata


For WooCommerce Orders, I have added a custom field (custom metadata) called order_approved, when the new order is created. Now I want to add a custom text on the email notification based on that metadata. But, I could not see this custom meta_data on the order object.

add_action('woocommerce_thankyou', 'assign_approver_to_order');
function assign_approver_to_order($order_id){
    $user_id = get_current_user_id();
    $company_id = get_user_meta($user_id, 'parent', true);

    if($company_id){
        update_post_meta($order_id, 'approval_required', 'yes');
    }   
}


add_action( 'woocommerce_email_order_details', 'custom_text_in_customer_new_order_email', 999, 4 );
function custom_text_in_customer_new_order_email( $order, $sent_to_admin, $plain_text, $email ) {
    $order_approval = get_post_meta($order->id, 'approval_required', true);
    echo "order approval: ".$order_approval ."<br>"; // I am not getting any value

    var_dump($order)// also could not see the 'approval_required' order meta on the order object

    if( $order_approval == "yes" ) {
        switch($email->id){
            case "new_order":
                $custom_txt = "<b>This order is waiting for approval.</b><br>";
                echo  $custom_txt;
                break;
            case "customer_on_hold_order":
                $custom_txt = "<b>Your order is waiting for approval.</b><br>";
                echo  $custom_txt;
                break;
        }
    }
}

Solution

  • There are some mistakes in your code, and you are not using the correct hook for your first function… Since WooCommerce 3, you should not use anymore WordPress post meta functions, instead you should use getter and setter methods on CRUD Objects like WC_Order Objects. This is mandatory if High Performance Orders Storage is enabled, as WooCommerce is progressively migrating data to custom tables.

    Assuming that "parent" user metadata exists, try the following simplified code instead:

    add_action('woocommerce_checkout_order_created', 'assign_approver_to_order');
    function assign_approver_to_order($order){
        if( get_user_meta($order->get_user_id(), 'parent', true) ){
            $order->update_meta_data('approval_required', 'yes');
            $order->save();
        }   
    }
    
    
    add_action( 'woocommerce_email_order_details', 'custom_text_in_customer_new_order_email', 5, 4 );
    function custom_text_in_customer_new_order_email( $order, $sent_to_admin, $plain_text, $email ) {
        if( $order->get_meta('approval_required') === 'yes' ) {
            switch($email->id){
                case 'new_order':
                    echo '<b>'.__('This order is waiting for approval.', 'woocommerce').'</b><br>';
                    break;
                case 'customer_on_hold_order':
                    echo '<b>'.__('Your order is waiting for approval.', 'woocommerce').'</b><br>';
                    break;
            }
        }
    }
    

    Code goes on functions.php file of your child theme (or in a plugin). It should work.