在Woocommerce管理员电子邮件通知中显示产品ACF值 [英] Display product ACF value in Woocommerce admin email notifications

查看:116
本文介绍了在Woocommerce管理员电子邮件通知中显示产品ACF值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在woocommerce产品中获取高级自定义字段,以传递给管理员的新订单电子邮件。它仅供管理员参考,并且仅针对每种产品。我已经尝试过了,这可以将其发送到后端,而不是通过电子邮件发送。

Trying to get an advanced custom field in a woocommerce product to pass to the new order email for the admin. It's only there for the admin reference and is specific to each product. I have tried and this gets it to the backend but not in the email.

add_action( 'woocommerce_before_order_itemmeta', 'product_size', 10, 3 );
function product_size( $item_id, $item, $product ){
    // Only in backend Edit single Order pages
    if( current_user_can('edit_shop_orders') ):

    // The product ID 
    $product_id = $product->get_id();

    // Get your ACF product value 
    $acf_value = __('Size: ') . get_field( 'package_size', $product_id );

    // Outputing the value of the "package size" for this product item
    echo '<div class="wc-order-item-custom"><strong>'. $acf_value .'</strong></div>';

    endif;
}

我尝试使用它来获取电子邮件,但是却取消了订购过程。它通过了后端,但是在下了订单之后,它只是刷新了结帐页面,而没有去过谢谢或生成电子邮件。

I tried using this to get to the email but it killed the order process. It went through in the backend but after hitting place order, it just refreshes the checkout page and does not go to the thank you or generate an email.

add_action( 'woocommerce_email_order_details', 'display', 10, 4 );
function display( $order, $sent_to_admin, $plain_text, $email ) {
global $product;
$id = $product->get_id();
    $value = get_field( "package_size", $id );

    if($value)  {
        echo "<p>Package Size : ".$value ."</p>";
    }

}

任何建议或帮助都值得赞赏。

Any suggestions or help is appreciated.

推荐答案

WC_Product 对象 $ product 不能定义为全局变量。您需要使用foreach循环才能首先获取订单商品。

The WC_Product object $product can't be defined as a global variable. You need to use a foreach loop to get order items first.

但是由于订单中可以包含许多项目(产品),因此您可能会在此ACF字段中看到很多显示。

But as an order can have many items (products) you may get many displays for this ACF field.

您重新访问的代码:

add_action( 'woocommerce_email_order_details', 'display_package_size_email_order_details', 10, 4 );
function display_package_size_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
    // Only admin notifications
    if( ! $sent_to_admin )
         return; // Exit

    foreach( $order->get_items() as $item ) {
        if( $package_size = get_field( "package_size", $item->get_product_id() ) ){
            echo '<p><strong>'.__('Package Size').': </strong>'.$package_size.'</p>';
        }
    }
}

代码进入function.php活动子主题(或活动主题)的文件。经过测试并可以工作。

Code goes in function.php file of your active child theme (or active theme). Tested and works.

相关:从Woocommerce订单电子邮件中的ACF字段显示值

这篇关于在Woocommerce管理员电子邮件通知中显示产品ACF值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆