最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I create a shortcode from an html and php code written together

programmeradmin1浏览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:

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.

发布评论

评论列表(0)

  1. 暂无评论