在Woocommerce管理员电子邮件通知中显示产品自定义字段值

哈里

我的function.php中有此代码,如何在管理邮件订单中显示此字段值?谢谢!

//1.1 Display field in admin
add_action('woocommerce_product_options_inventory_product_data', 'woocommerce_product_custom_fields');
function woocommerce_product_custom_fields(){
    global $woocommerce, $post;
    echo '<div class="product_custom_field">';
    woocommerce_wp_text_input(
        array(
            'id' => '_custom_product_text_field',
            'placeholder' => 'Name',
            'label' => __('customtext', 'woocommerce'),
            'desc_tip' => 'true'
        )
    );    
    echo '</div>';
}

//1.2 Save field
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields_save($post_id){
    $woocommerce_custom_product_text_field = $_POST['_custom_product_text_field'];
    if (!empty($woocommerce_custom_product_text_field))
        update_post_meta($post_id, '_custom_product_text_field', esc_attr($woocommerce_custom_product_text_field));

}
LoicTheAztec

更新2有两种非常相似的替代方法(第一种更好):

要在电子邮件(以及订单)中轻松显示此产品自定义字段值,您可以使用以下代码,一旦下订单,该代码会将这些数据保存在订单项中。

第一个备用代码(使用WC 3+最新CRUD方法的最佳新代码

// 1.3 Save custom field value in order items meta data (only for Woocommerce versions 3+)
add_action( 'woocommerce_checkout_create_order_line_item', 'add_custom_field_to_order_item_meta', 20, 4 );
function add_custom_field_to_order_item_meta( $item, $cart_item_key, $values, $order ) {
    $custom_field_value = get_post_meta( $item->get_product_id(), '_custom_product_text_field', true );
    if ( ! empty($custom_field_value) ){
            $item->update_meta_data( __('Custom label', 'woocommerce'), $custom_field_value );
    }
}

… 要么 …

另一种选择(旧的更新)

// 1.3 Save custom field value in order items meta data
add_action( 'woocommerce_add_order_item_meta', 'add_custom_field_to_order_item_meta', 20, 3 );
function add_custom_field_to_order_item_meta( $item_id, $values, $cart_item_key ) {

    $custom_field_value = get_post_meta( $values['data']->get_id(), '_custom_product_text_field', true );
    if ( ! empty($custom_field_value) )
        wc_add_order_item_meta( $item_id, __('Custom label', 'woocommerce'), $custom_field_value );
}

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

在订单上,您将获得(两者):

在此处输入图片说明

在电子邮件中,您会得到的(两者):

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在WooCommerce“订单已完成”电子邮件中添加自定义产品字段

仅对WooCommerce管理员电子邮件通知显示自定义字段

如何在新订单电子邮件中显示WooCommerce自定义产品元?

在WooCommerce管理员订单页面上将产品自定义字段显示为订单项元

Woocommerce中管理员编辑产品的复选框自定义字段

在WooCommerce电子邮件订购项目中显示可变产品的产品自定义字段

根据Woocommerce电子邮件通知中的送货方式显示自定义内容

在Woocommerce订单和电子邮件中显示产品变体的自定义字段

在Woocommerce管理员电子邮件通知中显示购买记录

在Woocommerce中以BCC身份发送给管理员的新帐户电子邮件通知

在Woocommerce管理员电子邮件通知中显示产品ACF值

在Woocommerce管理员订单列表自定义列中显示结帐字段值

在Woocommerce管理员,订单和电子邮件中显示自定义付款字段

在WooCommerce订单和电子邮件通知中显示自定义字段值

在电子邮件通知上显示WooCommerce自定义结帐字段值

使Woocommerce(3.8.0)管理员电子邮件中包含我在结帐页面上的自定义字段数据

电子邮件,管理员订单和“谢谢”页面上的WooCommerce 4.0自定义结帐和ACF字段值

订单自定义字段未显示在WooCommerce电子邮件通知中

在特定的WooCommerce电子邮件通知上显示自定义字段

在WooCommerce电子邮件通知上显示第二个自定义字段

在WooCommerce电子邮件通知上显示自定义产品图片

在WooCommerce电子邮件通知中为管理员显示自定义字段

在woocommerce订单电子邮件中显示多个产品的自定义字段

在订单电子邮件通知中输出产品自定义字段值

获取要在 Woocommerce 电子邮件通知中显示的自定义字段值

使用变量从奏鸣曲管理员发送自定义电子邮件

在woocommerce电子邮件中输出产品自定义字段(图像)

发送给管理员的 WooCommerce 电子邮件通知中的自定义格式地址部分

在 WooCommerce 的管理员新订单电子邮件通知中显示订单下载