Dynamically added custom fields not displayed on WooCommerce email notifications

I have a different kind of scenario then the typical custom fields (I suppose). I am not getting custom values (fields) from user in the form rather I have an implementation which adds:

  • ColorName
  • Size
  • City

These are from a custom product flow which adds custom attributes to the cart, here is how I am doing that:

add_action('wp_ajax_wdm_add_user_custom_data_options', 'wdm_add_user_custom_data_options_callback');
add_action('wp_ajax_nopriv_wdm_add_user_custom_data_options', 'wdm_add_user_custom_data_options_callback');


function wdm_add_user_custom_data_options_callback()
{
    // print_r($_POST);
    $productIDM = $_POST['product_id'];
    // case swith
    switch ($productIDM) {
        case Ace Dura Silk:
            $productID = 3254;
            break;
        case Ace Matt Finish:
            $productID = 3232;
            break;
        case Ace Plastic Emulsion:
            $productID = 3276;
            break;
        case Ace Weather Defender:
            $productID = 2991;
            break;
        case Overall Plasticoat:
            $productID = 3112;
            break;
      }

    $colorname = $_POST['colorname'];
    $cityname = $_POST['cityname'];
    $size = $_POST['size'];
    $price = $_POST['price'];
    global $woocommerce;
    $woocommerce-cart-add_to_cart( $productID, 1 );
    //   die();
    // echo 'I am in...';
    $result = array(
        'status' = true
    );
    echo json_encode($result);
}
add_filter('woocommerce_add_cart_item_data','wdm_add_item_data',1,10);
function wdm_add_item_data($cart_item_data, $product_id) {

    global $woocommerce;
    $new_value = array();
    $new_value['_custom_options'] = $_POST['custom_options'];

    if(empty($cart_item_data)) {
        return $new_value;
    } else {
        return array_merge($cart_item_data, $new_value);
    }
}

add_filter('woocommerce_get_cart_item_from_session', 'wdm_get_cart_items_from_session', 1, 3 );
function wdm_get_cart_items_from_session($item,$values,$key) {

    if (array_key_exists( '_custom_options', $values ) ) {
        $item['_custom_options'] = $values['_custom_options'];
    }

    return $item;
}
add_filter('woocommerce_cart_item_name','add_usr_custom_session',1,3);
function add_usr_custom_session($product_name, $values, $cart_item_key ) {

    $return_string = $product_name . div class='cart_subitems_custom'br /small.$values['_custom_options']['colorname']./smallbr /small.$values['_custom_options']['cityname']./smallbr /small.$values['_custom_options']['size']./small/div ; //. br / . print_r($values['_custom_options']);
    return $return_string;

}
add_action('woocommerce_add_order_item_meta','wdm_add_values_to_order_item_meta',1,2);
function wdm_add_values_to_order_item_meta($item_id, $values) {
    global $woocommerce,$wpdb;

    wc_add_order_item_meta($item_id,'_colorname',$values['_custom_options']['colorname']);
    wc_add_order_item_meta($item_id,'_cityname',$values['_custom_options']['cityname']);
    wc_add_order_item_meta($item_id,'_size',$values['_custom_options']['size']);

}
add_action( 'woocommerce_before_calculate_totals', 'update_custom_price', 1, 1 );
function update_custom_price( $cart_object ) {
    foreach ( $cart_object-cart_contents as $cart_item_key = $value ) {       
        // Version 2.x
        //$value['data']-price = $value['_custom_options']['custom_price'];
        // Version 3.x / 4.x
         if($value['_custom_options']['price'] == null){
            echo;
        }else{
            $value['data']-set_price($value['_custom_options']['price']);
        }
    }
}

I am getting these custom values almost everywhere except Email Notification.

Here is what normal product order edit shows:

Here is how I am getting the custom product in order edit page:

I have tried all the solution I can possibly find (filter action hooks) but nothing works for me.

I have tried first answer from this:

add_action( 'woocommerce_checkout_create_order_line_item', 'custom_checkout_create_order_line_item', 20, 4 );
function custom_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
    if( isset( $values['colorname'] ) )
        $item-add_meta_data( __('DCM Shade'), $values['_colorname'] );
}

Also the common method I found everywhere:

function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
    // Get meta
    $color = $order-get_meta( 'colorname', true );
    
    // NOT empty
    if( ! empty( $color ) ) {  
        $fields['colorname'] = array(
            'label' = __( 'Shade' ),
            'value' = $color,
        );
    }
    
    // Get (other) meta
    $shipping_email = $order-get_meta( '_cityname', true );
    
    // NOT empty
    if ( ! empty( $shipping_email ) ) { 
        $fields['_cityname'] = array(
            'label' = __( 'City' ),
            'value' = $shipping_email,
        );
    }
    
    return $fields;
}
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );

But I can't get the custom fields.

What am I doing wrong can please anyone please guide me.

Topic woocommerce-offtopic php custom-field Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.