当 WooCommerce 中的特定订单项元数据为空时发送自定义电子邮件
Send a custom email when a specific order item meta is empty in WooCommerce
我在某些产品中使用此代码获取元数据。
$meta_data = $item->get_formatted_meta_data($hideprefix = '_', $include_all = false );
foreach ($meta_data as $dato){
$datoo = (array) $dato;
$mess .= $datoo['display_key'].': ' .$datoo['value']. '<br>' ;
}
其中一个键是“Nome 文件”。
现在我需要在所有产品中此元为空时发送邮件。
我需要修改这段代码,但我不知道如何...
// On order completed status
add_action('woocommerce_order_status_processing', 'send_a_custom_email', 20, 1 );
//add_action('woocommerce_order_status_on-hold', 'send_a_custom_email', 20, 1 );
function send_a_custom_email( $order_id ) {
$order = wc_get_order( $order_id ); // The WC_Order object
foreach ( $order->get_items() as $item_id => $item ) {
$product = $item->get_product(); // The WC_Product object
// Get the parent product ID for product variations for product categories
$the_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
if ( has_term( array(115, 117, 555, 556, 789, 786, 554), 'product_cat', $the_id ) ) {
有人可以帮助我吗?
这是在特定订单项元数据丢失(或为空)时发送自定义电子邮件的方法:
add_action('woocommerce_order_status_processing', 'send_a_custom_email', 20, 2 );
// add_action('woocommerce_order_status_on-hold', 'send_a_custom_email', 20, 2 );
function send_a_custom_email( $order_id, $order ) {
$product_ids = array(); // Initializing
foreach ( $order->get_items() as $item ) {
$meta_data = $item->get_meta('Nome File'); // Get order item meta data
if( empty($meta_data) ) {
$product_ids[] = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();
}
}
if ( ! empty($product_ids) ) {
$recipient = 'name@email.com'; // Set email recipient
$subject = sprintf( __('Order #%d: Has missing item meta data'), $order_id );
$content = sprintf( __('On Order #%d, purchased products %s have missing meta data'), $order_id, implode(', ', $product_ids) );
wp_mail( $recipient, $subject, $content ); // Send email
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。应该可以。
我在某些产品中使用此代码获取元数据。
$meta_data = $item->get_formatted_meta_data($hideprefix = '_', $include_all = false );
foreach ($meta_data as $dato){
$datoo = (array) $dato;
$mess .= $datoo['display_key'].': ' .$datoo['value']. '<br>' ;
}
其中一个键是“Nome 文件”。
现在我需要在所有产品中此元为空时发送邮件。
我需要修改这段代码,但我不知道如何...
// On order completed status
add_action('woocommerce_order_status_processing', 'send_a_custom_email', 20, 1 );
//add_action('woocommerce_order_status_on-hold', 'send_a_custom_email', 20, 1 );
function send_a_custom_email( $order_id ) {
$order = wc_get_order( $order_id ); // The WC_Order object
foreach ( $order->get_items() as $item_id => $item ) {
$product = $item->get_product(); // The WC_Product object
// Get the parent product ID for product variations for product categories
$the_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
if ( has_term( array(115, 117, 555, 556, 789, 786, 554), 'product_cat', $the_id ) ) {
有人可以帮助我吗?
这是在特定订单项元数据丢失(或为空)时发送自定义电子邮件的方法:
add_action('woocommerce_order_status_processing', 'send_a_custom_email', 20, 2 );
// add_action('woocommerce_order_status_on-hold', 'send_a_custom_email', 20, 2 );
function send_a_custom_email( $order_id, $order ) {
$product_ids = array(); // Initializing
foreach ( $order->get_items() as $item ) {
$meta_data = $item->get_meta('Nome File'); // Get order item meta data
if( empty($meta_data) ) {
$product_ids[] = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();
}
}
if ( ! empty($product_ids) ) {
$recipient = 'name@email.com'; // Set email recipient
$subject = sprintf( __('Order #%d: Has missing item meta data'), $order_id );
$content = sprintf( __('On Order #%d, purchased products %s have missing meta data'), $order_id, implode(', ', $product_ids) );
wp_mail( $recipient, $subject, $content ); // Send email
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。应该可以。