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

Mayank Chawla:

有什么方法可以将自定义产品字段中的内容添加到“已完成订单”的WooCommerce电子邮件中?

我已将此代码添加到我的functions.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));
}

我还添加了此代码,以在“订单完成” 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>';
   }
}

上面的代码运行良好,并且显示了所有“已完成订单”电子邮件的自定义文本。

但是,我想用自定义产品字段数据替换文本的URL部分。每个产品的数据都不同。

谢谢。

7uc1f3r:

使用: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 );

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

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

将产品自定义字段添加到WooCommerce已完成的订单电子邮件中

WooCommerce电子邮件上的订单自定义字段中的替换收件人

Woocommerce新订单电子邮件通知中的自定义“回复”电子邮件标题

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

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

在WooCommerce的电子邮件和订单页面中保存并显示产品自定义数据

在Woocommerce中的电子邮件订单总计表上的新行中插入自定义字段值

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

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

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

如何将自定义字段作为抄送添加到WooCommerce客户订单电子邮件

产品标签的WooCommerce自定义电子邮件

如何在woocommerce中的新订单电子邮件中显示高级自定义字段数据

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

自定义WooCommerce datepicker检出字段已保存并显示在订单和电子邮件中

在Woocommerce订单和电子邮件中的订单总数之后添加自定义文本

在WooCommerce订单和电子邮件中添加和显示自定义购物车项目数据

WooCommerce:使用自定义电子邮件通知添加自定义订单状态

将可变产品的“描述”字段内容添加到woocommerce“已完成订单”电子邮件中

WooCommerce-每个产品的自定义电子邮件

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

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

PrestaShop 1.6 将订单产品添加到自定义电子邮件模板

仅在 Woocommerce 中针对本地运输和已完成订单的自定义电子邮件通知

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

WooCommerce 订单电子邮件中未显示自定义电话号码字段

在 WooCommerce 客户处理订单电子邮件通知中根据付款方式添加自定义消息

WooCommerce - 将产品简短描述添加到已完成的订单电子邮件