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 github符合条件20分钟秒到账,github空投 提供github账号可兑换💰感兴趣的可以找我交流一下
  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?