简体   繁体   English

自定义 email 主题占位符,用于 WooCommerce 订单相关预订

[英]Custom email subject placeholders for WooCommerce order related bookings

Woocommerce gives the option to add placeholders to the subject of an email. Woocommerce 提供了向 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 重力 forms 产品插件和 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.我已经尝试为 Woocommerce email 通知主题代码创建其他变量/占位符,并进行一些更改以查看是否可以使其工作。 Also, I tried getting the woocommerce meta fields but that didn't work either.另外,我尝试获取 woocommerce 元字段,但这也不起作用。

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 );
}

First of all your main problem is:首先你的主要问题是:

How to get the booking Id(s) from an order (Id)?如何从订单 (Id) 中获取预订 ID?

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.由于一个订单可以有许多预订(项目),您可以使用 WordPress WPDB Z9BD81329FEBF6EFE227 和可用方法通过非常简单且轻量的 SQL 查询获取针对帖子父级(即订单 ID)的预订 ID。

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:然后,您将能够从该订单的一个预订中获取开始日期,并将其设置为 email 通知主题的自定义占位符:

// 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;
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM