简体   繁体   English

在 WooCommerce 'Order Completed' 电子邮件中添加自定义产品字段

[英]Add Custom Product Field in WooCommerce 'Order Completed' Emails

Is there any way I can add content from a custom product field to 'order completed' WooCommerce emails?有什么方法可以将自定义产品字段中的内容添加到“订单完成”WooCommerce 电子邮件中?

I have added this code to my functions.php in order to save my custom field to the product meta information.我已将此代码添加到我的函数中。php 以便将我的自定义字段保存到产品元信息中。

//1.1 Display Activation Instructions field in admin
add_action('woocommerce_product_options_inventory_product_data', 'woocommerce_product_act_fields');
function woocommerce_product_act_fields(){
    global $woocommerce, $post;
    echo '<div class="product_custom_field">';
    woocommerce_wp_text_input(
        array(
            'id' => '_act',
            'label' => __('Activation Instructions', 'woocommerce'),
            'desc_tip' => 'true',
            'description' => 'Enter the Activation Instructions Link.'
        )
    );    
    echo '</div>';
}

//1.2 Save Activation Instructions field in product meta
add_action('woocommerce_process_product_meta', 'woocommerce_product_act_field_save');
function woocommerce_product_act_field_save($post_id){
    $woocommerce_act_product_text_field = $_POST['_act'];
    if (!empty($woocommerce_act_product_text_field))
        update_post_meta($post_id, '_act', esc_attr($woocommerce_act_product_text_field));
}

I have also added this code to display a custom text on my 'order completed' woocomerce emails.我还添加了此代码以在我的“订单完成”woocomerce 电子邮件中显示自定义文本。

add_action( 'woocommerce_email_customer_details', 'bbloomer_add_content_specific_email', 20, 4 );
function bbloomer_add_content_specific_email( $order, $sent_to_admin, $plain_text, $email ) {
   if ( $email->id == 'customer_completed_order' ) {
      echo '<h2>Activation & Download Instructions</h2><p style="margin-bottom:35px;">Please refer to 
      our <a href="https://example.com/activation-guides/">Activation Guides</a> for detailed 
      activation and download instructions.</p>';
   }
}

The above code works well and it displays the custom text for all 'order completed' emails.上面的代码运行良好,它显示了所有“订单完成”电子邮件的自定义文本。

But, I want to replace the URL part of the text with the custom product field data.但是,我想用自定义产品字段数据替换文本的 URL 部分。 The data is different for every product.每个产品的数据都不同。

Thanks.谢谢。

Use: get_post_meta to retrieves a post meta field for the given post ID.使用: get_post_meta检索给定帖子 ID 的帖子元字段。

//1.1 Display Activation Instructions field in admin
function woocommerce_product_act_fields() {
    echo '<div class="product_custom_field">';

    woocommerce_wp_text_input(
        array(
            'id' => '_act',
            'label' => __('Activation Instructions', 'woocommerce'),
            'desc_tip' => 'true',
            'description' => 'Enter the Activation Instructions Link.'
        )
    );

    echo '</div>';
}
add_action('woocommerce_product_options_inventory_product_data', 'woocommerce_product_act_fields', 10, 0 );

//1.2 Save Activation Instructions field in product meta
function woocommerce_product_act_field_save( $product ) {
    if( isset($_POST['_act']) ) {
        $product->update_meta_data( '_act', sanitize_text_field( $_POST['_act'] ) );
    }
}
add_action( 'woocommerce_admin_process_product_object', 'woocommerce_product_act_field_save', 10, 1 );

//2 Add url to email
function woocommerce_product_act_email( $order, $sent_to_admin, $plain_text, $email ) {
    if ( $email->id == 'customer_completed_order' ) {
        $items = $order->get_items();

        echo '<h2>Activation & Download Instructions</h2>';

        foreach ( $items as $item ) {
            // Get product ID
            $product_id = $item->get_product_id();

            // Get product name
            $product_name = $item->get_name();

            // Get post meta
            $url = get_post_meta( $product_id, '_act', true);

            if ( $url ) {
                echo '<p style="margin-bottom:35px;">For ' . $product_name . ' follow <a href="' . $url. '"> these instructions</a></p>';
            }
        }
    }
}
add_action( 'woocommerce_email_customer_details', 'woocommerce_product_act_email', 20, 4 );
add_action( 'woocommerce_email_customer_details', 'bbloomer_add_content_specific_email', 20, 4 );

function bbloomer_add_content_specific_email( $order, $sent_to_admin, $plain_text, $email ) {
    if ( $email->id == 'customer_completed_order' ) {

        $items = $order->get_items();

        foreach ( $items as $item ) {
            // Get product ID
            $product_id = $item->get_product_id();

            // Get post meta
            $url = get_post_meta( $product_id, '_act', true );

            if ( $url ) {
                echo '<h2>'.$item->get_name().'Activation & Download Instructions</h2><p style="margin-bottom:35px;">Please refer to our <a href="' . $url . '">Activation Guides</a> for detailed activation and download instructions.</p>';
            }
        }
    }
}

Try this code试试这个代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将产品自定义字段添加到 WooCommerce 已完成的订单电子邮件 - Adding Product Custom Fields to WooCommerce Completed Order Emails 将可变产品的“描述”字段内容添加到 woocommerce “已完成订单” email - Add "description" field contents for variable product to woocommerce "Completed order" email 将产品图像(而非缩略图)添加到WooCommerce订单电子邮件中 - Add Product Images (Not Thumbnails) to WooCommerce Order Emails 在woocommerce电子邮件中输出产品自定义字段(图像) - Output product custom field (image) in woocommerce emails 在woocommerce管理订单页面中添加产品属性或自定义字段 - Add product attribute or custom field in woocommerce admin order page 将自定义增值税字段添加到 woocommerce 电子邮件 - Add custom vat field to woocommerce emails Woocommerce - 自动将特定产品添加到新客户的已完成订单中 - Woocommerce - Automatically add specific product to completed order from a new customer 如何在新订单电子邮件中显示 WooCommerce 自定义产品元? - How to show WooCommerce custom product meta in new order emails? 在 Woocommerce 订单和电子邮件中显示产品变体的自定义字段 - Displaying custom field of product variations within Woocommerce orders and emails 订单完成后获取自定义字段值 woocommerce 挂钩 - Getting custom field value after order completed woocommerce hook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM