0

How can I create a shortcode from an html and php code like below:

function hire_feedback_surveyyy() {
$product_id = 21876;

// We get all the Orders for the given product ID in an arrray
$orders_ids_array = retrieve_orders_ids_from_a_product_id( $product_id );

$output = '';
$output .= '<select>';
foreach($orders_ids_array as $value) {
      $order = wc_get_order( $value );
      $sp_name = $order->get_billing_company();
      $order_id = $order->get_id();

$output .=  '<option value="'.$order_id.'">'.$sp_name.'</option>';
}
$output .= '</select>';

return $output;
}
add_shortcode( 'show_hire_analytics', 'hire_feedback_surveyyy');

I also tried this

function hire_feedback_surveyyy() {
    ob_start();
    $product_id = 21876;

// We get all the Orders for the given product ID in an arrray
$orders_ids_array = retrieve_orders_ids_from_a_product_id( $product_id );

?>
<select>
  <?php foreach($orders_ids_array as $value) {
      $order = wc_get_order( $value );
      $sp_name = $order->get_billing_company();
      $order_id = $order->get_id();
  ?>
    <option value="<?php echo $order_id ?>"><?php echo $sp_name ?></option>
  <?php }?>
</select>

<?php
        $content = ob_get_contents();
    ob_end_clean();
    return $content;
}
add_shortcode( 'show_hire_analytics', 'hire_feedback_surveyyy');

Here is the function I am trying to use in the shortcode as seen on this page: https://stackoverflow.com/questions/43664819/get-all-orders-ids-from-a-product-id

function retrieve_orders_ids_from_a_product_id( $product_id ) {
    global $wpdb;

    // Define HERE the orders status to include in  <==  <==  <==  <==  <==  <==  <==
    $orders_statuses = "'wc-completed'";

    # Requesting All defined statuses Orders IDs for a defined product ID
    $orders_ids = $wpdb->get_col( "
        SELECT DISTINCT woi.order_id
        FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim, 
             {$wpdb->prefix}woocommerce_order_items as woi, 
             {$wpdb->prefix}posts as p
        WHERE  woi.order_item_id = woim.order_item_id
        AND woi.order_id = p.ID
        AND p.post_status IN ( $orders_statuses )
        AND woim.meta_key LIKE '_product_id'
        AND woim.meta_value LIKE '$product_id'
        ORDER BY woi.order_item_id DESC"
    );
    // Return an array of Orders IDs for the given product ID
    return $orders_ids;
}

I have tried different things but nothing is working. It seems the foreach statement in the code is preventing the shortcode from working. Any help will be greatly appreciated.

  • The code looks fine to me. At least from my phone. What specifically isn’t working? Is the HTML incorrect? Is something missing? Is nothing appearing at all? Can you be sure the issue is with your mixture of HTML and PHP, and not something to do with the data? – Jacob Peattie May 30 at 14:56
  • Hi Jacob, thanks for your comment. What you suggested may be the problem. I have updated my question with the function I am trying to use in the shortcode. Can you please review that to see if that's where my issue is coming from. – Sadiq11111 May 30 at 15:15
  • What is the issue? What does "nothing is working" mean? Are you getting errors? – rudtek May 30 at 15:18
  • My website breaks when I load the page where the shortcode is on. The shortcode doesn't load and page becomes smaller. – Sadiq11111 May 30 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.