WooCommerce 订单相关预订的自定义电子邮件主题占位符 [英] Custom email subject placeholders for WooCommerce order related bookings

查看:23
本文介绍了WooCommerce 订单相关预订的自定义电子邮件主题占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Woocommerce 提供了向电子邮件主题添加占位符的选项.我想通过创建一个自定义占位符来扩展这种能力,该占位符从 woocommerce 重力形式产品插件和 woocommerce 预订中提取信息.

Woocommerce gives the option to add placeholders to the subject of an email. I'd like to expand on that ability by creating a custom placeholder that pulls information from woocommerce gravity forms product addons and woocommerce bookings.

我试过为 Woocommerce 电子邮件通知主题创建其他变量/占位符 代码进行一些更改以查看是否可以使其正常工作.另外,我尝试获取 woocommerce 元字段,但这也不起作用.

I've tried Create additional variables/placeholders for Woocommerce email notifications subject code making some changes to see if I could make it work. Also, I tried getting the woocommerce meta fields but that didn't work either.

在此代码上,我无法获得与订单相关的预订:

On this code I am not able to get the booking related to the order:

// For woocommerce versions up to 3.2
add_filter( 'woocommerce_email_format_string' , 'filter_email_format_string', 20, 2 );
function filter_email_format_string( $string, $email ) {
    // Get the instance of the WC_Order object
    $booking = $get_order;

    // Additional wanted placeholders in the array (find / replace pairs)
    $additional_placeholders = array(
        '{custom_one}'      => $booking->get_start_date(),
    );

    // return the clean string with new replacements
    return str_replace( array_keys( $additional_placeholders ), array_values( $additional_placeholders ), $string );
}

推荐答案

首先你的主要问题是:

由于一个订单可以有多个预订(项目),您可以使用 WordPress WPDB 类和可用方法.

As an order can have many bookings (items), you can get the booking Id(s) targeting the post parent (which is the order ID) with a very simple and light SQL query Using WordPress WPDB Class and available methods.

然后,您将能够从该订单的一次预订中获取开始日期,并将其设置为电子邮件通知主题的自定义占位符:

Then you will be able to get the start date from one booking of this order and set it as a custom placeholder for email notification subject:

// Custom function to get the booking Ids from an order Id (with an SQL query)
function wc_get_booking_ids( $order_id ){
    global $wpdb;

    return $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'wc_booking' AND post_parent =  %d", $order_id ) );
}

// For woocommerce versions up to 3.2
add_filter( 'woocommerce_email_format_string' , 'filter_email_format_string', 20, 2 );
function filter_email_format_string( $string, $email ) {
    if( ! is_a($email->object, 'WC_Order') ) return; // Exit

    $booking_ids = wc_get_booking_ids( $email->object->get_id() );

    if( ! empty($booking_ids) ) {
        // Get the instance of the WC_Booking object from one booking Id
        $booking = new WC_Booking( reset($booking_ids) );

        if( is_a($booking, 'WC_Booking') && method_exists($booking, 'get_start_date') ) {
            // Additional placeholders array (one by line)
            $custom_placeholders = array(
                '{start_date}' => $start_date = $booking->get_start_date(),
            );
            $string = str_replace( array_keys( $custom_placeholders ), array_values( $custom_placeholders ), $string );
        }
    }
    return $string;
}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

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

这篇关于WooCommerce 订单相关预订的自定义电子邮件主题占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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