【问题标题】:Custom email subject placeholders for WooCommerce order related bookingsWooCommerce 订单相关预订的自定义电子邮件主题占位符
【发布时间】:2020-11-22 20:59:33
【问题描述】:

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

我已经尝试Create additional variables/placeholders for Woocommerce email notifications subject 代码进行一些更改,看看我是否可以让它工作。另外,我尝试获取 woocommerce 元字段,但也没有用。

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

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

【问题讨论】:

  • 这里的 $get_order 是什么?例如,请显示 $get_order 的 var_dump

标签: php sql wordpress woocommerce woocommerce-bookings


【解决方案1】:

首先你的主要问题是:

如何从订单(Id)中获取预订ID?

由于一个订单可以有许多预订(项目),您可以通过非常简单且轻量级的 SQL 查询使用 WordPress WPDB 类和可用方法。

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

// 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 文件中。经过测试并且可以工作。

【讨论】:

  • 非常感谢!我只是在我这边测试了它,它适用于开始日期。我还有其他一些要求,我使用上面的代码尝试了它们,但它的功能不一样。这是你可以看的东西吗?我很乐意为您支付您的时间。
猜你喜欢
  • 2016-03-03
  • 2018-07-20
  • 2019-03-30
  • 1970-01-01
  • 2019-05-29
  • 1970-01-01
  • 1970-01-01
  • 2020-11-20
  • 2020-08-21
相关资源
最近更新 更多