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

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
    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 );

    点赞 评论
  • douti6740
    douti6740 2015-11-09 15:22

    Thanks for the info Rens! This now works perfectly.

    For anyone reading this post, who'd need it for future reference, here's the code i've ended up with:

    $order = new WC_Order( $order_id );
    $address = $order->get_formatted_shipping_address();
    $ordernum = $order->get_order_number();
        $items = $order->get_items();
        $suppliers = array();
        foreach ( $items as $item ) {
            $supplier_email = get_post_meta( $item['product_id'], 'Supplier_Email', true );
            $supplier = get_post_meta( $item['product_id'], 'Supplier', true );
            $to = "$supplier <$supplier_email>";
            $suppliers[$supplier]['email'] = $to;
            $suppliers[$supplier]['items'][] = array('name'=>$item['name'], 'price'=>$item['price'], 'SKU'=>$item['product_id'], 'Quantity'=>$item['qty'], 'Price'=>$item['line_total'], 'VAT'=>$item['line_tax'] );
        }
        foreach ($suppliers as $sp) {
    
           $content = '<b>Please supply the following items:</b><br>';
    
            foreach($sp['items'] as $item) {
                $content .= $item['name'] . ' - SKU:' . $item['SKU']. ' - &pound;' . number_format($item['Price'], 2) .' (quantity of ' . $item['Quantity'] . ')' . "<br>
    ";
            }
            $content .= "<br><br><b>Shipping Address:</b><br>";
            $content .=  $address;
            $headers  = 'MIME-Version: 1.0' . "
    ";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "
    ";
            $subject = 'Order #' . $ordernum . '- Please dispatch the following items:';
            $status = wp_mail($sp, $subject, $content, $headers);
    
        }
    
    点赞 评论

相关推荐