Add a custom meta on products for Woocommerce

Image result for woocommerce item meta

img source : StackOverflow

 

This function will add a custom meta for items in WooCommerce :

//1. Add a cart meta
function jpc_add_custom_cart_meta( $item_data, $cart_item ) {
	if ( empty( $cart_item['output']['working_dir'] ) ) { //Check existing item meta on cart page, in this case, I want to pull an existing meta called "working_dir"
	   return $item_data;
	}
	$design_upload = $cart_item['output']['working_dir'];

	$item_data[] = array(
	   'key' => "Custom Design",
	   'value' => $design_upload,
	   'display' => '',
	);
	return $item_data;
}add_filter( 'woocommerce_get_item_data', 'jpc_add_custom_cart_meta', 10, 2 );

//2. Add order meta
function jpc_add_design_text_to_order_items( $item, $cart_item_key, $values, $order ) {
	if ( empty( $values['output']['working_dir']) ) {
	   return;
}
	$design_upload = $values['output']['working_dir'];
	$item->add_meta_data('Custom Design', $design_upload );
}add_action( 'woocommerce_checkout_create_order_line_item', 'jpc_add_design_text_to_order_items', 10, 4 );

John Perri Cruz

About the Author

John Perri Cruz was born in the Philippines, at the cold month of december. (Not cold enough cause Philippines is a tropical country.)

He's currently working as a Fullstack Web developer and when not sleeping, he can be found in coffee shop most of the time.

He gained his experience and skills by making and developing websites and systems for various individuals. He is well equipped with different Web programming languages and tools.

Connect :


Buy me a cup of coffee :

Leave a Reply