If $codes_a
is a string like this: 08847,032434,35545
then you need to use explode()
to turn it into an array. I suspect what you really want is this:
if ( in_array( "55410", explode( ',', $codes_a ) ) {
echo "Codes A";
}
Pergunta
I have two custom fields in my admin settings that allow me to enter two sets of valid zip codes ('woocommerce_local_delivery_zone_a' and 'woocommerce_local_delivery_zone_b').
I cannot figure out why this code isn't working:
$codes_a = get_option( 'woocommerce_local_delivery_zone_a' );
$codes_b = get_option( 'woocommerce_local_delivery_zone_b' );
if ( in_array( "55410", array( $codes_a ) )) {
echo "Codes A";
}
if ( in_array( "55410", array( $codes_b ) )) {
echo "Codes B";
}
If I echo $codes_a
or $codes_b
(outside of the "if" statement), the string of zip codes returns on the page. If I take that string and enter it directly where the variable is in if (in_array( "55410", array( NUMBERS HERE )))
the text I am trying to echo (Codes A
or Codes B
) works.
I also tried placing array( get_option( 'field_name')) directly in the if statement, and that didn't do anything either. Does in_array not work with getting admin custom fields?
Solução
If $codes_a
is a string like this: 08847,032434,35545
then you need to use explode()
to turn it into an array. I suspect what you really want is this:
if ( in_array( "55410", explode( ',', $codes_a ) ) {
echo "Codes A";
}