M
M
makboriska2021-12-01 20:22:41
WooCommerce
makboriska, 2021-12-01 20:22:41

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

I just didn't try I can't get the subcategory of the product we found... help plz

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
makboriska, 2021-12-13
@makboriska

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

This code displays products and instead of a link to a product gives a link to a subcategory as it should)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question