mercredi 28 février 2018

If condition PHP wrong?

I have a basic question about what's giving me a very big headache.

In the code below:

$item['attributes']['chave'] = '1001';

// Adicionar item se não existir
if ($cart->isItemExists($item['attributes']['chave'])) {
  echo 'Exists, id: '.$item['attributes']['chave'];
} else {
  $cart->add($_POST['id'], $_POST["my-item-qty"], [
  'price'  => $_POST["my-item-price"],
  'color'  => $_POST["my-item-name"],]);
}

The "if" condition is not executed, even though I know that the item with code '1001' exists, if the if condition is not executed, it goes straight to the "else".

isItemExists() returning false? Or my condition some how is not cheking correct?

Here is the isItemExists() code:

public function isItemExists($id, $attributes = [])
{
    $attributes = (is_array($attributes)) ? array_filter($attributes) : [$attributes];

    if (isset($this->items[$id])) {
        $hash = md5(json_encode($attributes));
        foreach ($this->items[$id] as $item) {
            if ($item['hash'] == $hash) {
                return true;
            }
        }
    }

    return false;
}

I'm looking at the php manual, and my condition looks correct. What is wrong?

Aucun commentaire:

Enregistrer un commentaire