在AJAX调用循环中返回自定义产品

时间:2019-02-15 作者:Oliver

从事Wordpress/Woocommerce架构的工作,我是Woocommerce的新手,在我从事django和laravel框架之前,该项目是一个插件wp,其中一个小表单适合选择标签,以便按sku返回一些产品。我可以获取产品对象,但我不能覆盖ajax响应返回的wp\\U查询,同时也不能用自定义数据覆盖归档循环模板并显示最终结果。

这是我的班级:

    if(!defined(\'EXCELREADER_ASSETS_URL\'))
    define(\'EXCELREADER_ASSETS_URL\', dirname(  FILE ));

class GetAllMenu {

public $tab;

public $id_product;

public function __construct(){
    add_action( \'search-phone\', array( $this, \'render_phone_search\' ) );
    add_action( \'wp_enqueue_scripts\', array( $this, \'init_plugin\' ) );
    add_action(\'wp_ajax_render_phone_search\', array($this,\'render_phone_search\'));         
    add_action(\'wp_ajax_nopriv_render_phone_search\', array($this,\'render_phone_search\')); 

    add_action( \'search-phone\', array( $this, \'display_search_result\' ) );
    add_action(\'wp_ajax_display_search_result\', array($this,\'display_search_result\'));         
    add_action(\'wp_ajax_nopriv_display_search_result\', array($this,\'display_search_result\')); 
}

public function init_plugin()
{
    wp_enqueue_script( 
        \'ajax_script\', 
        plugins_url( \'assets/js/admin.js\', EXCELREADER_ASSETS_URL ), 
        array(\'jquery\'), 
        TRUE 
    );
    wp_localize_script( 
        \'ajax_script\', 
        \'searchAjax\', 
        array(
            \'url\'   => admin_url(\'admin-ajax.php\'),
            \'nonce\' => wp_create_nonce( "search-phone" ),
        )
    );
}

public function render_phone_search(){
    $id = urldecode($_POST[\'phone_id\']);
   if(isset($id)){
        $objPhone = new ER_PhoneName();
        $phone = $objPhone->get_phones_by_mark(trim($id));
        include(EXCELREADER_TEMPLATE_URL . \'/search-phone-widget.php\');
   }
    exit;
}

public function render_mark_search(){
    $mark = new ER_PhoneMark();
    $data = $mark->call_all_mark_in_db();
    include(EXCELREADER_TEMPLATE_URL . \'/search-widget.php\');
}

public function display_search_result(){
    $sku = urldecode($_POST[\'sku_universel\']);
    $sku = explode(\',\',$sku);
    $this->tab = array();
    foreach($sku as $res){
        $product =  PhpSpreadReader::select_product_by_sku($res);
        if($product){
            $this->id_product[] = $product->get_id(); 
            $this->tab[] = $product;          
        }
    }
    $args = array(
        \'post_type\' => array(\'products\'),
        \'post_status\' => array(\'publish\'),
        \'post__in\' => $this->id_product
    );

    // The Query here is my problem =====================================
    $ajaxposts = new WP_Query( $args );

    $response = \'\';

    // The Query i want to use my archive product in my theme not this one
    if ( $ajaxposts->have_posts() ) {
        while ( $ajaxposts->have_posts() ) {
            $ajaxposts->the_post();
            $response .= get_template_part(\'woocommerce/content-product\');
        }
    } else {
        $response .= get_template_part(\'none\');
    }

    echo $response;

    //echo get_language_attributes();
    //echo $this->display_product_result();
exit;
}

public function changing_query_post($wp_query){
    $wp_query->posts;
    if ( $wp_query->is_home() && $wp_query->is_main_query() ) {
        $wp_query->set( \'post__in\', array($this->tab) );
    }
    return $wp_query;
}


public function render_search(){
    $this->render_mark_search();
}

}

Here is my ajax call in js :

$(document).on("click", "#ex-form-submit", (e) => { e.preventDefault() if($("#option-mark").val() === "0"){ return; } let str = { \'action\': \'display_search_result\', \'sku_universel\': $("#select-phone").val(), }; // console.log(searchAjax.url); $.ajax({ type : "post", dataType : "html", url : searchAjax.url, data : str, success: (response) => { $(\'.grilleproduits\').html(response).resize(); console.log(response); }, error : (error) => console.log(error) }); });
我可以在js中显示所有内容,但这不是一种正确的方式,因为在显示结果时会出现一些问题,例如产品标题翻译不符合要求。

提前感谢您的支持,并对我的nooby技能表示抱歉!我只在一周内回答,因为我没有周末考试的代码和网站。

编辑:我忘了说这是我正在开发的插件

2 个回复
最合适的回答,由SO网友:Antti Koskinen 整理而成

是的,您可以根据产品的ID查询产品,并将其显示为ajax响应。您的代码只需要稍微修改一下。也许你可以尝试一下这样的方法,

public function display_search_result(){ 

  $skus = urldecode($_POST[\'sku_universel\']); 
  $skus = explode(\',\',$sku);
  $product_ids = array();

  // you can use native woocommerce function to get the product ids
  foreach($skus as $sku){
    $product_id = wc_get_product_id_by_sku($sku);
    if ( is_int($product_id) ) { 
      $product_ids[] => $product_id;
    }
  } 

  $args = array( 
    \'post_type\'      => \'product\', // product, not products
    \'post_status\'    => \'publish\', 
    \'post__in\'       => $product_ids,
    \'posts_per_page\' => 100 // change this based on your needs
  );
  $ajaxposts = new WP_Query( $args );

  $response = \'\';

  if ( $ajaxposts->posts ){ 
    while ( $ajaxposts->have_posts() ) { 
      $ajaxposts->the_post(); 
      $response .= wc_get_template_part( \'content\', \'product\' ); // use WooCommerce function to get html
    } 
  } else { 
    // handle not found by yourself or
    // perhaps do_action( \'woocommerce_no_products_found\' ); could do the trick?
  }

  echo $response;
  exit;

}

SO网友:Oliver

好的,首先,谢谢你的帮助!您的wc\\u get\\u product\\u id\\u by\\u sku不起作用(始终返回null),因此我保留了我的上一个功能,我可以使用自定义查询和良好样式显示,只需最后一个问题我的导航菜单搜索显示了2次!我想我需要执行删除操作?

   public function display_search_result(){
    $sku = urldecode($_POST[\'sku_universel\']);
    $sku = explode(\',\',$sku);
    $this->tab = array();
    foreach($sku as $res){
        $product =  PhpSpreadReader::select_product_by_sku($res);
        if($product){
            $this->id_product[] = $product->get_id();       
        }
    }
    $args = array( 
        \'post_type\'      => \'product\', // product, not products
        \'post_status\'    => \'publish\', 
        \'post__in\'       => $this->id_product,
        \'posts_per_page\' => 100, // change this based on your needs
        \'page\' => \'1\'
      );
    // The Query
    $ajaxposts = new WP_Query( $args );

    $response = \'\';

    // The Query
    $response .= woocommerce_product_loop_start();

    if ( $ajaxposts->have_posts() ) {
        while ( $ajaxposts->have_posts() ) {
            $ajaxposts->the_post();
            $response .= wc_get_template_part( \'content\', \'product\' );
        }
    } else {
        $response .= get_template_part(\'none\');
    }

    $response .= woocommerce_product_loop_end();

    //$response .= get_template_part(\'/woocommerce/theme-custom/woo\', \'pagination\'); 

    echo $response;

    //echo get_language_attributes();
    //echo $this->display_product_result();
exit;
}
我的分页仍然不起作用,我继续搜索。。。

相关推荐

如何使用AJAX加载自定义帖子档案子页面?

我想保留/page/2/、/page/373/等的分页链接,但不是更改浏览器上的URL,而是希望所有内容都加载到存档主页上。理想情况下,所有分页的URL都应该重定向到归档文件的第一页。我不想要“加载更多”按钮,我想保持页面的逻辑,但通过ajax加载页面,保持url不变。我该怎么做?非常感谢您的帮助。