-1

In my woocommerce store are different shipping methods. Therefor I want to send customized "Order Completed" emails. One of the shipping methods is: Bezorgen

I try to get this code to work but so far i'm failing:

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";
    }
}

What ever I try this function returns false. As you can see below the value "Bezorgen" does exists in the array. What is wrong here....

  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
    )

)
RiggsFolly
  • 83,545
  • 20
  • 96
  • 136
A3O
  • 493
  • 11
  • 27
  • These aren't arrays, they are objects. You have an array of objects. With no keys (numeric). – delboy1978uk Apr 23 '18 at 13:16
  • 1
    The array itself doesn't contain it. It contains an object which has it as a property. You need to loop trough the array to check if the object has the property `order_item_name` set to `Bezorgen` –  Apr 23 '18 at 13:17

5 Answers5

2

$result is an array of objects, it is not an array of string

foreach($result as $order_item) {
   if($order_item->order_item_name === "Bezorgen") {
      echo "This looks good";
   }
}
ernesthm
  • 391
  • 1
  • 12
1

in_array only works using an 1D array, eg:

$array = ["cars", "planes", "trains"]; // in_array("cars", $array) -- true

To check whether an value exists in a Multidimensional Array you have to do it like so:

foreach($result as $value)
{
    if(in_array("bezorgen", $value))
    {
        echo "Bezorgen exists";
    }
}

Or create your 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;
}

And use it like:

in_array_recursive("bezorgen", $result);
Red
  • 5,189
  • 7
  • 32
  • 62
1

You could use array_column() to extract the data for just the order_item_name column...

if (in_array("Bezorgen", array_column($result, 'order_item_name'))){
   echo "This looks good";
} else {
   echo "There is still something wrong here";
}
Nigel Ren
  • 51,875
  • 11
  • 34
  • 49
  • This is working great! Thanks! It is also easy to extend with my other shipping methods as well! – A3O Apr 23 '18 at 14:18
0

in_array() searchs an array key. For example:

$people = array("Peter", "Joe", "Glenn", "Cleveland");

and then:

in_array("Peter", $peolple);

The value you are searching for is not a key (its a property of an object inside the array).

I think you may try something like this:

$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";
}
0

Your query return an array of objects as result.
You need to loop on each objects to check if your value exists.

You can try as below :

$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";

Hoping I could help :)

TiTnOuK
  • 144
  • 1
  • 9