[英]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件是“供应商X”,而有3件来自“供应商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! 现在,它可以完美运行。
对于阅读此帖子的任何人,需要它以备将来参考,下面是我最终得到的代码:
$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']. ' - £' . 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.