Created
August 27, 2021 19:42
-
-
Save ayoub-bousetta/adb07f95b3d49a56711d939a66e0a786 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///search_bar | |
add_action( 'wp_ajax_load_search_res', 'lamacom_search_bar' ); | |
add_action( 'wp_ajax_nopriv_load_search_res', 'lamacom_search_bar' ); | |
function lamacom_search_bar() { | |
if (isset($_POST['datainit'])) { | |
$dataInfo=[]; | |
$data=$_POST['datainit']; | |
if (isset($data['type']) && $data['type'] !="") { | |
//recettes | |
if ($data['type']=="recettes") { | |
$args = array( | |
'post_type' => 'recettes', | |
'post_status' => 'publish', | |
'posts_per_page' => -1, | |
"s"=>sanitize_text_field($data['valueInput']), | |
); | |
}else{ | |
//produits | |
//Query | |
$q1 = get_posts(array( | |
'fields' => 'ids', | |
'post_type' => 'product', | |
"s"=>sanitize_text_field($data['valueInput']), | |
)); | |
//SKU and Metas | |
$q2 = get_posts(array( | |
'fields' => 'ids', | |
'post_type' => 'product', | |
'meta_query' => array( | |
array( | |
'key' => '_sku', | |
'value' => $data['valueInput'], | |
'compare' => 'LIKE' | |
) | |
) | |
)); | |
$unique = array_unique( array_merge( $q1, $q2 ) ); | |
$args = array( | |
'post_type' => 'product', | |
'post__in' => $unique, | |
'post_status' => 'publish', | |
'posts_per_page' => 4, | |
'numberposts' => 4, | |
); | |
} | |
} | |
$get_posts = new WP_Query( $args ); | |
$count_post=$get_posts->found_posts; | |
$posts = $get_posts->query($args); | |
foreach ($posts as $key => $value) { | |
if($data['type']=="produits"){ | |
$productyet=wc_get_product( $value->ID); | |
$taxonomy = 'series'; | |
$taxonomyseries = wp_get_post_terms( $productyet->get_id(), $taxonomy ); | |
$thumbnail = get_the_post_thumbnail_url($value->ID); | |
}else{ | |
$productyet=""; | |
$taxonomyseries=""; | |
$thumbnail =get_field( 'miniature',$value->ID ); | |
$thumbnail =$thumbnail['url']; | |
} | |
$taxonomy = 'series'; // <== Here set your custom taxonomy | |
$dataInfo[]=[ | |
'name'=>$value->post_title, | |
'id'=>$value->ID, | |
'url'=>get_the_permalink($value->ID), | |
'price'=>$productyet!="" ? $productyet->get_price_html() :"", | |
'sku'=>$productyet!="" ? $productyet->get_sku() :"", | |
'img'=>$thumbnail, | |
'series'=>$taxonomyseries!="" ? ucfirst($taxonomyseries[0]->name) :"", | |
]; | |
} | |
} | |
echo json_encode(['data'=>$dataInfo,'count'=>$count_post]); | |
wp_die(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment