Wordpress add text base on $product['id']

Here is my script :

require('../wp-load.php');
header("Pragma: no-cache");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Expires: 0");
$access = false;
if(isset($_SESSION['stock_control_access'])){
  if($_SESSION['stock_control_access'] == "yes"){
    $access = true;
  }
}
if($access == false){
  exit("accessdenied");
}
if(!isset($_GET['item_id'])){
  exit("Mauvaise requête");
}
$cat_id = $_GET['item_id'];
wp_set_current_user(null, "user");
$request = new WP_REST_Request('GET', "/wc/v3/products");
$request->set_query_params(["per_page" => "100", "category" => $cat_id]);
$response = rest_do_request($request);
$server = rest_get_server();
$products = $server->response_to_data($response, false);
$output = '';
$output .= "<ul class='product_list' id='{$cat_id}'>" . PHP_EOL;
foreach ($products as $product) {
  if($product['type'] == 'simple'){
    $output .= "<li class='product' data-id='{$product['id']}' data-type='simple' data-stock='{$product['stock_quantity']}' data-open='closed'><a href='{$product['images'][0]['src']}' target='_blank'><img src='{$product['images'][0]['src']}'></a>{$product['name']}-{$product['id']} $text</li>";
  } else {
    $output .= "<li class='product' data-id='{$product['id']}' data-open='closed'><a href='{$product['images'][0]['src']}' target='_blank'><img src='{$product['images'][0]['src']}'></a>{$product['name']}-{$product['id']}</li>";
  }
  $output .= PHP_EOL;
}
$output .= '</ul>';
echo $output;
?>

I'd like to add within the li my custom text with $text. So, i've tried something like this:

if($product['id'] == ('8499' || '5348'|| '5389' || '7331')) {
  $text = "test1"
  }
if($product['id'] == ('8494' || '5368'|| '5339' || '5331')) {
  $text = "test2"
  }

With no luck, can someone help me implement these two pieces of code?

1 answer

  • answered 2021-06-19 12:45 cbrr09

    $ids = array('8499', '5348', '5389', '7331');
    
    if( in_array($product['id'], $ids) ) {
        $text = 'some text';
    }
    

    then repeat the process.