Issue
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';
}
Solution
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
Answered By - M. Eriksson
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.