Home › Forums › Backend Issues (wp-admin) › Extracting ACF fields in custom email
On order completion, I set up and send an email to a supplier.
My course of action for this now is as follows:
public function create_email($order_id) {
$order = wc_get_order($order_id);
$items = $order->get_items();
foreach ($items as $item_key => item) {
$product_id = $item->get_product_id();
$supplier = get_field("supplier", $product_id);
}
}
Notice: In the code example I do not use $supplier. However in my email I added to the body using $body .= ‘<p>’ . $supplier . ‘</p>’.
$supplier is always empty and I can’t seem to access the value.
foreach ($items as $item_key => item) {
Should be:
foreach ($items as $item_key => $item) {
Sorry – this was a typo in the code example. In my running code it is $item.
Can you supply more code pertaining to where $supplier is used?
Are you adding this value to the email inside of the loop or outside of the loop?
By looking at what you have, if you have multiple items in the order $supplier will only be contain the value of the last item in this list.
@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);
}
}
Sorry, I don’t see anything in this code that should not be working.
What does appear in the email being sent?
Okay – I’ve now found the issue.
When adding a new field the first value is visible in the select on the product page in the control panel. However, this field does not contain the value. I, therefore, had to go into each and every product and select another value and then back to the value I wanted for the change to kick in.
It works now. Thank you.
You must be logged in to reply to this topic.
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 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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.