栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Get orders from a SQL query displayed on as a list in Woocommerce

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Get orders from a SQL query displayed on as a list in Woocommerce

There is many little errors and mistakes. Your SQL query was incorrect, so I
have changed in a way to make it work first.

Here is your revisited pre to get order line items from your SQL query in an
html table. It’s just a functional and tested example pre, that you
should need to change for your needs:

add_shortpre('sqlquery_shortpre', 'sqlquery_shortpre');function sqlquery_shortpre( $atts ) {    global $wpdb;    $results = $wpdb->get_results("        SELECt a.order_id, a.order_item_id AS item_id, a.order_item_name AS item_name,        b.meta_value AS product_id, c.meta_value AS quantity, d.meta_value AS subtotal        FROM {$wpdb->prefix}woocommerce_order_items a        JOIN {$wpdb->prefix}woocommerce_order_itemmeta  b ON a.order_item_id = b.order_item_id        JOIN {$wpdb->prefix}woocommerce_order_itemmeta  c ON a.order_item_id = c.order_item_id        JOIN {$wpdb->prefix}woocommerce_order_itemmeta  d ON a.order_item_id = d.order_item_id        WHERe a.order_item_type = 'line_item'        AND b.meta_key = '_product_id'        AND c.meta_key = '_qty'        AND d.meta_key = '_line_subtotal'        ORDER BY a.order_id DESC    ");    // Start buffering    ob_start();    echo '<table><tr>';    echo '<th>Order ID</th>';    echo '<th>Item ID</th>';    echo '<th>Product ID</th>';    echo '<th>Item Name</th>';    echo '<th>Quantity</th>';    echo '<th>Subtotal</th>';    echo '</tr>';    // Loop through each order post object    foreach( $results as $result ){        // Html display        echo '<tr>';        echo '<td>' . $result->order_id . '</td>';        echo '<td>' . $result->item_id . '</td>';        echo '<td>' . $result->product_id . '</td>';        echo '<td>' . $result->item_name . '</td>';        echo '<td>' . $result->quantity . '</td>';        echo '<td>' . $result->subtotal . '</td>';        echo '</tr>';    }    echo '</table>';    // Return output (render the buffered data)    return ob_get_clean();}

Code goes in function.php file of your active child theme (or active theme).
Tested and works.


USAGE:

You will paste that shortpre

[sqlquery_shortpre]
in the Wordpress text
editor of a page or a post.

Or you can also paste the following in any php template or file:

echo do_shortpre( "[sqlquery_shortpre]" );

or in between html tags:

<?php echo do_shortpre( "[sqlquery_shortpre]" ); ?>

You will get something like:



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/470561.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号