Last active
December 13, 2023 14:17
-
-
Save wpserve/6bb6d1c7141f23b058a1ca726ca57372 to your computer and use it in GitHub Desktop.
In stock product variations by default
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* THIS CODE IS NO LONGER WORKS | |
* PLEASE USE THE FILTER EVERYTHING PRO VERSION 1.7.15.6 OR ABOVE | |
*/ | |
/** | |
* Makes only In stock products visible in filtering process | |
* You can put this code in the end of the functions.php file | |
* of your active theme | |
add_action( 'wpc_filtered_query_end', 'flrt_display_products_in_stock_only' ); | |
add_filter( 'wpc_variations_meta_query', 'flrt_display_variations_in_stock_only' ); | |
add_action( 'wpc_all_set_wp_queried_posts', 'flrt_all_set_wp_queried_in_stock_only' ); | |
function flrt_display_products_in_stock_only( $filtered_query ) { | |
if ( $filtered_query->get('wc_query') === 'product_query' || $filtered_query->get('post_type') === 'product' ) { | |
$meta_query = $filtered_query->get('meta_query'); | |
$add_in_stock = true; | |
if ( ! empty( $meta_query ) ) { | |
foreach( $meta_query as $sub_query ) { | |
if ( isset( $sub_query['key'] ) && $sub_query['key'] === '_stock_status' ) { | |
$add_in_stock = false; | |
break; | |
} | |
} | |
} | |
//@todo consider to relate with WooCommerce 'woocommerce_hide_out_of_stock_items' option value | |
if ( $add_in_stock ) { | |
$meta_query[] = array( | |
'key' => '_stock_status', | |
'value' => 'instock', | |
'compare' => 'IN', | |
); | |
if ( count ( $meta_query ) > 1 ) { | |
$meta_query['relation'] = 'AND'; | |
} | |
$filtered_query->set('meta_query', $meta_query ); | |
} | |
} | |
return $filtered_query; | |
} | |
function flrt_display_variations_in_stock_only( $separated_queries ) { | |
$products_in_stock_query_exists = false; | |
if ( isset( $separated_queries['for_products'] ) ) { | |
foreach ( $separated_queries['for_products'] as $products_meta_query ) { | |
if ( isset( $products_meta_query['key'] ) && $products_meta_query['key'] === '_stock_status' ) { | |
$products_in_stock_query_exists = true; | |
break; | |
} | |
} | |
} | |
$variations_in_stock_query_exists = false; | |
if ( isset( $separated_queries['for_variations'] ) ) { | |
foreach ( $separated_queries['for_variations'] as $products_meta_query ) { | |
if ( isset( $products_meta_query['key'] ) && $products_meta_query['key'] === '_stock_status' ) { | |
$variations_in_stock_query_exists = true; | |
break; | |
} | |
} | |
} | |
//@todo consider to relate with WooCommerce 'woocommerce_hide_out_of_stock_items' option value | |
if ( $products_in_stock_query_exists && ! $variations_in_stock_query_exists ) { | |
$separated_queries['for_variations'] = array( | |
array( | |
'key' => '_stock_status', | |
'value' => array( 'instock' ), | |
'compare' => "IN" | |
) | |
); | |
$separated_queries['for_products'] = array(); | |
} | |
return $separated_queries; | |
} | |
function flrt_all_set_wp_queried_in_stock_only( $set_wp_query ){ | |
/** | |
* Set In Stock products by default | |
* For correct term count calculations | |
* | |
if ( $set_wp_query->get('wc_query') === 'product_query' || $set_wp_query->get('post_type') === 'product' ) { | |
$meta_query = $set_wp_query->get( 'meta_query' ); | |
$add_in_stock = true; | |
if ( ! empty( $meta_query ) ) { | |
foreach( $meta_query as $sub_query ) { | |
if ( isset( $sub_query['key'] ) && $sub_query['key'] === '_stock_status' ) { | |
$add_in_stock = false; | |
break; | |
} | |
} | |
} | |
if ( $add_in_stock ) { | |
$meta_query[] = array( | |
'key' => '_stock_status', | |
'value' => 'instock', | |
'compare' => 'IN', | |
); | |
$set_wp_query->set( 'meta_query', $meta_query ); | |
} | |
} | |
return $set_wp_query; | |
} | |
*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment