Home > Mobile >  How can I echo something only if the woo-commerce product id is listed in my array?
How can I echo something only if the woo-commerce product id is listed in my array?

Time:01-21

I need to echo a script only if the WP product id is listed in the array. The below code is of course working only for the first product id (226), but I am not sure how to update this part if(is_product() && get_the_id() == 226) in order to check among all of the array elements

$booking = array(
    '226' => 349992,
    '2456' => 349999,
    '2498' => 350001,
    '2500' => 350002,
    '2502' => 350003,
    '2504' => 350006,
    '2665' => 350008
);
            $item_number = $booking[$product->get_id()];
$fh_cal = '<script src="https://test.com/embeds/script/calendar-small/test/items/'.$item_number.'/?fallback=simple&full-items=no"></script>';
            if(is_product() && get_the_id() == 226) {
    echo $fh_cal;
} else {
include ABSPATH.'wp-content/themes/test/inc/form.php';
}

CodePudding user response:

If you want to know if the product exists in that array, we can simply do an isset() to check it:

Change

&& get_the_id() == 226 

to

&& isset($booking[get_the_id()])

That will evaluate as true if there is an array element with that ID as key

  •  Tags:  
  • Related