php - 如何将产品 SKU 添加到 WooCommerce 新订单电子邮件主题
问题描述
基于Woocommerce - 将用户名添加到新订单电子邮件主题行。我正在尝试更改 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;
}
解决方案
您可以为此使用以下内容,产品 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 );
推荐阅读
- macos - 如何在 OS X 中显示加载的 shell 文件?
- javascript - JavaScript 助手 | startsWith( ) 不是函数
- java - 使用 Optional.orElseGet 做一些日志记录逻辑是个好主意吗
- javascript - 使用 API Gateway 在 Lambda 请求中不断收到 502。请求的资源上不存在“Access-Control-Allow-Origin”标头
- python - 用该列中的类标记绘图图例
- c - 直接将 char *nib 转换为 int hex
- wordpress - Wordpress 迁移后徽标丢失
- r - 在列表列中修改数据框
- ethereum - 可以在 Truffle 中混合 v0.4.x 合约和 v0.5.x 合约吗?
- python - 运行线程并在工作期间询问数据