How to properly add custom product meta to order meta in woocommerce

I have the code to add some custom product meta data to cart and then later pass them to order meta. Everything works fine but it feels like the add to cart process is quite slow which makes me wonder if there is something wrong with my code. I will really appreciate if anyone can help me review the code to see if I am making any mistake.

Also, is there a better way to add custom product meta to order meta instead of first adding them to cart and then passing them to order meta.

My code to add the custom product meta to cart:

// Store the custom data fields to cart
add_filter( ‘woocommerce_add_cart_item_data’, ‘action_save_my_custom_product_fields’, 10, 2 );
function action_save_my_custom_product_fields( $cart_item_data, $product_id ) {
$bool = false;
$data = array();

$e_to_customer = get_post_meta($product_id, ‘e_to_customer’, true);
$the_customer_name = get_post_meta($product_id, ‘the_customer_name’, true);
$customer_phone_no = get_post_meta($product_id, ‘customer_phone_no’, true);
$customer_profile_url = get_post_meta($product_id, ‘customer_profile_url’, true);

$cart_item_data[‘custom_data’][‘e-to-customer’] = $e_to_customer;
$data[‘e-to-customer’] = $e_to_customer;
$bool = true;

$cart_item_data[‘custom_data’][‘the_customer_name’] = $the_customer_name;
$data[‘the_customer_name’] = $the_customer_name;

if( $customer_phone_no ) {
$cart_item_data[‘custom_data’][‘customer_phone_no’] = $customer_phone_no;
$data[‘customer_phone_no’] = $customer_phone_no;
}

if( $customer_profile_url ) {
$cart_item_data[‘custom_data’][‘customer_profile_url’] = $customer_profile_url;
$data[‘customer_profile_url’] = $customer_profile_url;
}

if( $bool ) {
// below statement make sure every add to cart action as unique line item
$unique_key = md5( microtime().rand() );
$cart_item_data[‘custom_data’][‘unique_key’] = $unique_key;
$data[‘unique_key’] = $unique_key;
WC()->session->set( ‘custom_data’, $data );
}
return $cart_item_data;
}

// save fields to order meta
add_action( ‘woocommerce_checkout_update_order_meta’, ‘lets_save_what_we_added’ );
function lets_save_what_we_added( $order_id ){
// We break the loop to get the custom data from 1 cart item
foreach(WC()->cart->get_cart() as $cart_item) break;
$data = $cart_item[‘custom_data’];

if( !empty( $_POST[‘quote_price_input’] ) )
update_post_meta( $order_id, ‘quote_price_input’, sanitize_text_field( $_POST[‘quote_price_input’] ) );

if ( isset( $_POST[‘select_price_option’] ) ) {
update_post_meta( $order_id, ‘select_price_option’, $_POST[‘select_price_option’] );
}

if( !empty( $_POST[‘message_to_customer’] ) )
update_post_meta( $order_id, ‘message_to_customer’, sanitize_textarea_field( $_POST[‘message_to_customer’] ) );

if( !empty( $_POST[‘sp_link’] ) )
update_post_meta( $order_id, ‘sp_link’, sanitize_text_field( $_POST[‘sp_link’] ) );

if( $data[‘customer_phone_no’] ) {
update_post_meta( $order_id, ‘customer_phone_no’, $data[‘customer_phone_no’] );
}

if( $data[‘customer_profile_url’] ) {
update_post_meta( $order_id, ‘customer_profile_url’, $data[‘customer_profile_url’] );
}

if( $data[‘e-to-customer’] ) {
update_post_meta( $order_id, ‘e-to-customer’, $data[‘e-to-customer’] );
}

if( $data[‘the_customer_name’] ) {
update_post_meta( $order_id, ‘the_customer_name’, $data[‘the_customer_name’] );
}
}

Read more here:: How to properly add custom product meta to order meta in woocommerce

Leave a Reply

Your email address will not be published. Required fields are marked *