如何在WooCommerce新订单通知邮件中添加订单SKU总数量?
实现WooCommerce新订单邮件末尾展示总SKU数的方案
不用额外安装插件,直接将以下代码添加到当前使用的WordPress主题(更推荐子主题,避免主题更新后代码被覆盖)的functions.php文件即可生效:
add_action( 'woocommerce_email_after_order_table', 'add_total_sku_count_to_new_order_email', 10, 4 ); function add_total_sku_count_to_new_order_email( $order, $sent_to_admin, $plain_text, $email ) { // 仅对新订单通知邮件生效 if ( $email->id !== 'new_order' ) { return; } $sku_collection = []; foreach ( $order->get_items() as $order_item ) { $product = $order_item->get_product(); if ( $product ) { $sku = $product->get_sku(); if ( !empty($sku) ) { $sku_collection[] = $sku; } } } // 统计去重后的SKU总数,如需统计含重复购买的总数量,删除array_unique即可 $total_sku_count = count( array_unique( $sku_collection ) ); echo '<p><strong>总SKU数:' . esc_html( $total_sku_count ) . '</strong></p>'; }
内容的提问来源于stack exchange,提问作者Jack Johnson
相关产品推荐
相关产品推荐

