Modify functions.php in wordpress theme to show out of stock products in product categories in woocommerce
Budget: $10 – $30 USD
I need someone to modify my functions.php. Right now there is code that doesnt show out of stock products on my themes product pages in woocommerce. There is code that will show out of stock products on its own page. Basically i want to keep everything as is but be able to see out of stock products if i go to a products category link.
Current products which is empty at the moment - https://hunternott.com/current-puppies/
Out of stock products showing - https://hunternott.com/past-puppies/
Example category where out of stock products arent showing but show if i change inventory to 1 - https://hunternott.com/product-category/coco_010622/
My functions.php ccode currently
add_action( 'pre_get_posts', 'iconic_hide_out_of_stock_products' );
function iconic_hide_out_of_stock_products( $q ) {
if ( ! $q->is_main_query() || is_admin() ) {
return;
}
if ( $outofstock_term = get_term_by( 'name', 'outofstock', 'product_visibility' ) ) {
$tax_query = (array) $q->get('tax_query');
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => array( $outofstock_term->term_taxonomy_id ),
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
remove_action( 'pre_get_posts', 'iconic_hide_out_of_stock_products' );
}
/**
* Temporarily enable hide out of stock items.
*
* @param string $template_name
* @param string $template_path
* @param bool $located
* @param array $args
*/
function iconic_enable_hide_out_of_stock_items( $template_name, $template_path, $located, $args ) {
if( $template_name !== "single-product/related.php" ) {
return;
}
add_filter( 'pre_option_woocommerce_hide_out_of_stock_items', function( $option ) { return "yes"; }, 10, 1 );
}
add_action( 'woocommerce_before_template_part', 'iconic_enable_hide_out_of_stock_items', 10, 4 );
/**
* Temporarily disable hide out of stock items.
*
* @param string $template_name
* @param string $template_path
* @param bool $located
* @param array $args
function iconic_disable_hide_out_of_stock_items( $template_name, $template_path, $located, $args ) {
if( $template_name !== "single-product/related.php" ) {
return;
}
add_filter( 'pre_option_woocommerce_hide_out_of_stock_items', function( $option ) { return "no"; }, 10, 1 );
}
*/
//add_action( 'woocommerce_after_template_part', 'iconic_disable_hide_out_of_stock_items', 10, 4 );
/**
* @snippet Display All Products Out of Stock via a Shortcode - WooCommerce
*/
add_shortcode( 'out_of_stock_products', 'bbloomer_out_of_stock_products_shortcode' );
function bbloomer_out_of_stock_products_shortcode() {
global $product, $woocommerce, $woocommerce_loop;
$columns = 4;
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_stock',
'value' => 1,
'compare' => '<'
)
)
);
Current products which is empty at the moment - https://hunternott.com/current-puppies/
Out of stock products showing - https://hunternott.com/past-puppies/
Example category where out of stock products arent showing but show if i change inventory to 1 - https://hunternott.com/product-category/coco_010622/
My functions.php ccode currently
add_action( 'pre_get_posts', 'iconic_hide_out_of_stock_products' );
function iconic_hide_out_of_stock_products( $q ) {
if ( ! $q->is_main_query() || is_admin() ) {
return;
}
if ( $outofstock_term = get_term_by( 'name', 'outofstock', 'product_visibility' ) ) {
$tax_query = (array) $q->get('tax_query');
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => array( $outofstock_term->term_taxonomy_id ),
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
remove_action( 'pre_get_posts', 'iconic_hide_out_of_stock_products' );
}
/**
* Temporarily enable hide out of stock items.
*
* @param string $template_name
* @param string $template_path
* @param bool $located
* @param array $args
*/
function iconic_enable_hide_out_of_stock_items( $template_name, $template_path, $located, $args ) {
if( $template_name !== "single-product/related.php" ) {
return;
}
add_filter( 'pre_option_woocommerce_hide_out_of_stock_items', function( $option ) { return "yes"; }, 10, 1 );
}
add_action( 'woocommerce_before_template_part', 'iconic_enable_hide_out_of_stock_items', 10, 4 );
/**
* Temporarily disable hide out of stock items.
*
* @param string $template_name
* @param string $template_path
* @param bool $located
* @param array $args
function iconic_disable_hide_out_of_stock_items( $template_name, $template_path, $located, $args ) {
if( $template_name !== "single-product/related.php" ) {
return;
}
add_filter( 'pre_option_woocommerce_hide_out_of_stock_items', function( $option ) { return "no"; }, 10, 1 );
}
*/
//add_action( 'woocommerce_after_template_part', 'iconic_disable_hide_out_of_stock_items', 10, 4 );
/**
* @snippet Display All Products Out of Stock via a Shortcode - WooCommerce
*/
add_shortcode( 'out_of_stock_products', 'bbloomer_out_of_stock_products_shortcode' );
function bbloomer_out_of_stock_products_shortcode() {
global $product, $woocommerce, $woocommerce_loop;
$columns = 4;
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_stock',
'value' => 1,
'compare' => '<'
)
)
);