Php 获取要在变量中使用的所有订单项

Php 获取要在变量中使用的所有订单项,php,wordpress,loops,woocommerce,Php,Wordpress,Loops,Woocommerce,我正在使用woocommerce\u payment\u complete()函数在付款完成后发送一封包含订单详细信息的自定义电子邮件: add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1); function custom_process_order($order_id) { $order = wc_get_order( $order_id ); $first_name =

我正在使用woocommerce\u payment\u complete()函数在付款完成后发送一封包含订单详细信息的自定义电子邮件:

add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);
function custom_process_order($order_id) {
    $order      = wc_get_order( $order_id );
    $first_name = $order->get_billing_first_name();
    $last_name      = $order->get_billing_last_name();
    $company        = $order->get_billing_company();
    $email          = $order->get_billing_email();
    $order_number   = $order->get_order_number();
    $items      = $order->get_items();
    $total      = $order->get_total();
    $more_info      = $order->get_customer_note();

    foreach ($items as $item) {
        $product_name   = $item->get_name();
    }

    $to = $email;
    $subject = 'Order Details';
    $body = 'This order total is: ' . $total . '<br />First name: ' . $first_name . '<br />Last name: ' . $last_name . '<br />Company: ' . $company . '<br />Email: ' . $email . '<br />Order number: ' . $order_number . '<br />Items: ' . $product_name;
    $headers = array('Content-Type: text/html; charset=UTF-8');

    wp_mail( $to, $subject, $body, $headers );

    return $order_id;
}
add_action('woocmerce_payment_complete','custom_process_order',10,1);
函数自定义处理订单($order\U id){
$order=wc\u get\u order($order\u id);
$first_name=$order->get_billing_first_name();
$last_name=$order->get_billing_last_name();
$company=$order->get_billing_company();
$email=$order->get_billing_email();
$order_number=$order->get_order_number();
$items=$order->get_items();
$total=$order->get_total();
$more_info=$order->get_customer_note();
foreach($items作为$item){
$product_name=$item->get_name();
}
$to=$email;
$subject='订单详情';
$body='此订单总数为:'.$total'.
名字:'.$First_name'.
姓氏:'.$Last_name'.
公司:'.$Company'.
电子邮件:'.$Email'.
订单号:'.$order_number'.
项目:'.$product_name; $headers=array('Content-Type:text/html;charset=UTF-8'); wp_邮件($to、$subject、$body、$headers); 返回$order\u id; }
它有效,除了一件事。问题是,电子邮件通知的“项目”($product_name)部分仅在订单中只有一种产品时显示某些内容。如果有多个产品,“项目”将不显示任何内容

我做错了什么

问题出在foreach循环中,每个循环都要执行 覆盖当前变量(产品名称),产品名称为 下一个产品

示例:

$items = array("foo", "bar", "hello", "world");

$product_name = '';

foreach ($items as $item) {
    // The product name
    $product_name .= $item . ' - ';
}

// Result = foo - bar - hello - world -
echo $product_name;
$items=数组(“foo”、“bar”、“hello”、“world”);
foreach($items作为$item){
$product\U name=$item;
}
//结果=世界
echo$产品名称;
解决方法是将字符串连接起来

因此,与
$variable
=
某物相比

使用
$variable
=
一些东西

所以你得到了

示例:

$items = array("foo", "bar", "hello", "world");

$product_name = '';

foreach ($items as $item) {
    // The product name
    $product_name .= $item . ' - ';
}

// Result = foo - bar - hello - world -
echo $product_name;
解决方案:用以下代码替换当前的foreach循环

$product\u name='';
foreach($items作为$item){
//产品名称
$product_name.=$item->get_name()。'-';
}