Anyone looking for a way to add memos or notes to certain products on the backend
I was looking for a way to add memos and private notes against certain products on woocommerce, although this really should just be part of the core functionality, I didn't really want to install yet another plugin to do this, so i got claude to write a small PHP Snippet, which works perfectly for my needs, just thought i'd share it incase anyone else was looking for the same thing.
---------------------------------------------------------------------------------------
<?php
/**
* Add Private Admin Memo to WooCommerce Products
* Add this to your theme's functions.php or a custom plugin
*/
// Add custom meta box to product edit page
add_action('add_meta_boxes', 'wc_add_admin_memo_metabox');
function wc_add_admin_memo_metabox() {
add_meta_box(
'wc_admin_memo', // Unique ID
'Private Admin Memo', // Box title
'wc_admin_memo_metabox_callback', // Content callback
'product', // Post type
'side', // Context (side, normal, advanced)
'high' // Priority
);
}
// Display the meta box content
function wc_admin_memo_metabox_callback($post) {
// Add nonce for security
wp_nonce_field('wc_admin_memo_nonce_action', 'wc_admin_memo_nonce');
// Get existing memo value
$memo_value = get_post_meta($post->ID, '_wc_admin_memo', true);
// Display textarea
?>
<div style="margin-top: 10px;">
<textarea
name="wc_admin_memo"
id="wc_admin_memo"
rows="8"
style="width: 100%; resize: vertical;"
placeholder="Add private notes for admins only..."
><?php echo esc_textarea($memo_value); ?></textarea>
<p style="margin-top: 8px; color: #666; font-size: 12px;">
<em>This memo is only visible to administrators and will not appear on the frontend.</em>
</p>
</div>
<?php
}
// Save the meta box data
add_action('save_post_product', 'wc_save_admin_memo_metabox');
function wc_save_admin_memo_metabox($post_id) {
// Check if nonce is set
if (!isset($_POST['wc_admin_memo_nonce'])) {
return;
}
// Verify nonce
if (!wp_verify_nonce($_POST['wc_admin_memo_nonce'], 'wc_admin_memo_nonce_action')) {
return;
}
// Check if this is an autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
// Check user permissions
if (!current_user_can('edit_post', $post_id)) {
return;
}
// Check if user is an administrator
if (!current_user_can('manage_options')) {
return;
}
// Sanitize and save the data
if (isset($_POST['wc_admin_memo'])) {
$memo_data = sanitize_textarea_field($_POST['wc_admin_memo']);
update_post_meta($post_id, '_wc_admin_memo', $memo_data);
} else {
// Delete meta if empty
delete_post_meta($post_id, '_wc_admin_memo');
}
}
// Optional: Add a column to the products list table to show if a memo exists
add_filter('manage_product_posts_columns', 'wc_add_memo_column');
function wc_add_memo_column($columns) {
// Insert after the title column
$new_columns = array();
foreach ($columns as $key => $value) {
$new_columns[$key] = $value;
if ($key === 'name') {
$new_columns['admin_memo'] = '<span class="dashicons dashicons-clipboard" title="Admin Memo"></span>';
}
}
return $new_columns;
}
// Populate the custom column
add_action('manage_product_posts_custom_column', 'wc_populate_memo_column', 10, 2);
function wc_populate_memo_column($column, $post_id) {
if ($column === 'admin_memo') {
$memo = get_post_meta($post_id, '_wc_admin_memo', true);
if (!empty($memo)) {
echo '<span class="dashicons dashicons-yes" style="color: #46b450;" title="Has memo"></span>';
} else {
echo '<span class="dashicons dashicons-minus" style="color: #ddd;"></span>';
}
}
}
// Make the column sortable (optional)
add_filter('manage_edit-product_sortable_columns', 'wc_memo_column_sortable');
function wc_memo_column_sortable($columns) {
$columns['admin_memo'] = 'admin_memo';
return $columns;
}
---------------------------------------------------------------------------------------