Skip to content

Commit

Permalink
Reverted changes for manual category count. Ref: #1789
Browse files Browse the repository at this point in the history
  • Loading branch information
coenjacobs committed Nov 19, 2012
1 parent 4720628 commit 22afc34
Show file tree
Hide file tree
Showing 6 changed files with 2 additions and 106 deletions.
36 changes: 1 addition & 35 deletions admin/includes/updates/woocommerce-update-1.7.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,38 +206,4 @@
unset( $tax_amount );
}
}
}

// Update manual counters for product categories (only counting visible products, so visibility: 'visible' or 'catalog')
// Loop through all products and put the IDs in array with each product category term id as index
$products_query_args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array( 'visible', 'catalog' ),
'compare' => 'IN',
),
),
);

$products_query = new WP_Query( $products_query_args );

$counted_ids = array();

foreach( $products_query->posts as $visible_product ) {
$product_terms = wp_get_post_terms( $visible_product->ID, 'product_cat', array( 'fields' => 'ids' ) );

foreach ( $product_terms as $product_term_id ) {
if ( ! isset( $counted_ids[ $product_term_id ] ) || ! is_array( $counted_ids[ $product_term_id ] ) ) {
$counted_ids[ $product_term_id ] = array();
}

if ( ! in_array( $visible_product->ID, $counted_ids[ $product_term_id ] ) ) {
array_push( $counted_ids[ $product_term_id ], $visible_product->ID );
}
}
}

update_option( 'wc_prod_cat_counts', $counted_ids );
}
1 change: 0 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc
* Fix - Added more error messages for coupons.
* Fix - Variation sku updating after selection.
* Fix - Active plugins display on status page.
* Fix - Manual product category counting to make sure hidden products are not counted.

* Localization - French update by Arnaud Cheminand and absoluteweb.
* Localization - Romanian update by silviu-bucsa.
Expand Down
42 changes: 0 additions & 42 deletions woocommerce-core-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1702,48 +1702,6 @@ function woocommerce_remove_roles() {
}
}

/**
* Manual category counting to prevent category widget from showing hidden products in total
*
* @access public
* @return void
*/
function woocommerce_manual_category_count( $terms, $taxonomy ) {
// Keep the normal count in sync
_update_post_term_count( $terms, $taxonomy );

if ( isset( $_POST['post_ID'] ) && isset( $_POST['_visibility'] ) && 'product_cat' == $taxonomy->query_var ) {
foreach ( $terms as $term_id ) {
$do_count = array( 'visible', 'catalog' );
$do_not_count = array( 'search', 'hidden' );

$counted_ids = get_option( 'wc_prod_cat_counts' );
if ( ! is_array( $counted_ids ) )
$counted_ids = array();
$counted_ids[ $term_id ] = ( empty( $counted_ids[ $term_id ] ) || ! is_array( $counted_ids[ $term_id ] ) ) ? array() : $counted_ids[ $term_id ];

if ( in_array( $_POST['_visibility'], $do_count ) ) {
if ( ! empty( $counted_ids[ $term_id ] ) ) {
if ( ! in_array( $_POST['post_ID'], $counted_ids[ $term_id ] ) ) {
array_push( $counted_ids[ $term_id ], absint( $_POST['post_ID'] ) );
update_option( 'wc_prod_cat_counts', $counted_ids );
}
} else {
$counted_ids[ $term_id ] = array( absint( $_POST['post_ID'] ) );
update_option( 'wc_prod_cat_counts', $counted_ids );
}
} elseif ( in_array( $_POST['_visibility'], $do_not_count ) ) {
if ( in_array( $_POST['post_ID'], $counted_ids[ $term_id ] ) ) {
if ( ( $key = array_search( $_POST['post_ID'], $counted_ids[ $term_id ] ) ) !== false ) {
unset( $counted_ids[ $term_id ][ $key ] );
update_option( 'wc_prod_cat_counts', $counted_ids );
}
}
}
}
}
}


/**
* Add a item to an order (for example a line item).
Expand Down
20 changes: 0 additions & 20 deletions woocommerce-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1226,24 +1226,4 @@ function woocommerce_get_order_id_by_order_key( $order_key ) {
$order_id = $wpdb->get_var( "SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_key = '_order_key' AND meta_value = '{$order_key}'" );

return $order_id;
}

/**
* Change the count properties for all terms based on our manual counters
*
* @access public
* @return array Contains all the terms with updated counts
*/
function wc_get_terms_count_filter( $terms, $taxonomies ) {
if ( ! is_admin() && in_array( 'product_cat', $taxonomies ) ) {
$counted_ids = get_option( 'wc_prod_cat_counts' );

foreach ( $terms as $term ) {
if ( isset( $counted_ids[ $term->term_id ] ) ) {
$term->count = count( $counted_ids[ $term->term_id ] );
}
}
}

return $terms;
}
7 changes: 0 additions & 7 deletions woocommerce-hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,6 @@
add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );

/**
* Categories
*
* @see wc_get_terms_count_filter()
*/
add_filter( 'get_terms', 'wc_get_terms_count_filter', 10, 2 );

/**
* Subcategories
*
Expand Down
2 changes: 1 addition & 1 deletion woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ function init_taxonomy() {
array('product'),
array(
'hierarchical' => true,
'update_count_callback' => 'woocommerce_manual_category_count',
'update_count_callback' => '_update_post_term_count',
'label' => __( 'Product Categories', 'woocommerce'),
'labels' => array(
'name' => __( 'Product Categories', 'woocommerce'),
Expand Down

0 comments on commit 22afc34

Please sign in to comment.