I’m having a difficult to finish a if statement twice.. The rest works fine but when I try to check in another subarray it doesn’t work.
EDIT:
– The first if condition is to check the type of products
– The second if condition is to check the size attributes.
– Products: sweatshirt_crianca and sweatshirtc_crianca_capuz
– Sizes: 4_anos, 6_anos, 8_anos, 11_anos, 12_anos and 14_anos
Need to disable ( $this->enabled = false; ) Those sizes except 14_anos.
This is what I get from $order->products[$i]
Array
(
[qty] => 1
[name] => .Mr Mickey T-shirt
[model] =>
[image] => blusa-mr-mrs-blusao-sweat-camisolas-portugal.png
[tax] => 20
[tax_description] => IVA 23%
[price] => 10.8333
[final_price] => 16.6666
[weight] => 0.00
[id] => 1342{18}135{17}132{19}148
[attributes] => Array
(
[0] => Array
(
[option] => s_cor_produto
[value] => branco
[option_id] => 18
[value_id] => 135
[prefix] => +
[price] => 0.0000
)
[1] => Array
(
[option] => s_produto
[value] => sweatshirt_crianca
[option_id] => 17
[value_id] => 132
[prefix] => +
[price] => 5.8333
)
[2] => Array
(
[option] => s_tamanho_produto
[value] => 8_anos
[option_id] => 19
[value_id] => 148
[prefix] => +
[price] => 0.0000
)
)
)
Array
(
[qty] => 1
[name] => Adivinha quem vai ser mama shirt
[model] =>
[image] => adivinha-quem-vai-ser-mama-tshirt-gravida.png
[tax] => 20
[tax_description] => IVA 23%
[price] => 10.8333
[final_price] => 10.8333
[weight] => 0.00
[id] => 1860{20}157{18}139{17}128{19}152
[attributes] => Array
(
[0] => Array
(
[option] => s_cor_impressao
[value] => branco
[option_id] => 20
[value_id] => 157
[prefix] => +
[price] => 0.0000
)
[1] => Array
(
[option] => s_cor_produto
[value] => azul_royal
[option_id] => 18
[value_id] => 139
[prefix] => +
[price] => 0.0000
)
[2] => Array
(
[option] => s_produto
[value] => tshirt_mulher
[option_id] => 17
[value_id] => 128
[prefix] => +
[price] => 0.0000
)
[3] => Array
(
[option] => s_tamanho_produto
[value] => M
[option_id] => 19
[value_id] => 152
[prefix] => +
[price] => 0.0000
)
)
)
Sometimes it can contains 1 or 4 subarrays. This is cod payment from osCommerce, I’m trying to exclude specific products from this method based on the attributes of it.
This works fine in the first if statement to check the [value] but the lines that are commented the syntax is correct but it’s not working. This is the code I got:
for ($i=0, $n=sizeof($order->products); $i<$n; $i++) {
if ($order->products[$i]['weight'] > '0.00') {
$this->enabled = false;
}
if ( (isset($order->products[$i]['attributes'])) && (sizeof($order->products[$i]['attributes']) > 0) ) {
for ($j=0, $n2=sizeof($order->products[$i]['attributes']); $j<$n2; $j++) {
if ( ($order->products[$i]['attributes'][$j]['option'] == 's_produto') && ($order->products[$i]['attributes'][$j]['value'] == 'sweatshirt_crianca') ) {
//if ( ($order->products[$i]['attributes'][$j]['option'] == 's_tamanho_produto') && ($order->products[$i]['attributes'][$j]['value'] == '14_anos') ) {
$this->enabled = false;
//}
}
else if ( ($order->products[$i]['attributes'][$j]['option'] == 's_produto') && ($order->products[$i]['attributes'][$j]['value'] == 'sweatshirtc_crianca_capuz') ) {
//if ( ($order->products[$i]['attributes'][$j]['option'] == 's_tamanho_produto') && ($order->products[$i]['attributes'][$j]['value'] != '14_anos') ) {
$this->enabled = false;
//}
}
}
}
}
Any solutions? Thanks.
3
Answers
Sidyll beat me to it but I was thinking along similar lines:
I would rewrite your loop using
foreach
to make it more readable and prone to less errors. Also, watch out for your comparisons (==
vs.===
). Try it in this manner:The main problem with your code is that you want to test for different
array elements but you’re doing it while iterating one by one. The code
inside the
for
loop will only see the current element on theiteration. So when you nest the
if
statements, you’re basicallytesting if the current element is equal to something, and then if it
is you test if this very same element is equal to something else. This
logic will fail.
Since apparently only the
option
andvalue
keys are important, Isuggest creating another array with those first. Then, you can access
all these properties at once — and not only the current attribute
inside an iterator. For example, here is a simplified attributes array
for your last example in the question:
To create this easily and also to simplify your code, I suggest using
the
foreach ... as
loop. Also, you don’t need to check if thearray has items. If there are no items, simply the
foreach
won’t beexecuted at all. It is worth checking if it is set, like you did, if you
don’t know in advance. But another way is to just skip directly to the
next iteration if it is not set with the
continue
controlstructure.
Now, in the tests mark above you can do your tests like: