【问题标题】:PHP - Get variable from foreach into array for e-mail function?PHP - 将变量从 foreach 获取到电子邮件函数的数组中?
【发布时间】:2015-11-09 23:22:02
【问题描述】:

对以下问题表示歉意,因为我的 PHP 知识不是很强,但是在 google 下,我似乎无法找到解决方案,或者无法解释如何完成此操作

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

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

下面是我目前的代码

$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 到它自己的数组中 - 然后以这种方式发送一封电子邮件?但到目前为止我似乎无法让它工作,有人可以帮忙吗?

谢谢!

【问题讨论】:

  • 这有点模糊,但也许将供应商 ID 添加到您的 foreach 中的数组中是一个想法。然后在开始时检查当前的供应商 ID 是否已经在数组中。
  • 只需先对特定订单的商品进行循环,然后创建一个额外的数组,将供应商 ID 存储为数组的键,并在其中存储一个包含所有商品的数组。然后,您可以循环该数组,并通过一封电子邮件以及其中的所有项目很好地将电子邮件发送给每个供应商。
  • Bart 提到的基本相同 :) 只需要 ID 检查。你可以简单地这样做:$array[$suplier_id]['items][] = array()
  • 感谢您提供的信息,您能否提供更多信息,了解如何将每个供应商实际纳入阵列?我目前正在尝试使用 $sps[] = array(get_post_meta( $items['product_id'], 'Supplier', true )); 将每个供应商放入一个数组中。但似乎运气不太好

标签: php arrays wordpress woocommerce hook


【解决方案1】:
$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!所以我想我们需要在循环之外启动数组,我可以看到我们如何将数组添加到底部的新变量中,我有几个快速的问题......理想情况下我想要数组中的项目名称/价格等,所以我猜以下是我需要的$suppliers[$supplier]['items'][] = array('name'=&gt;$item['name'], 'price'=&gt;$item['price'], 'SKU'=&gt;$product_id, ); 然后我需要在每个供应商上运行另一个循环来发送电子邮件吗?会不会是类似于foreach ($supplier as $sp) { // code here }
  • 是的,完全一样,这只会向每个供应商发送一封电子邮件,但会包含他们所有的物品:) 所以你只有 3 个循环,一个用于循环订单并使用他们的物品创建供应商数组,一个用于循环供应商,并在其中循环以获取项目。
【解决方案2】:

感谢 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']. ' - &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);

    }

【讨论】:

  • 嘿,看起来不错,小便条,我不知道您是否愿意为您的电子邮件字符集使用 ISO-8859-1。 “UTF-8 是一种多字节编码,可以表示任何 unicode 字符。ISO-8859-1 是一种单字节编码,可以表示前 256 个 unicode 字符。尽管两者对 ASCII 的编码方式完全相同。”我个人一直使用 UTF-8,当一些外语客户使用我构建的插件时,我遇到了 ISO 问题。
猜你喜欢
  • 2018-06-19
  • 1970-01-01
  • 2015-11-27
  • 1970-01-01
  • 2015-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多