dongshi2836 2019-05-01 16:52
浏览 58

从我的高级搜索结果中获取WC产品时遇到问题

Hello there and wish you all good month. I'm having an issue while I try to fetch products from WooCommerce using my search results (I'm trying to fetch them through my custom "searcher"). The thing is my searcher works but it only brings up a white screen and the name of the product. I want the product to show as it would be shown in a regular Woo Commerce store, with styles and everything as if I was searching it category wise or with any other parameter (like the plugins out there).

In order to help, this is my code so far :

functions.php:

function search_tickets_shortcode() {
   ob_start();
   get_template_part('tickets', 'search');
   return ob_get_clean();   
} 
add_shortcode( 'ticket_searcher', 'search_tickets_shortcode' );

add_action('init','wpse_load_custom_search_template');
function wpse_load_custom_search_template(){
    if( isset($_REQUEST['search']) == 'tickets' ) {
        require('advanced-search-template.php');
        die();
    }
}

advanced-search-template.php:

<ul class="products">
<?php 

$params = array('posts_per_page' => 5,
                'post_type' => 'product'); // (1)
$wc_query = new WP_Query($params); 
// Get data from URL into variables
$_strprt = $_GET['startingport'] != '' ? $_GET['startingport'] : '';
$_endprt = $_GET['endingport'] != '' ? $_GET['endingport'] : '';


// Start the Query
$v_args = array(
        'post_type'     =>  'product', // your CPT
        //'s'             =>  $_strprt, // looks into everything with the keyword from your 'name field'
        'meta_query'    =>  array(
                                array(
                                    'key'     => '_sku', // assumed your meta_key is 'car_model'
                                    'value'   => $_strprt,
                                    'compare' => 'LIKE', // finds models that matches 'model' from the select field
                                ),
                            )
    );
$vehicleSearchQuery = new WP_Query( $v_args );

// Open this line to Debug what's query WP has just run
// var_dump($vehicleSearchQuery->request);

// Show the results
if( $vehicleSearchQuery->have_posts() ) :
    while( $vehicleSearchQuery->have_posts() ) : $vehicleSearchQuery->the_post();
        the_title(); // Assumed your cars' names are stored as a CPT post title
    endwhile;
else :
    _e( 'Sorry, nothing matched your search criteria', 'textdomain' );
endif;
wp_reset_postdata();
?>
</ul>

And the form that I utilize for the searcher from tickets-search.php:

<form method="get" id="tickets-search" role="search" action="<?php echo esc_url( home_url( '/' ) ); ?>">
    <div class ="row aligned">
    <h3><?php _e( 'Tickets Search', 'textdomain' ); ?></h3>

    <!-- PASSING THIS TO TRIGGER THE ADVANCED SEARCH RESULT PAGE FROM functions.php -->
    <input type="hidden" name="search" value="tickets">


    <label for="startingport" class="" required><?php _e( 'From: ', 'textdomain' ); ?></label><br>
    <select name="startingport" id="startingport" required>
        <option value=""><?php _e( 'From:', 'textdomain' ); ?></option>
        <option value="Ancona"><?php _e( 'Ancona', 'textdomain' ); ?></option>
        <option value="Igoumenitsa"><?php _e( 'Igoumenitsa', 'textdomain' ); ?></option>
    </select><br><br>

    <label for="endingport" class=""><?php _e( 'To: ', 'textdomain' ); ?></label><br>
    <select name="endingport" id="endingport" required>
        <option value=""><?php _e( 'To:', 'textdomain' ); ?></option>
        <option value="IgoumenitsaR"><?php _e( 'AnconaR', 'textdomain' ); ?></option>
        <option value="AnconaR"><?php _e( 'IgoumenitsaR', 'textdomain' ); ?></option>
    </select>
    <br><br>

    <label for="datepickah">Choose date</label><br>
    <input type="text" name="date" id="datepickah" required />
    <br><br>
    <div class="subm">
    <input type="submit" id="searchsubmit" value="tickets" />
    </div>
</div>
</form>

How can I achieve proper front-end "Products page" instead of just bringing the names of the products itself in a white background? I'm a newbie in PHP so any kind of help would be greatly appreciated. Thank you in advance!

PS:

Tried to find a plugin to achieve something like this but can't see any that suits my case. I also browsed a lot of questions posted here but can't seem to find also more help regarding the problem.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
    • ¥20 测距传感器数据手册i2c