WooCommerce自定义缺货排序代码在属性页面失效的解决求助
解决WooCommerce产品属性页面缺货商品排序无效问题
你的代码逻辑没问题,只是条件判断没有覆盖产品属性页面。WooCommerce中产品属性页面属于自定义产品分类法页面,需要在判断条件中添加对应的检测逻辑。
修改后的条件判断行
直接添加产品属性页面的检测函数:
if (is_woocommerce() && (is_shop() || is_product_category() || is_product_tag() || is_product_attribute()))
或者用更简洁的写法,覆盖所有产品相关的分类法页面(包含分类、标签、属性):
if (is_woocommerce() && (is_shop() || is_product_taxonomy()))
完整修改后的代码
/** * Sorting out of stock WooCommerce products - Order product collections by stock status, in-stock products first. */ class iWC_Orderby_Stock_Status { public function __construct() { // Check if WooCommerce is active if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) { add_filter('posts_clauses', array($this, 'order_by_stock_status'), 2000); } } public function order_by_stock_status($posts_clauses) { global $wpdb; // only change query on WooCommerce loops if (is_woocommerce() && (is_shop() || is_product_taxonomy())) { $posts_clauses['join'] .= " INNER JOIN $wpdb->postmeta istockstatus ON ($wpdb->posts.ID = istockstatus.post_id) "; $posts_clauses['orderby'] = " istockstatus.meta_value ASC, " . $posts_clauses['orderby']; $posts_clauses['where'] = " AND istockstatus.meta_key = '_stock_status' AND istockstatus.meta_value <> '' " . $posts_clauses['where']; } return $posts_clauses; } } new iWC_Orderby_Stock_Status; /** * END - Order product collections by stock status, instock products first. */
说明:is_product_taxonomy()函数会匹配所有WooCommerce产品相关的归档类页面,包括产品分类、标签、属性页面,一次修改就能覆盖所有场景,避免后续遗漏其他产品归档页面的问题。
内容的提问来源于stack exchange,提问作者Lupani
相关产品推荐
相关产品推荐

