制作一个WooCommerce快捷代码,在数据库中搜索自定义字段



我是编码新手。我想要一个搜索表单,一旦他们搜索某个序列号,它就会在数据库中找到,并显示其他自定义字段,如证书、开始日期和结束日期。下面的代码是我走过的最远的代码。

function wpb_demo_shortcode() { global $wpdb;

$results = $wpdb->get_results(" SELECT DISTINCT o.order_id, o.`order_item_name`, om.`meta_value` as 'bcs',  (select  pm.`meta_value`  from   `wp_postmeta` as pm  WHERE  o.order_item_id = om.order_item_id AND pm.`post_id` = o.order_id AND  pm.`meta_key` = '2-certificate') as 'certificate',  (select  pm.`meta_value`  from   `wp_postmeta` as pm  WHERE o.order_item_id = om.order_item_id AND pm.`post_id` = o.order_id AND  pm.`meta_key` = '3-serial') as 'serial' FROM `wp_woocommerce_order_items` as o, `wp_woocommerce_order_itemmeta` as om, `wp_postmeta` as pm WHERE o.order_item_id = om.order_item_id AND pm.`post_id` = o.order_id AND om.`meta_key` = 'bcs' AND  (select  pm.`meta_value`  from   `wp_postmeta` as pm  WHERE  o.order_item_id = om.order_item_id AND pm.`post_id` = o.order_id AND  pm.`meta_key` = '2-certificate') IS NOT null  AND   (select  pm.`meta_value`  from   `wp_postmeta` as pm  WHERE o.order_item_id = om.order_item_id AND pm.`post_id` = o.order_id AND  pm.`meta_key` = '3-serial') IS NOT null ");

$serial = (isset( $_GET['$serial'] )) ? sanitize_text_field($_GET['$serial']) : ''; ?> <form action="" method="get">   <label for="fname">Serial number:</label><br>   <input type="text" id="$serial" name="$serial"><br>   <input type="submit" value="Submit"> </form> <?php $enteredSerial = $_GET['$serial']; searchSerial($enteredSerial); //echo $holder;
//ob_start();
echo '<table><tr>';
echo '<th>Order ID</th>';
echo '<th>Product Name</th>';
echo '<th>BCS</th>';
echo '<th>Serial</th>';
echo '<th>Certificate</th>';
echo '</tr>';

} 
function searchSerial($serial) {   echo $serial; }
add_shortcode('greeting', 'wpb_demo_shortcode');

前端会显示这一点前端图片

有办法做到这一点吗?

您可以通过在产品或自定义帖子类型中使用自定义元框来尝试这一点。我建议使用CMB2(插件(一旦安装,你可以初始化你自己的产品帖子的元框选项,并在你的模板/搜索页面中调用它们。

相关内容

  • 没有找到相关文章

最新更新