Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/*
Plugin Name: Product custom field to order details
Plugin URI: https://www.damiencarbery.com/
Description: Add a product custom field to the order details page - an experiment for a Facebook question.
Author: Damien Carbery
Version: 0.1
*/
/*
// Display the product custom fields on the single product page - to find one to show on the Edit Order page.
add_action( 'woocommerce_after_single_product_summary', 'dcwd_product_custom_fields', 12 );
function dcwd_product_custom_fields() {
global $product;
echo '<pre>', var_export( get_post_meta( $product->get_id() ), true ), '</pre>';
}*/
// This adds a line to the list that is after the Item data on the Edit Order page.
add_filter( 'woocommerce_order_item_get_formatted_meta_data', 'dcwd_get_formatted_meta_data', 10, 2 );
function dcwd_get_formatted_meta_data( $formatted_meta, $order_obj ) {
if ( 'WC_Order_Item_Product' == get_class( $order_obj ) ) {
//error_log( var_export( $formatted_meta, true ) );
//error_log( var_export( $order_obj, true ) );
$formatted_meta[] = (object) array(
'key' => 'unused',
'value' => 'unused',
'display_key' => 'Total Sales (' . $order_obj->get_type(). ')',
'display_value' => '<p><em>' . get_post_meta( $order_obj->get_product_id(), 'total_sales', true ) . '</em></p>',
);
}
return $formatted_meta;
}
// Adds text after the item.
add_action( 'woocommerce_after_order_itemmeta', 'dcwd_after_order_itemmeta', 10, 3 );
function dcwd_after_order_itemmeta( $item_id, $item, $product ) {
echo '<p>Added after itemmeta: Item ID: ', $item_id, '</p>';
}
// These were experiments that were not useful.
/*add_action( 'woocommerce_before_order_item_line_item_html', 'dcwd_before_order_item_line_item', 10, 3 );
function dcwd_before_order_item_line_item( $item_id, $item, $order ) {
echo '<p>Added before: Item ID: ', $item_id, '</p>';
}*/
/*add_action( 'woocommerce_order_item_line_item_html', 'dcwd_order_item_line_item', 10, 3 );
function dcwd_order_item_line_item( $item_id, $item, $order ) {
echo '<p>Added: Item ID: ', $item_id, '</p>';
}*/
/*add_action( 'woocommerce_admin_order_items_after_line_items', 'dcwd_order_items_after_line_items' );
function dcwd_order_items_after_line_items( $order_id ) {
echo '<p>Added: Order ID: ', $item_id, '</p>';
}*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment