Ваш код устарел с Woocommerce 3 и содержит много ошибок. Если вы включите отладку в Wordpress, вы получите много сообщений об ошибках. Вместо этого попробуйте следующее, чтобы отправить электронное письмо для каждого продукта, когда он соответствует определенной категории продуктов:
// Get email content from the custom template
function get_custom_email_content_html( $order, $heading = false, $mailer, $category_slug ) {
$template = 'emails/woo-custom-emails-per-product-'.$category_slug.'.php';
return wc_get_template_html( $template, array(
'order' => $order,
'email_heading' => $heading,
'sent_to_admin' => true,
'plain_text' => false,
'email' => $mailer
)
);
}
// Send a custom email notification for each order item on order processing status change
add_action( 'woocommerce_order_status_changed', 'processing_custom_email_notification', 10, 4 );
function processing_custom_email_notification( $order_id, $status_from, $status_to, $order ) {
if( $status_to === 'processing' ) {
$liquify_array = array('cutting-agent', 'dabjuice-liquify');
$terpene_array = array('terpene-blends', 'strain-specific', 'hybrid', 'indica', 'sativa', 'terpene-sample-kits', 'hybrid-sample-kit', 'indica-sample-kit', 'sativa-sample-kit');
$isolates_array = array('raw-terpene-isolates', 'alpha', 'beta');
// Loop through order items
foreach ( $order->get_items() as $item ) {
$product_category = '';
$categories_slugs = get_the_terms( $item->get_product_id() , 'product_cat', array( 'fields' => 'slugs' ) );
foreach( $categories_slugs as $term_slug ) {
if( in_array( $term_slug, $liquify_array ) ) {
$product_category = 'liquify';
break;
} elseif(in_array( $term_slug, $terpene_array ) ) {
$product_category = 'terpenes';
break;
} elseif(in_array( $term_slug, $isolates_array ) ) {
$product_category = 'isolates';
break;
}
}
if( isset( $product_category ) ) {
// Email subject
if( $product_category == 'liquify' ) {
$subject = __("Instructions for usage of your Liquify Product");
} elseif($product_category == 'terpenes' ) {
$subject = __("Instructions for usage of your Terpenes Product");
} elseif( $product_category == 'isolates' ) {
$subject = __("Instructions for usage of your Raw Terpenes Isolates");
}
$mailer = WC()->mailer(); // Get an instance of the WC_Emails Object
$recipient = $order->get_billing_email(); // Email recipient
$content = get_custom_email_content_html( $order, $subject, $mailer, $product_category ); // Email content (template)
$headers = "Content-Type: text/html\r\n"; // Email headers
// Send the email
WC()->mailer()->send( $recipient, $subject, $content, $headers );
}
}
}
}
Теперь он должен работать лучше (но не тестировался с вашими категориями продуктов) .
3 пользовательских шаблона электронной почты должны находиться в папке woocommerce
> emails
(активной темы) :
woo-custom-emails-per-product-liquify.php
woo-custom-emails-per-product-terpenes.php
woo-custom-emails-per-product-isolates.php