Display custom fields values in WooCommerce order and email notification

前端 未结 1 712
陌清茗
陌清茗 2021-01-13 19:35

Based on \"Choosing a date and time after choosing the WooCommerce delivery method\" answer code, that displays custom Pickup fields and delivery dates, the following code d

相关标签:
1条回答
  • 2021-01-13 19:54

    To display the custom fields values in backend order edit pages (if they are saved in database for the order), use the following:

    // View fields in Edit Order Page
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_custom_fields_value_admin_order', 10, 1 );
    function display_custom_fields_value_admin_order( $order ){
        // Display the delivery option
        if( $delivery_option =  $order->get_meta('_delivery_option') )
            echo '<p><strong>'.__('Delivery type').':</strong> ' . $delivery_option . '</p>';
    
        // Display the delivery date
        if( $delivery_datetime = $order->get_meta('_delivery_datetime') )
            echo '<p><strong>'.__('Delivery Date').':</strong> ' . $delivery_datetime . '</p>';
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.


    The best shorter clean way to display the custom field values everywhere on frontend order pages and on email notifications is to display them in order totals table, just like used payment methods:

    // Display the chosen delivery information
    add_filter( 'woocommerce_get_order_item_totals', 'chosen_delivery_item_order_totals', 10, 3 );
    function chosen_delivery_item_order_totals( $total_rows, $order, $tax_display ) {;
        $new_total_rows = [];
    
        // Loop through Order total lines
        foreach($total_rows as $key => $total ){
            // Get the chosen delivery values
            $delivery_option  = $order->get_meta('_delivery_option');
            $delivery_datetime = $order->get_meta('_delivery_datetime');
    
            // Display delivery information before payment method
            if( ! empty($delivery_option) && 'payment_method' === $key ){
                $label  = empty($delivery_datetime) ? __('Delivery') : __('Delivery Date');
                $value  = empty($delivery_datetime) ? __('AZAP', $domain) : $delivery_datetime;
    
                // Display 'Delivery method' line
                $new_total_rows['chosen_delivery'] = array( 'label' => $label,'value' => $value );
            }
            $new_total_rows[$key] = $total;
        }
    
        return $new_total_rows;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.


    Related thread: Choosing a date and time after choosing the WooCommerce delivery method

    0 讨论(0)
提交回复
热议问题