doushi7819 2015-11-09 00:52
浏览 49
已采纳

PHP - 从foreach获取变量到数组以获取电子邮件功能?

Apologies for the following question, as my PHP knowledge isn't hugely strong, But having google'd around, i can't seem to find a solution, or an explanation as to how i could complete this

I have a custom function in Wordpress with WooCommerce which fires off on order complete.

The idea is each item has a custom field for supplier, and a suppliers e-mail address, upon a completed order, we'd like to send an e-mail to that supplier requesting they ship those items direct to the customer.

Below is my code currently

$order = new WC_Order( $order_id );
$items = $order->get_items();
$address = $order->get_formatted_shipping_address();
$totalprice;
$str = "";
foreach ( $items as $item ) {
        $supplier = get_post_meta( $item['product_id'], 'Supplier', true );
        $supplier_email = get_post_meta( $item['product_id'], 'Supplier_Email', true );
        $str .= $product_name = $item['name']."  ";
        $str .= $product_id = $item['product_id']." ";
        $pprice = $item['price'];
        $name = $item['name'];
        $headers  = 'MIME-Version: 1.0' . "
";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";
        $to = array("$supplier <$supplier_email>");
        $subject = "Please dispatch" + $name;
        $content = "Please send the following item 
".$str;
        $status = wp_mail($to, $subject, $content, $headers);
    }

Which works, however, if we have say 4 items, with 1 item being some 'supplier X' and 3 items being from 'supplier Y' we're sending out 3 individual e-mails to supplier X, As opposed to one e-mail, which lists all items.

I'm thinking that i need to get each $supplier_email from this foreach, into it's own array - and then send an e-mail that way? But i can't seem to get it to work thus far, Could anyone help?

Thanks!

  • 写回答

2条回答 默认 最新

  • duanfenhui5511 2015-11-09 01:15
    关注
    $order = new WC_Order( $order_id );
    $items = $order->get_items();
    $suppliers = array();
    foreach ( $items as $item ) {
        $supplier = get_post_meta( $item['product_id'], 'Supplier', true );
        $to = "$supplier <$supplier_email>";
        $suppliers[$supplier]['email'] = $to;
        $suppliers[$supplier]['items'][] = array('name'=>$item['name']);
    }
    

    You know the rest :) Also note that i used supplier name, if you can try to get the ID instead this will make sure it is unique, in case 2 different supplier with same name occur.

    For instance use:

    $supplier = get_post_meta( $item['product_id'], 'Supplier_ID', true );

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Java,消息推送配置
  • ¥15 Java计划序号重编制功能,此功能会对所有序号重新排序,排序后不改变前后置关系。
  • ¥15 关于哈夫曼树应用得到一些问题
  • ¥15 使用sql server语句实现下面两个实验(需要代码和运行结果截图)
  • ¥20 用web解决,要给我一个完整的网页,符合上述的要求
  • ¥20 求个sql server代码和结果的图 两道题
  • ¥15 银河麒麟操作系统无法使用U盘
  • ¥100 寻找:光电二极管电路设计服务
  • ¥15 YOLOv5改进后的结构图
  • ¥15 全志v3s怎么设置高速时钟,使用的荔枝派zero开发板,串口2需要921600的波特率