Output all products
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 16 Dec 2015 23:53:33 +0000 (18:53 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 16 Dec 2015 23:53:33 +0000 (18:53 -0500)
add category drop down

index.php
views/list_categories.php
views/list_products.php

index 9bcea20..dc59557 100644 (file)
--- a/index.php
+++ b/index.php
@@ -158,7 +158,7 @@ function michsci_taxonomy_filter_post_type_request( $query )
             }
         }
     }
-    
+
 }
 
 add_filter( 'parse_query', 'michsci_taxonomy_filter_post_type_request' );
@@ -171,15 +171,18 @@ function michsci_shortcode($atts)
     extract( shortcode_atts( array( 'limit' => '10' ), $atts ) );
     ob_start();
 
-    if ( $product_id = filter_var( $_REQUEST['product'] ) ) {
+    echo '<form action=""><div class="row"><div class="small-12 medium-9 columns">';
+    echo michsci_get_terms_dropdown();
+    echo '</div><div class="small-12 medium-3 columns"><input type="submit" value="search" class="button"></div></div>';
+    echo '</form>';
+
+    if ( $product_id = filter_var( $_REQUEST['product'], FILTER_VALIDATE_INT ) ) {
         michsci_show_product($product_id);
-    } if($cat_id = filter_var( $_REQUEST['term'])) {
-        michsci_show_category($cat_id);
-    }
-    else {
+    } else if ( $cat_id = filter_var( $_REQUEST['category'], FILTER_VALIDATE_INT ) ) {
+        //michsci_list_categories($cat_id);
+    } else if ( !$catid_id && !$product_id ) {
         michsci_list_products();
-        michsci_list_categories();
+        //michsci_list_categories();
     }
 
     $output = ob_get_contents();
@@ -187,6 +190,28 @@ function michsci_shortcode($atts)
     return $output;
 }
 
+function michsci_get_terms_dropdown()
+{
+    $myterms = get_terms('michsci_category', array(
+            'orderby'    => 'name',
+            'hide_empty' => true
+        ));
+    $output ="<select name=\"michsci_category\"><option value=\"\">Select Category</option>";
+    foreach ( $myterms as $term ) {
+        $term_taxonomy = $term->taxonomy;
+        $term_slug = $term->slug;
+        $term_name = $term->name;
+        $link = $root_url.'/'.$term_taxonomy.'/'.$term_slug;
+        $output .= '<option value="' . $term->term_id . '"';
+        if ($_REQUEST['michsci_category'] == $term->term_id) {
+             $output .= ' selected';
+        }
+        $output .= '>' . $term_name . '</option>';
+    }
+    $output .="</select>";
+    return $output;
+}
+
 function michsci_list_products()
 {
     global $wpdb, $wp;
@@ -216,6 +241,9 @@ function michsci_list_products()
     $iterator      = 1;
 
     foreach ( $products as $product ) {
+        $productTerms = wp_get_post_terms( $product->ID, 'michsci_category', 'name' );
+        $pTerm = $productTerms[0];
+        $product->term = $pTerm->name;
         $product->end = false;
         if ( $iterator == $totalProducts ) {
             $product->end = true;
@@ -225,20 +253,27 @@ function michsci_list_products()
             . "product=" . $product->ID;
         ++$iterator;
     }
-
+    //echo '<pre>' . print_r($products, true) . '</pre>';
     include 'views/list_products.php';
     return;
 }
-function michsci_list_categories()
+function michsci_list_categories( $cat_id = null )
 {
     global $wpdb, $wp;
 
     $taxonomy = 'michsci_category';
-    $taxonomy_terms = get_terms( $taxonomy, array(
-    'hide_empty' => false,
-        'parent'=> 0) );
+    $args = array(
+        'hide_empty' => true,
+        'pad_counts' => true
+    );
+    if ( $cat_id ) {
+        $args['child_of'] = $cat_id;
+    } else {
+        $args['parent'] = 0;
+    }
+    $taxonomy_terms = get_terms( $taxonomy, $args );
+
 
-  
     $totalTerms = count( $taxonomy_terms);
     $current_url   = esc_url( add_query_arg( $wp->query_string, '', home_url( $wp->request ) ) );
     $iterator      = 1;
@@ -268,7 +303,16 @@ function michsci_show_product($prod_id)
 function michsci_show_category($cat_id)
 {
     global $wpdb, $wp;
-    $term = get_terms( $cat_id );
+    $term = get_terms(
+        'michsci_category',
+        array(
+            'child_of'     => $cat_id,
+            'hide_empty'   => true,
+            //'parent'       => $catid_id,
+            //'hierarchical' => false
+        )
+    );
+    echo '<pre>' . print_r( $term, true ) . '</pre>';
     include 'views/category_detail.php';
     return;
 }
index 3220d78..3451119 100644 (file)
@@ -1,12 +1,22 @@
-  
+
 <div class="row">
-    <?php if ( isset( $taxonomy_terms) && is_array( $taxonomy_terms ) ) : foreach ( $taxonomy_terms  as $term ) : ?>
+
+<?php
+    if ( isset( $taxonomy_terms) && is_array( $taxonomy_terms ) ) :
+        foreach ( $taxonomy_terms  as $term ) : ?>
+    <pre><?php  //echo print_r( $term, true ) ?></pre>
         <div class="small-12 medium-3 columns<?php if ($term->end){ echo ' end';}?>">
-        <a href="<?php echo $term->href; ?>">
-                <?php echo $term->name; ?>
-            </a>
+            <div class="row">
+                <div class="small-12 columns"><a href="<?php echo $term->href; ?>">
+            <?php echo $term->description ?></a>
+                </div>
+                <div class="small-12 text-center columns">
+            <a href="<?php echo $term->href; ?>"> <?php echo $term->name; ?> </a>
+                </div>
+            </div>
         </div>
-    <?php endforeach; endif; ?>
-         
+    <?php
+        endforeach;
+    endif; ?>
+
 </div>
index c0d4a36..a2f31a5 100644 (file)
@@ -1,14 +1,18 @@
 <div class="row">
     <?php if ( isset( $products ) && is_array( $products ) ) : foreach ( $products as $product ) : ?>
         <div class="small-12 medium-3 columns<?php if ($product->end){ echo ' end';}?>">
-        <a href="<?php echo $product->href; ?>">
-                <?php echo $product->post_title; ?>
-           
-            </a>
-            
+            <div class="row">
+                <div class="small-12 columns"><a href="<?php echo $product->href; ?>">
+            <?php echo get_the_post_thumbnail( $product->ID, 'post-thumbnail' ); ?></a>
+                </div>
+                <div class="small-12 columns">
+                    <a href="<?php echo $product->href; ?>"><?php echo $product->term; ?></a>
+                </div>
+                <div class="small-12 columns">
+                    <a href="<?php echo $product->href; ?>"><strong><?php echo $product->post_title; ?></strong></a>
+                </div>
+            </div>
         </div>
     <?php endforeach; endif; ?>
-   
-     
 </div>