Skip to content

Instantly share code, notes, and snippets.

@nixonmedia
Created April 18, 2018 16:27
Show Gist options
  • Save nixonmedia/584c440ef000f2168385d4be51b5d8e8 to your computer and use it in GitHub Desktop.
Save nixonmedia/584c440ef000f2168385d4be51b5d8e8 to your computer and use it in GitHub Desktop.
<?php
$grouped = [];
function clean($string) {
return str_replace('*', '', $string);
}
function sortMyThings($things) {
uasort($things, function($left, $right) {
$left = strtoupper(clean($left['title']));
$right = strtoupper(clean($right['title']));
if ($left == $right) return 0;
return $left < $right? -1 : 1;
});
return $things;
}
//sort($package);
sortMyThings($package);
foreach ($package as $prod) {
$manu = $prod['manufacturer'];
$brand = $prod['brand'];
$item = $prod['type'];
$pid = $prod['id'];
$title = $prod['title'];
if (! isset($grouped[ $manu ])) {
$grouped[ $manu ] = [
'manufacturer' => $manu,
'brands' => [],
];
}
if ( ! isset($grouped[ $manu ]['brands'][ $brand ])) {
$grouped[ $manu ]['brands'][ $brand ] = [
'brand' => $brand,
'items' => [],
];
}
if ( ! isset($grouped[ $manu ]['brands'][ $brand ]['items'][$item])) {
$grouped[ $manu ]['brands'][ $brand ]['items'][]= [
'id' =>$pid,
'rid' =>$rid,
'name' => $item,
'title'=>$title
];
}
//$grouped[ $manu ]['brands'][ $brand ]['items'] []= $item;
}
?>
<?php
//echo '<pre>';
//print_r($grouped);
//echo '</pre>';
?>
<?php foreach ($grouped as $manu): ?>
<h2><?php echo $manu['manufacturer'] ?></h2>
<?php foreach ($manu['brands'] as $brand): ?>
<h3><?php echo $brand['brand']; ?></h3>
<ul class="tprods testtest">
<?php foreach ($brand['items'] as $item):
$expiration_date = get_field('expiration_date',$item['id']);
//$post_status = get_post_status($item['id']);
?>
<?php
//if a thumbnail exists
if (!has_post_thumbnail( $item['id'] ) ){?>
<li class="tprod" data-id="<?php echo $item['id']; ?>">
<span><?php if(!empty($item['name'])){
echo $item['name'];
}else{
echo $item['title'];
} ?>
</span>
<i class="fas fa-camera"></i>
<?php
if($expiration_date){
if( strtotime($expiration_date) < strtotime('+21 days') ){
$date = date_create($expiration_date);
echo '<span class="expires float-right">Expires: '.date_format($date,'F d, Y').'</span>';
}
}
?>
</li>
<?php
// no thumbnail
}else{
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $item['id'] ), "large" );
$image = $image[0];
?>
<li class="tprod active">
<a href="#"
data-toggle="modal"
data-target="#tobaccoModal"
data-title="<?php echo $brand['brand'] . ' ' . $item['name']; ?>"
data-image="<?php echo $image; ?>"
>
<span><?php echo $item['name']; ?></span> <i class="fas fa-camera"></i>
</a>
</li>
<?php } ?>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
<?php endforeach; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment