Answer the question
In order to leave comments, you need to log in
Woocommerce category link?
Given: A subcategory is a product
Task: ajax search for a product (subcategory)
There is already a js file that calls ajax, there is also a long-suffering php file with a search function
<?php
// ajax поиск по сайту
add_action('wp_ajax_nopriv_ajax_search', 'ajax_search');
add_action('wp_ajax_ajax_search', 'ajax_search');
function ajax_search()
{
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date',
's' => $_POST['term'],
'posts_per_page' => -1
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post(); ?>
<li class="codyshop-ajax-search">
<a href="" class="ajax-search__link"> // сюда необходимо вывести ССЫЛКУ НА ПОДКАТЕГОРИЮ ДАННОГО ТОВАРА
<?php echo get_the_post_thumbnail() ?>
<?php the_title(); ?></a>
</li>
<?php }
} else { ?>
<li class="ajax-search__item">
<div class="ajax-search__not-found">Ничего не найдено</div>
</li>
<?php }
exit;
}
Answer the question
In order to leave comments, you need to log in
God, I've been looking for an answer for 500 years, tried everything ... and everything turned out to be much simpler
// ajax поиск по сайту
add_action('wp_ajax_nopriv_ajax_search', 'ajax_search');
add_action('wp_ajax_ajax_search', 'ajax_search');
function ajax_search()
{
$product_cat = get_terms( $args );
$args = array(
'post_type' => 'product', // Тип записи: post, page, кастомный тип записи
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date',
's' => $_POST['term'],
'posts_per_page' => -1,
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$terms = get_terms(array(
'taxonomy' => 'product_cat',
'post_type' => 'product',
'hide_empty' => false,
'parent' => 0
));
$cat_link = get_category_link( $category_id );
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_id = $term->term_id;
break;
}
?>
<li class="codyshop-ajax-search">
<a href="<?php echo get_category_link( $product_cat_id ); ?>" class="ajax-search__link">
<?php echo get_the_post_thumbnail() ?>
<?php the_title(); ?></a>
</li>
<?php }
} else { ?>
<li class="ajax-search__item">
<div class="ajax-search__not-found">Not found</div>
</li>
<?php }
exit;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question