Add an automated purchase note from ACF

Editing HTML in purchase notes in WooCommerce can be a cumbersome process, especially if it’s just a part of it, like a download link. I couldn’t find a filter for displaying the purchase note for a product on the front-end, but this will automatically change it before an order is placed:

<?php

/**
 * Add automated notes to orders
 */
function add_custom_note($order_id, $order)
{
  $items = $order->get_items();

  if (empty($items)) return;

  // Change to your custom field or option
  $field = get_field('field', 'option');

  if (!$field) return;

  foreach ($items as $item) {
    $product = $item->get_product();

    // Change to your product SKU
    if ('SKU' === $product->get_sku()) {
      $note = sprintf('Note: %s', $field);

      if ($note !== $product->get_purchase_note()) {
        $product->set_purchase_note($note);

        $product->save();
      }
    }
  }
}
add_action('woocommerce_new_order', 'add_custom_note', 20, 2);

Leave a Reply

Your email address will not be published. Required fields are marked *