Created
October 7, 2020 11:45
-
-
Save git-bhanu/e4fb415042747135ea88a62dbd684980 to your computer and use it in GitHub Desktop.
AjaxSearch.php
This file contains 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
<?php | |
namespace App; | |
use App\Product; | |
use function Arrayy\create as a; | |
function categoryProductSearch() { | |
// check_ajax_referer('ajax-nonce', 'nonce'); | |
$maincategory = sanitize_text_field($_POST['mainCategory']); | |
$search_term = sanitize_text_field($_POST['search']); | |
$category = $_POST['selectedCategories']; | |
$colorsArray = $_POST['selectedColors']; | |
$sizesArray = $_POST['selectedSizes']; | |
$materialsArray = $_POST['selectedMaterial']; | |
$PriceArray = $_POST['selectedPrice']; | |
$offsetVal = $_POST['offset']; | |
if( $maincategory != 0) { | |
$getParent = Product::getParentCategoryfromaCategory($maincategory); | |
// Get Parent Name | |
$getParentTerm = get_term_by('id', $getParent, 'product_cat'); | |
$parent_name = $getParentTerm->name; | |
$getCategories = Product::getCategoriesFromParent($getParent); | |
} else { | |
$getParent = array(); | |
$getCategories = Product::getCategoriesFromSearchTerm( $search_term ,$getParent); | |
} | |
$cat_ids = array(); | |
array_push($cat_ids, $getParent); | |
$getColors = Product::getColors($search_term, $maincategory); | |
$getMaterials = Product::getMaterials($search_term, $maincategory); | |
$colors = array(); | |
$materials = array(); | |
$selectedcategory = array(); | |
if (isset($category)) { | |
foreach ($category as $cat) { | |
array_push($selectedcategory, $cat['id']); | |
} | |
} | |
$selectedcolors = array(); | |
if (isset($colorsArray)) { | |
foreach ($colorsArray as $color) { | |
array_push($selectedcolors, $color['id']); | |
} | |
} | |
$selectedSizes = array(); | |
if (isset($sizesArray)) { | |
foreach ($sizesArray as $size) { | |
array_push($selectedSizes, $size['id']); | |
} | |
} | |
$selectedmaterials = array(); | |
if (isset($materialsArray)) { | |
foreach ($materialsArray as $material) { | |
array_push($selectedmaterials, $material['id']); | |
} | |
} | |
$price_init = array( 100, 75000 ); | |
if (isset($PriceArray)) { | |
$price = $PriceArray; | |
} | |
$offset = 1; | |
if ($offsetVal) { | |
$offset = intval($offsetVal); | |
} | |
$product_per_page = 30; | |
$all_size_variations = | |
Product::getMainCategoriesSizes($search_term,$cat_ids); | |
$args = array( | |
's' => $search_term, | |
'post_type' => array('product', 'product_variation'), | |
'posts_per_page' => $product_per_page, | |
'post_status' => 'publish', | |
'offset' => $offset, | |
'tax_query' => array( | |
'relation' => 'AND', | |
array( | |
'taxonomy' => 'product_cat', | |
'field' => 'id', | |
'terms' => $cat_ids, | |
'include_children' => false, | |
'operator' => 'OR' | |
), | |
array( | |
'taxonomy' => 'product_cat', | |
'field' => 'id', | |
'terms' => $selectedcategory, | |
'include_children' => false, | |
'operator' => 'OR' | |
), | |
array( | |
'taxonomy' => 'color', | |
'field' => 'id', | |
'terms' => $selectedcolors, | |
'operator' => 'OR' | |
), | |
array( | |
'taxonomy' => 'material', | |
'field' => 'id', | |
'terms' => $selectedmaterials, | |
'operator' => 'OR' | |
), | |
array( | |
'taxonomy' => 'pa_size', | |
'field' => 'id', | |
'terms' => $selectedSizes, | |
'operator' => 'OR' | |
), | |
), | |
'meta_query' => array( | |
'relation' => 'OR', | |
array ( | |
'key' => '_price', | |
'compare' => 'NOT EXISTS', | |
'value' => '', | |
), | |
array( | |
'key' => '_price', | |
'value' => array( intval($price_init[0]), intval($price_init[1]) ), | |
'compare' => 'BETWEEN', | |
'type' => 'numeric', | |
), | |
), | |
); | |
$query = new \WP_Query($args); | |
ddd($query->request); | |
$query->parse_query($args); | |
$query->request; | |
relevanssi_do_query( $query ); | |
$result_count = $query->found_posts; | |
$products = a(array()); | |
$count = 0; | |
foreach ( $query->posts as $post ) { | |
$buffer = a(array()); | |
$id = $post->ID; | |
$product = Product::getProductData($id); | |
if (!$product) { | |
continue; | |
} | |
$product_name = Product::getProductName($product); | |
$product_price = Product::getProductPrice($product); | |
// d($product_name,get_post_meta($id, '_price', true)); | |
$price = $product->get_price(); | |
$featured_image = Product::getProductFeaturedImage($product); | |
$featured_image_small = Product::getProductFeaturedImageSmall($product); | |
$product_stock = Product::getStockStatus($product); | |
$product_url = Product::getPermalink($product); | |
$product_sizes = Product::getProductSizeStocks($product); | |
$collection = Product::getProductsCollectionName($product); | |
$gallery_image = a(Product::getProductGalleryImages($product)); | |
$attributes = Product::getAttributesForCard($product); | |
$card_type = Product::checkAttributesColorAndSize($product); | |
$gallery_image->prepend( array( | |
'image' => $featured_image, | |
'image_placeholder' => $featured_image_small | |
)); | |
$buffer->appendArrayValues([$id], 'id') | |
->appendArrayValues([$product_name], 'name') | |
->appendArrayValues([$gallery_image], 'images') | |
->appendArrayValues([$product_url], 'link') | |
->appendArrayValues([$product_sizes], 'sizes') | |
->appendArrayValues([$attributes], 'attributes') | |
->appendArrayValues([$product_stock], 'stock_status') | |
->appendArrayValues([$price], '_price') | |
->appendArrayValues([$count], '_rindex') | |
->appendArrayValues([$card_type], 'cardType') | |
->appendArrayValues([$collection], 'collection') | |
->appendArrayValues([$product_price], 'price'); | |
$products->append($buffer->toArray()); | |
$count = $count + 1; | |
} | |
wp_send_json_success( | |
array( | |
'products' => $products->toArray(), | |
'categories' => $getCategories, | |
'colors' => $getColors, | |
'sizes' => $all_size_variations, | |
'materials' => $getMaterials, | |
'category' => $category, | |
'parent_cat' => $getParent, | |
'count' => $result_count, | |
'product_per_page' => $product_per_page, | |
'parent_name' => $parent_name, | |
) | |
); | |
die(); | |
} | |
add_action('wp_ajax_categoryProductSearch', __NAMESPACE__ . '\\categoryProductSearch'); | |
add_action('wp_ajax_nopriv_categoryProductSearch', __NAMESPACE__ . '\\categoryProductSearch'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment