如何将产品SKU添加到WooCommerce新订单电子邮件主题

时间:2020-08-26 09:07:54

标签: php wordpress woocommerce email-notifications sku

基于Woocommerce - Add username to new order email subject line。我正在尝试更改WooCommerce电子邮件主题行。在下订单时会以邮件的形式发送给我,这是我作为网站管理员收到的电子邮件。

我正在尝试创建以下结构

[在线商店]订单(#....)名称,第二个名称,总计-...,SKU1,SKU2,SKU3 ...

我使用此代码,但我不知道如何获取订单中每个产品的SKU?

add_filter('woocommerce_email_subject_new_order', 'change_admin_email_subject', 1, 2);
function change_admin_email_subject( $subject, $order ) {
global $woocommerce;
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);

$subject = sprintf( '[%s] Order (# %s) от %s %s Total-%u EU. %s ', $blogname, $order->id, 
$order->billing_first_name, $order->billing_last_name, $order->order_total, $order->...);

return $subject;
}

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码,产品SKU是通过foreach循环从订单对象获得的。

在代码中添加了注释和解释

function filter_woocommerce_email_subject_new_order( $subject, $order ) {   
    // Blogname
    $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
    
    // Get order ID
    $order_id = $order->get_id();
    
    // Get billing first name
    $first_name = $order->get_billing_first_name();
    
    // Get billing last name
    $last_name = $order->get_billing_last_name();
    
    // Get order total
    $order_total = $order->get_total();
    
    // Empty array
    $product_skus = array();

    // Loop through order items
    foreach( $order->get_items() as $item ) {
        // Get an instance of corresponding the WC_Product object
        $product = $item->get_product();
        
        // Push to array
        $product_skus[] = $product->get_sku();
    }
    
    // Array to string
    $product_skus_str = implode( ", ", $product_skus );
    
    // Subject
    $subject = sprintf( '[%s] Order (# %s) From %s %s Total %s SKU %s', $blogname, $order_id, $first_name, $last_name, $order_total, $product_skus_str );

    return $subject;
}
add_filter( 'woocommerce_email_subject_new_order', 'filter_woocommerce_email_subject_new_order', 1, 2 );
相关问题