Exemplo n.º 1
0
    if (has_post_thumbnail($random_cat_query->post->ID)) {
        echo get_the_post_thumbnail($random_cat_query->post->ID, 'medium', array('class' => '[ block center ]'));
    } else {
        echo '<img class="[ block center ]" src="' . woocommerce_placeholder_img_src() . '" alt="Placeholder" />';
    }
    ?>
							</div>
						</div>

						<h4 class="[ text-center ][ ellipsis ][ no-margin ]"><?php 
    the_title();
    ?>
</h4>

						<p class="[ text-center ]">de <a href="#"><?php 
    echo get_seller_name(get_the_author_meta('ID'));
    ?>
						</a></p>
						<h5 class="[ text-center ][ product-price ]">
							<?php 
    echo $product->get_price_html();
    ?>
						</h5>
						<div class="[ product-full-description ][ bg-primary ]">
							<h5 class="[ text-center ][ margin-bottom--small ]">
								<?php 
    the_title();
    ?>
							</h5>
							<p class="[ text-center ][ product-price ][ color-light ][ margin-bottom--small ]">
								<?php 
Exemplo n.º 2
0
/**
 * Búsqueda avanzada basada en filtros.
 * @param  string $string
 * @return string
 */
function filter_products()
{
    $category = $_POST['category'];
    $subcategories = isset($_POST['subcategories']) ? $_POST['subcategories'] : '';
    $states = isset($_POST['states']) ? $_POST['states'] : '';
    $colors = isset($_POST['colors']) ? $_POST['colors'] : '';
    $patterns = isset($_POST['patterns']) ? $_POST['patterns'] : '';
    $materials = isset($_POST['materials']) ? $_POST['materials'] : '';
    $price_range = $_POST['price_range'];
    $filter_products_results = array();
    $args = array('posts_per_page' => -1, 'post_type' => 'product', 'tax_query' => array('relation' => 'AND'));
    // Filter by category or subcategory
    $tax_query_index = 0;
    if ('' !== $subcategories) {
        foreach ($subcategories as $key => $subcat) {
            if ($key === 0) {
                array_push($args['tax_query'], array('relation' => 'OR'));
            }
            $subcat_arr = array('taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => $subcat);
            array_push($args['tax_query'][$tax_query_index], $subcat_arr);
        }
    } else {
        $category_arr = array('taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => $category);
        array_push($args['tax_query'], $category_arr);
    }
    // Filter by color
    $tax_query_index++;
    if ('' !== $colors) {
        foreach ($colors as $key => $color) {
            if ($key == 0) {
                array_push($args['tax_query'], array('relation' => 'OR'));
            }
            $color_arr = array('taxonomy' => 'pa_color', 'field' => 'slug', 'terms' => $color);
            array_push($args['tax_query'][$tax_query_index], $color_arr);
        }
        $tax_query_index++;
    }
    // Filter by state
    if ('' !== $states) {
        foreach ($states as $key => $state) {
            if ($key == 0) {
                array_push($args['tax_query'], array('relation' => 'OR'));
            }
            $state_arr = array('taxonomy' => 'pa_estado', 'field' => 'slug', 'terms' => $state);
            array_push($args['tax_query'][$tax_query_index], $state_arr);
        }
        $tax_query_index++;
    }
    // Filter by pattern
    if ('' !== $patterns) {
        foreach ($patterns as $key => $pattern) {
            if ($key == 0) {
                array_push($args['tax_query'], array('relation' => 'OR'));
            }
            $pattern_arr = array('taxonomy' => 'pa_patron', 'field' => 'slug', 'terms' => $pattern);
            array_push($args['tax_query'][$tax_query_index], $pattern_arr);
        }
        $tax_query_index++;
    }
    // Filter by material
    if ('' !== $materials) {
        foreach ($materials as $key => $pattern) {
            if ($key == 0) {
                array_push($args['tax_query'], array('relation' => 'OR'));
            }
            $pattern_arr = array('taxonomy' => 'pa_material', 'field' => 'slug', 'terms' => $pattern);
            array_push($args['tax_query'][$tax_query_index], $pattern_arr);
        }
        $tax_query_index++;
    }
    // Filter by price
    $args['meta_query'] = array(array('key' => '_price', 'value' => $price_range, 'compare' => 'BETWEEN', 'type' => 'numeric'));
    $query = new WP_Query($args);
    if (!$query->have_posts()) {
        $msg = array('error' => 1);
        echo json_encode(0, JSON_FORCE_OBJECT);
        exit;
    }
    $products = array();
    $i = 0;
    while ($query->have_posts()) {
        $query->the_post();
        global $product;
        global $post;
        $product_image_url = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full');
        $products[$i] = array('product_image_url' => $product_image_url[0], 'title' => get_the_title(), 'seller_name' => get_seller_name(get_the_author_meta('ID')), 'price_html' => $product->get_price_html(), 'permalink' => get_permalink());
        $i++;
    }
    echo json_encode($products, JSON_FORCE_OBJECT);
    die;
}