Changeset 2892222
- Timestamp:
- 04/02/2023 03:07:44 PM (3 years ago)
- Location:
- count-of-products-in-one-category/trunk
- Files:
-
- 2 edited
-
count-of-products-in-category.php (modified) (4 diffs)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
count-of-products-in-one-category/trunk/count-of-products-in-category.php
r2891109 r2892222 4 4 * Plugin URI: https://plugin.wp.osowsky-webdesign.de/count-of-products 5 5 * Description: This plugin provides a shortcode that displays the count of products in a product category of woocommerce. You can use the shortcode on every page or post. IMPORTANT! This is clearly NOT an official plugin from Woocommerce. 6 * Version: 1.0.1 06 * Version: 1.0.11 7 7 * Requires at least: 5.8.0 8 8 * Requires PHP: 7.2 … … 13 13 * Author URI: https://osowsky-webdesign.de/ 14 14 */ 15 function so_category_product_count_shortcode( $atts ) { 16 global $wpdb; 17 $prefix = $wpdb->prefix; 15 18 16 function so_category_product_count_shortcode( $atts ) {17 19 // shortcode attributes 18 20 $atts = shortcode_atts( array( … … 23 25 // get category id 24 26 $category_id = get_term_by( 'slug', wp_strip_all_tags($atts['title']), 'product_cat' )->term_id; 25 27 26 28 // get products in category with stock greater than zero 27 $args = array( 28 'post_type' => 'product', 29 'tax_query' => array( 30 array( 31 'taxonomy' => 'product_cat', 32 'field' => 'term_id', 33 'terms' => $category_id, 34 ), 35 ), 36 'meta_query' => array( 37 array( 38 'key' => '_stock', 39 'value' => '0', 40 'compare' => '>' 41 ) 42 ) 43 ); 44 $products = new WP_Query( $args ); 29 $sql = "SELECT COUNT(*) AS product_count, pm.meta_value AS price 30 FROM {$prefix}posts p 31 INNER JOIN {$prefix}term_relationships tr ON p.ID = tr.object_id 32 INNER JOIN {$prefix}term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id 33 INNER JOIN {$prefix}terms t ON tt.term_id = t.term_id 34 INNER JOIN {$prefix}postmeta pm ON p.ID = pm.post_id 35 WHERE p.post_type = 'product' 36 AND t.slug = '".wp_strip_all_tags($atts['title'])."' 37 AND pm.meta_key = '_stock' 38 AND pm.meta_value > 0 39 GROUP BY pm.meta_value"; 40 41 $results = $wpdb->get_results($sql); 45 42 46 43 // build output string … … 49 46 $output .= get_term_by('slug', $atts['title'], 'product_cat')->name . ' '; 50 47 } 51 $output .= '(' . $products->post_count . ')'; 48 foreach ($results as $result) { 49 $output .= '(' . $result->product_count . ') '; 50 } 52 51 $output .= '</div>'; 53 52 -
count-of-products-in-one-category/trunk/readme.txt
r2891109 r2892222 4 4 Requires at least: 5.8.0 5 5 Tested up to: 6.2 6 Stable tag: 1.0.1 06 Stable tag: 1.0.11 7 7 License: GPLv3 or later 8 8 License URI: https://www.gnu.org/licenses/gpl-3.0.html … … 29 29 30 30 == Changelog == 31 = 1.0.11 = 32 fix stock 33 31 34 = 1.0.10 = 32 35 update "how to use"
Note: See TracChangeset
for help on using the changeset viewer.