Home › Forums › Backend Issues (wp-admin) › Extracting ACF fields in custom email › Reply To: Extracting ACF fields in custom email
@hube2 Thank you for your answer. I will try to supply with more context.
The idea is as follows: When a order is completed I use the woocommerce_thankyou
action in WooCommerce to create an email as follows:
add_filter('woocommerce_thankyou', [$this, 'create_email'], 10, 1);
In my custom fields setup, I’ve created a field group that applies to all products. In the field group I can set a supplier for the product from predefined options in a single select.
What I want is to access that supplier on a product level in the create_email
function that runs after an order is completed. My approach is as follows:
public function create_email($order_id)
{
global $product;
$order = wc_get_order($order_id);
$items = $order->get_items();
$subject = 'NEW ORDER #' . $order->get_order_number();
$header = 'New order: #' . $order->get_order_number();
foreach ($items as $item_key => $item) {
$product = $item->get_product();
if (strpos($product->get_slug(), 'vareproeve')) {
continue;
}
$product_id = $item->get_product_id();
$main .= '<p>ID: ' . $product_id . '</p>';
if ($supplier = get_field("supplier", $item->get_product_id())) {
$main .= '<p><strong>' . __('Supplier: ') . ': </strong>' . $supplier . '</p>';
}
}
// Setup rest of email
if ($main != '') {
$body = $head . $main . $foot;
$this::send_email($recipient, $subject, $header, $body, $headers, $attachments, $order, $order_id);
update_post_meta($order_id, '_order_mail_sent', 'yes');
unset($head);
unset($main);
unset($foot);
}
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We’re hard at work on ACF 6.1, and Beta 1 is now available 🚀
— Advanced Custom Fields (@wp_acf) March 16, 2023
This release includes custom post type and taxonomy registration, an improved experience when selecting field types, PHP 8.1 and 8.2 compatibility, and more!
Let’s take a look 🧵https://t.co/Y0WcAT11l4
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.