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

PHP - Get variable from foreach into array for e-mail function?

对于以下问题表示歉意,因为我的 PHP 知识不是很丰富,但是 google 我似乎无法找到解决方案或解释我如何完成这个

我在带有 WooCommerce 的 Wordpress 中有一个自定义函数,它会在订单完成时触发。

我们的想法是每个项目都有一个供应商的自定义字段和一个供应商的电子邮件地址,在完成订单后,我们想向该供应商发送一封电子邮件,要求他们将这些项目直接运送到客户。

下面是我目前的代码

$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' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $to = array("$supplier <$supplier_email>");
        $subject = "Please dispatch" + $name;
        $content = "Please send the following item \n".$str;
        $status = wp_mail($to, $subject, $content, $headers);
    }

这可行,但是,如果我们说 4 件商品,其中 1 件商品来自 'supplier X',3 件商品来自 'supplier Y',我们将向供应商 X 发送 3 封单独的电子邮件, 而不是一封列出所有项目的电子邮件。

我在想我需要从这个 foreach 中获取每个 $supplier_email 到它自己的数组中 - 然后以这种方式发送电子邮件?但到目前为止我似乎无法让它工作,有人可以帮忙吗?

谢谢!

$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']);
}

你知道其余的:) 另请注意,我使用了供应商名称,如果您可以尝试获取 ID,这将确保它是唯一的,以防出现 2 个具有相同名称的不同供应商。

例如使用:

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

感谢 Rens 提供的信息!现在可以完美运行了。

对于阅读本文 post 的任何人,谁需要它以供将来参考,这是我最终得到的代码:

$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>\r\n";
        }
        $content .= "<br><br><b>Shipping Address:</b><br>";
        $content .=  $address;
        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $subject = 'Order #' . $ordernum . '- Please dispatch the following items:';
        $status = wp_mail($sp, $subject, $content, $headers);

    }