【发布时间】:2018-11-02 11:00:13
【问题描述】:
我在 woocommercethankyou.php 中获得了以下代码,如果只购买 ONE 类别的产品,则此代码有效。当购买了 'ebook' 和 'ticket' 类别的产品时,'ticket' 将被添加到 $productname 变量中。
如何判断列表中的商品属于一个类别还是多个类别?
<?php
$email = $order->billing_email;
$order_info = wc_get_order( $order );
$productname = "";
foreach( $order_info->get_items() as $item ) {
// check if a product is in specific category
if ( has_term( 'ebook', 'product_cat', $item['product_id'] ) ) {
$productname = ' ebook';
} elseif (has_term( 'ticket', 'product_cat', $item['product_id'] )) {
$productname = 'ticket';
} else {
$productname = 'order details';
}
}
echo '<p> Your ' . $productname . ' will be send to <strong>' . $email . '</strong> as soon as the payment is received.<br>;
?>
【问题讨论】:
-
你到底想做什么?从您正在运行的循环来看,只会显示订单列表中的最后一个产品。
标签: php wordpress woocommerce product orders