woocommerce code to bring all products in a dropdown php code

PHOTO EMBED

Wed Oct 18 2023 06:36:26 GMT+0000 (Coordinated Universal Time)

Saved by @Alihaan #php

    
// Form to add a record   to display products dropdown v1.2 starts
echo '<form id="add-record-form" style="display: flex; flex-direction: row;">
<div class="main_container" style="margin-right: 20px;">
    <label for="product_name">Product Name:</label>
    <select id="product_name" name="product_name">';
    
// Retrieve product names from the database
$query = new WP_Query(array(
    'post_type' => 'product',
    'post_status' => 'publish',
    'posts_per_page' => '-1'
));

if ($query->have_posts()) :
    while ($query->have_posts()) : $query->the_post();
        echo '<option value="' . get_the_title() . '">' . get_the_title() . '</option>';
    endwhile;
    wp_reset_postdata();
else:
    echo '<option value="">No products found</option>';
endif;

echo '</select>
</div>';

// v1.2 stops
content_copyCOPY