In this doc I'll show how you can add your own custom data to a Conversio receipt. The data can be added specifically to products, in this example I'll add the weight of the product to the receipt.

Requirements

To implement custom data to your receipt a bit of coding skills are required. The code snippets are mostly done for you, the only thing that needs to be changed is the data that is added to your receipt.

Below are two examples of how data can be retrieved. One way is by getting the data from the product, the other method is getting the data from the order items table.

1. Adding custom product data

The following code snippet shows exactly how you can add add the product weight to your receipt. At each step of the way I've added comments so you can see exactly what is happening at what moment.

// Add the filter for the order item data
add_filter( 'receiptful_api_args_items', 'conversio_add_custom_item_meta', 10, 2 ); 

/**
 * Add custom data to the item meta.
 */
function conversio_add_custom_item_meta( $items, $order ) {

// Loop through each item in the receipt
foreach ( $items as $key => $values ) {

// Get the product - 'reference' is the product ID
if ( ! $product = wc_get_product( $values['reference'] ) ) {
continue;
}

// Getting the data from the product + Check if the data is not empty
if ( ! $data = $product->get_weight() ) {
continue;
}

// Adding the data to the item meta
$items[ $key ]['metas'][] = array(
'key' => 'Weight',
'value' => $data,
); 

}

return $items; 

}

2. Adding custom data from the order item table

A common way for plugins is to save data as custom order item meta in the database. The following code snippet shows how you can retrieve that data. 

This works a bit differently because we need the 'order item id' specifically to get this data. 

// Add the filter for the order item data
add_filter( 'receiptful_api_args_items', 'conversio_add_custom_item_meta_2', 11, 2 ); 

/**
 * Add custom data to the item meta.
 */
function conversio_add_custom_item_meta_2( $items, $order ) { 

$new_items = array();
$order = wc_get_order( $order ); 

// Loop through each item in the receipt
foreach ( $order->get_items() as $item_id => $item ) { 

// Set the original data
$item_data = array();
foreach ( $items as $item_values ) {
if ( $item_values['reference'] == $item['product_id'] ) {
$item_data = $item_values;
break;
}
} 

// Getting the data from the order item + Check if the data is not empty
// For this example the product ID is gotten from the item meta table as this should always exist, making a good example.
if ( ! $data = wc_get_order_item_meta( $item_id, '_product_id', true ) ) {
continue;
} 

// Adding the data to the item meta
$item_data['metas'][] = array(
'key' => 'Product ID',
'value' => $data,
); 

// Setting the new item data to be returned
$new_items[] = $item_data; 

} 

return $new_items; 

} 

Thats how you can easily add custom data to your Conversio receipts. 

You can modify the code snippets to fit your exact needs and get the data accordingly.

Did this answer your question?