Return custom product in ajax call loop

Question

Working on WordPress / Woocommerce architecture,
I’m new in woocommerce, before i’ve worked on django and laravel framework, the project is a plugin wp, where a little form is fit with select tags for give me back some products by their sku.
I can get the products objects, but I can’t override the wp_query returned by the ajax response and, at the same time override the archive loop template with my custom datas and display my final result.

Here is my class :

    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)
});
});

I can display all in js but it’s not a right way to do this, cause some problems like the product title translation don’t follow when the result is displayed.

Thanks in advance for any support and sorry for my nooby skills ! I answer only during week cause i don’t have the code and website for my test in week end.

edit : I forgot to say it’s a plugin that i’m developing

0
, , , Oliver 4 years 2019-02-15T10:11:31-05:00 0 Answers 103 views 0

Leave an answer

Browse
Browse