Code này sẽ ẩn các sản phẩm “sold out” trong phần “Relate Product”, tuy nhiên nó sẽ để trống chỗ đó. Ví dụ như cài đặt là 6 sản phẩm liên quan, nếu có 1 sản phẩm hết hàng thì nó sẽ chỉ hiện 5 thôi chứ không tự thêm sản phẩm khác.
Lưu ý là nên backup trước khi áp dụng, nên chèn vào file function của child theme. Mình đã test trên theme Flatsome chạy ổn.
/**
* Temporarily enable <a href = 'https://iconicwp.com/blog/hide-stock-products-woocommerce-catalog-pages/' class = 'wpil_internal_link' style = 'display: inline !important; width: auto !important' target = '_blank' data-wpil-post-to-id = '364' >hide out of stock</a> 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 );
}
/**
* 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_before_template_part', 'iconic_enable_hide_out_of_stock_items', 10, 4 );
add_action( 'woocommerce_after_template_part', 'iconic_disable_hide_out_of_stock_items', 10, 4 );