“woocommerce query get current category id”
Problem:
The need to create a widget which can show a list of woocommerce products in same category as current product.
Solution:
- add this code to the widgets.php
- register your widget
- drag your widget in sidebar back-end
- add a css active item color
- done.
theme/_includes/widgets.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
// ***************************************** // ************** Custom widget ************ // ***************************************** class Products_in_category extends WP_Widget{ public function __construct() { parent::__construct('products_in_category', 'Products in category'); } public function widget( $args, $instance ) { echo $args['before_widget'].' <hr class="first"/>'; // start $this->_products($instance); echo $args['after_widget']; //end } /*---------------------------------------- update() ---------------------------------------- * Function to update the settings from * the form() function. * Params: * - Array $new_instance * - Array $old_instance ----------------------------------------*/ function update ( $new_instance, $old_instance ) { // var_dump($new_instance); $instance = $old_instance; $instance['title'] = empty($new_instance['title']) ? '' : $new_instance['title']; $instance['product_count'] = empty($new_instance['product_count']) ? 0 : (int) $new_instance['product_count']; return $instance; } /*---------------------------------------- form() ---------------------------------------- * The form on the widget control in the * widget administration area. * Make use of the get_field_id() and * get_field_name() function when creating * your form elements. This handles the confusing stuff. * Params: * - Array $instance ----------------------------------------*/ function form ( $instance ) { $product_count = isset($instance['product_count']) ? $instance['product_count'] : 0; $title = isset($instance['title']) ? $instance['title'] : ''; ?> <!-- Widget Title: Text Input --> <p> <label for="<?= $this->get_field_id( 'product_count' ); ?>"> Title </label> <input type="text" name="<?= $this->get_field_name( 'title' ); ?>" value="<?= $title; ?>" class="widefat" id="<?= $this->get_field_id( 'title' ); ?>" /> </p> <!-- Widget text: Text Input --> <p> <label for="<?= $this->get_field_id( 'product_count' ); ?>"> Number of Products To Show </label> <input type="text" name="<?= $this->get_field_name( 'product_count' ); ?>" value="<?= $product_count; ?>" class="widefat" id="<?= $this->get_field_id( 'product_count' ); ?>" /> </p> <?php } /** * Products in category * * @author WooThemes * @package WooCommerce/Templates * @version 1.6.4 */ private function _products($instance) { if ( is_singular('product') ) { global $post; $post_ID = $post->ID; // get categories $terms = wp_get_post_terms( $post->ID, 'product_cat' ); foreach ( $terms as $term ) $cats_array[] = $term->term_id; $query_args = array( // 'post__not_in' => array( $post->ID ), 'posts_per_page' => 20, 'no_found_rows' => 1, 'post_status' => 'publish', 'orderby' => 'title', 'order' => 'ASC', 'post_type' => 'product', 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'id', 'terms' => $cats_array ))); $r = new WP_Query($query_args); if ($r->have_posts()) { ?> <ul class="product_list_widget"> <?php while ($r->have_posts()) : $r->the_post(); global $product; ?> <li> <a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>" class="<?php if(get_the_ID() == $post_ID) echo "active"; ?>"> <?php if ( get_the_title() ) the_title(); else the_ID(); ?> </a> </li> <?php endwhile; ?> </ul> <?php // Reset the global $the_post as this query will have stomped on it wp_reset_query(); } } } } // class end register your widget register_widget( 'Products_in_category' ); CSS .product_list_widget a.active { color: #f15a29; } |