Reply To: Food Addon Values to PHP?

#33752
Alex
Keymaster

Hi sarawsmn,

By design the addon data is not meant to be extractable from the order, but I prepared a code you can use to get the addons for a order by given order id. Please note that you have to create an array with your addon labels:

add_action( 'init', function () {
    $order_id = 5557;
    // Manually enter all your addon labels
	$addon_names_list = array(
		'Accessories',
		'Gift Wrapping',
	);
	/** @var WC_Order $order */
	$order = wc_get_order( $order_id );
	/** @var []WC_Order_Item $order_items */
	$order_items = $order->get_items();
	foreach ( $order_items as $order_item ) {
		/** @var []WC_Meta_Data $order_meta_data */
		$order_meta_data_array = $order_item->get_meta_data();
		foreach ( $order_meta_data_array as $item ) {
			foreach ( $addon_names_list as $addon ) {
				$meta_data = $item->get_data();
				if ( strpos( $meta_data['key'], $addon ) !== false ) {
					echo $meta_data['key'] . ': ' . $meta_data['value'];
				}
			}
		}
	}
} );

On the other hand, you should be able to use another plugin which provides such functionality, but you need to disable the Addons in Lafka from Theme Options -> Shop -> Product Addons

Regards,
Alex