Plugin Directory

Changeset 2892222


Ignore:
Timestamp:
04/02/2023 03:07:44 PM (3 years ago)
Author:
s_i_l_l_e
Message:

fix stock

Location:
count-of-products-in-one-category/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • count-of-products-in-one-category/trunk/count-of-products-in-category.php

    r2891109 r2892222  
    44* Plugin URI: https://plugin.wp.osowsky-webdesign.de/count-of-products
    55* 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.10
     6* Version: 1.0.11
    77* Requires at least: 5.8.0
    88* Requires PHP:      7.2
     
    1313* Author URI: https://osowsky-webdesign.de/
    1414*/
     15function so_category_product_count_shortcode( $atts ) {
     16    global $wpdb;
     17    $prefix = $wpdb->prefix;
    1518
    16 function so_category_product_count_shortcode( $atts ) {
    1719    // shortcode attributes
    1820    $atts = shortcode_atts( array(
     
    2325    // get category id
    2426    $category_id = get_term_by( 'slug', wp_strip_all_tags($atts['title']), 'product_cat' )->term_id;
    25  
     27
    2628    // 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);
    4542 
    4643    // build output string
     
    4946        $output .= get_term_by('slug', $atts['title'], 'product_cat')->name . ' ';
    5047    }
    51     $output .= '(' . $products->post_count . ')';
     48    foreach ($results as $result) {
     49        $output .= '(' . $result->product_count . ') ';
     50    }
    5251    $output .= '</div>';
    5352 
  • count-of-products-in-one-category/trunk/readme.txt

    r2891109 r2892222  
    44Requires at least: 5.8.0
    55Tested up to: 6.2
    6 Stable tag: 1.0.10
     6Stable tag: 1.0.11
    77License: GPLv3 or later
    88License URI: https://www.gnu.org/licenses/gpl-3.0.html
     
    2929
    3030== Changelog ==
     31= 1.0.11 =
     32fix stock
     33
    3134= 1.0.10 =
    3235update "how to use"
Note: See TracChangeset for help on using the changeset viewer.