wickywills
9/1/2017 - 3:10 PM

fwp

fwp

<?php
/*
Template Name: Product archive
*/

use Timber\Timber;
use Lumberjack\PostTypes\Post;

$context = Timber::get_context();


/**
 * Filter bar
 */
$filterBarItemsArr[] = [
	"id" => "viewResultsAsMap",
	"title" => "View results as map",
	"link" => "#"
];

$context['filterBar'] = [
	"filters" => true,
	"items" => $filterBarItemsArr
];


/**
 * Products [.c-sub-cat-item]
 */
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 
	'posts_per_page' => get_option('posts_per_page'), 
	'paged' => $paged, 
	'post_type' => 'product',
	'orderby' => 'date',
	'order' => 'desc',
	'facetwp' => true
);
$cpt_query = new WP_Query($args);
$resultsCount = $cpt_query->found_posts;
$resultsHead = ($resultsCount == 1 ? " result" : " results");

if ($cpt_query->have_posts()) : while ($cpt_query->have_posts()) : $cpt_query->the_post();
	
	// Get all terms relating to the current post
	$currentProductCats = get_the_terms($post, 'product_category');
	$currentProductCatsClasses = [];
	$currentProductCatsNames = [];
	
	// Get the coordinates
	$currentProductCoords = get_field('exact_location');
	$currentProductLat = $currentProductCoords['lat'];
	$currentProductLng = $currentProductCoords['lng'];
	

	// Build up an array of category ID's (just in case there are more than one on the current product)
	if( is_array($currentProductCats) ) : foreach ($currentProductCats as $currentProductCat) :
		array_push($currentProductCatsClasses, 'e-pill-tag--category-'.str_replace(' ', '-', strtolower($currentProductCat->name)));
		array_push($currentProductCatsNames, $currentProductCat->name);
	endforeach; endif;

	$context['subCatItems'][] = [
		"image"				=> "http://via.placeholder.com/280x280",
		"title"				=> get_the_title(),
		"location"			=> get_field('location'),
		"description"		=> get_field('short_description'),
		"categorySlug"		=> implode(" ", $currentProductCatsClasses),
		"categoryTitle"		=> implode(", ", $currentProductCatsNames)
	];
	
	$context['markers'][] = [
		"lat"				=> $currentProductLat,
		"lng"				=> $currentProductLng,
		"type"				=> str_replace(' ', '-', strtolower($currentProductCat->name)),
		"content"			=> strip_tags(get_field('short_description')),
		"contentImage"		=> "http://via.placeholder.com/280x280",
		"categorySlug"		=> implode(" ", $currentProductCatsClasses),
		"categoryTitle"		=> implode(", ", $currentProductCatsNames)
	];
	
endwhile; endif;

function doPagination($cpt_query){
	wp_pagenavi( array( 'query' => $cpt_query ) ); 
}

ob_start();
doPagination($cpt_query);
$context['pagination'] = ob_get_contents();
ob_end_clean();


/**
 * Banner [.c-banner]
 */
$context['banner'] = [
	"heading1"		=> "Visit Winchester",
	"heading2"		=> $resultsCount . $resultsHead,
	"image"			=> "/app/uploads/2017/08/banner-image-compressor.jpg",
	"bannerClasses"	=> array( "c-banner--fixed" )
];


/**
 * Faceted search
 */
$context['facetwp'] = do_shortcode('[facetwp facet="by_location"]');

Timber::render(['archive-product.twig'], $context);