PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 5, 2022

[FIXED] How can I echo something only if the woo-commerce product id is listed in my array?

 February 05, 2022     php, wordpress     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing