在我的woocommerce商店有不同的运送方式。因此,我想发送定制的"订单已完成"电子邮件。其中一种运输方式是:Bezorgen
我尝试让这段代码正常运行,但到目前为止我还没有成功:
function llv_delivery_notes(){
$order_id = get_the_id();
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM llv_woocommerce_order_items
WHERE order_id = $order_id;" );
?>
<pre>
<?php print_r($result);?>
</pre>
<?php
if (in_array("Bezorgen", $result)){
echo "This looks good";
} else {
echo "There is still something wrong here";
}
}
我尝试此函数时返回false。正如您在下面看到的价值&#34; Bezorgen&#34;确实存在于数组中。这里有什么问题....
Array
(
[0] => stdClass Object
(
[order_item_id] => 160
[order_item_name] => Test pakket met koteletten en rollade
[order_item_type] => line_item
[order_id] => 448
)
[1] => stdClass Object
(
[order_item_id] => 161
[order_item_name] => Lekker sappig koteletje geweldig op de barbeque (4 stuks)
[order_item_type] => line_item
[order_id] => 448
)
[2] => stdClass Object
(
[order_item_id] => 162
[order_item_name] => Rollade
[order_item_type] => line_item
[order_id] => 448
)
[3] => stdClass Object
(
[order_item_id] => 163
[order_item_name] => Bezorgen
[order_item_type] => shipping
[order_id] => 448
)
[4] => stdClass Object
(
[order_item_id] => 164
[order_item_name] => NL-BTW 6%-1
[order_item_type] => tax
[order_id] => 448
)
[5] => stdClass Object
(
[order_item_id] => 165
[order_item_name] => wc_points_redemption_2_2018_03_19_12_33
[order_item_type] => coupon
[order_id] => 448
)
)
答案 0 :(得分:2)
$result
是一个对象数组,它不是string
foreach($result as $order_item) {
if($order_item->order_item_name === "Bezorgen") {
echo "This looks good";
}
}
答案 1 :(得分:1)
in_array
仅适用于1D数组,例如:
$array = ["cars", "planes", "trains"]; // in_array("cars", $array) -- true
要检查多维数组中是否存在值,您必须这样做:
foreach($result as $value)
{
if(in_array("bezorgen", $value))
{
echo "Bezorgen exists";
}
}
或创建own function:
function in_array_recursive($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
并使用它:
in_array_recursive("bezorgen", $result);
答案 2 :(得分:1)
您可以使用array_column()
仅为order_item_name列提取数据...
if (in_array("Bezorgen", array_column($result, 'order_item_name'))){
echo "This looks good";
} else {
echo "There is still something wrong here";
}
答案 3 :(得分:0)
in_array()搜索数组键。例如:
$people = array("Peter", "Joe", "Glenn", "Cleveland");
然后:
in_array("Peter", $peolple);
您要搜索的值不是键(它是数组中对象的属性)。
我想你可以尝试这样的事情:
$founded = false;
foreach($result as $value){
if($value->order_item_name == 'Bezorgen') $founded = true;
}
if ($founded){
echo "This looks good";
} else {
echo "There is still something wrong here";
}
答案 4 :(得分:0)
您的查询会返回一个对象数组 您需要循环每个对象以检查您的值是否存在。
您可以尝试如下:
$looksGood = false;
foreach($result as $key => $obj) {
if (isset($obj->order_item_name) && ($obj->order_item_name === "Bezorgen") {
$looksGood = true;
}
}
echo ($looksGood) ? "This looks good" : "There is still something wrong here";
希望我能帮忙:)。