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')) {
                $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');