2017-04-11 69 views
0

请我找到此代码作为WooCommerce解决方案新订单电子邮件手动触发某些产品,但我不知道在哪里把这个代码完全放在functions.php或谢谢。WooCommerce新订单电子邮件手动触发某些产品

/** 
* Modified from https://www.skyverge.com/blog/add-woocommerce-email-recipients-conditionally/ 
* 
* Add another email recipient for admin New Order emails if a product from a specific category or with a specific tag is ordered 
* 
* @param string $recipient a comma-separated string of email recipients (will turn into an array after this filter!) 
* @param \WC_Order $order the order object for which the email is sent 
* @return string $recipient the updated list of email recipients 
*/ 
function sv_conditional_email_recipient($recipient, $order) { 
    // Bail on WC settings pages since the order object isn't yet set yet 
    // Not sure why this is even a thing, but shikata ga nai 
    $page = $_GET['page'] = isset($_GET['page']) ? $_GET['page'] : ''; 
    if ('wc-settings' === $page) { 
     return $recipient; 
    } 

    // just in case 
    if (! $order instanceof WC_Order) { 
     return $recipient; 
    } 
    $items = $order->get_items(); 

    // check if product from category or with tag is in order 
    foreach ($items as $item) { 
     $product = $order->get_product_from_item($item); 
     $args = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'names'); 
     $product_cats = wp_get_post_terms($product->get_id, 'product_cat', $args); // could swap product_cat for product_tag 
     // add our extra recipient if there's a product from the category with slug "dieta" - commas needed! 
     // we can bail if we've found one, no need to add the recipient more than once 
     if ($product && in_array("dieta", $product_cats)) { 
      $recipient .= ', [email protected]'; 
      return $recipient; 
     } 
    } 

    return $recipient; 
} 
add_filter('woocommerce_email_recipient_new_order', 'sv_conditional_email_recipient', 10, 2); 
+0

你可以把它放在function.php的末尾,记得从代码中删除'<?php',请冷静地用大写。 – jjj

+0

HI Jerome感谢您的回应我添加它,但始终我无法接收新订单电子邮件手动触发某些产品有任何解决方法,它是如何做到的?谢谢 –

+0

你能收到来自本网站的自动电子邮件通知吗? – jjj

回答

0

我的代码这个和它的作品在我的网站,不要忘记通过两个邮件类别和[email protected],[email protected]的名称来代替the_name_of_the_cat

function change_email_recipient_depending_of_cat ($recipient, $order) { 
    global $woocommerce; 
    $items = $order->get_items(); 
    foreach ($items as $item) { 
     $product_id = $item['product_id']; 
     if (has_term('the_name_of_the_cat', 'product_cat', $product_id)) { 
      $recipient = '[email protected],[email protected]'; 
     } 
     return $recipient; 
    } 
} 
add_filter('woocommerce_email_recipient_new_order', 'change_email_recipient_depending_of_cat', 10, 2); 
相关问题