简体   繁体   English

Woocommerce:尽管应用了分类标准,但最后仍对缺货产品进行分类

[英]Woocommerce: Sort out of stock products at the end despite sort criteria applied

I have been able to show out of stocks products at the end with the default sorting using this snippet:我已经能够使用此代码段使用默认排序在最后显示缺货产品:

/**
 * @snippet       Order out of stock products at the end - WooCommerce
 * @author        Sebastian Velandia
 * @compatible    WooCommerce 3.8.0
 */
add_filter('posts_clauses', 'order_by_stock_status');
function order_by_stock_status($posts_clauses) {
    global $wpdb;
    // only change query on WooCommerce loops
    if (is_woocommerce() && (is_shop() || is_product_category() || is_product_tag() || 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;
}

This works okey with the default sorting, but when you apply other sorting criteria like for example, by price, then it stops working and out of stock products become visible again from the beginning of the catalog.这对默认排序有效,但是当您应用其他排序标准(例如按价格)时,它将停止工作并且缺货产品从目录的开头再次可见。

How to fix this snippet to always display the out of stock products at the end despite the sorting applied in woocommerce catalog?尽管在 woocommerce 目录中应用了排序,但如何修复此代码段以始终在最后显示缺货产品?

So this seems to work in testing.所以这似乎在测试中起作用。 The orderby clause when you change the WooCommerce sort order on the shop page is superseded by the ordering.当您更改商店页面上的 WooCommerce 排序顺序时,orderby 子句将被排序所取代。 So this uses the filter woocommerce_get_catalog_ordering_args to append to all sorting queries your orderby function before the choice from the dropdown.因此,这使用过滤器woocommerce_get_catalog_ordering_args到 append 在从下拉列表中选择之前对您的 orderby function 进行所有排序查询。

add_filter('woocommerce_get_catalog_ordering_args', 'dd_catalog_order');
function dd_catalog_order(){
    add_filter('posts_clauses', 'keep_order_by_stock_status');
}

function keep_order_by_stock_status($posts_clauses){
    $posts_clauses['orderby'] = " istockstatus.meta_value ASC, " . $posts_clauses['orderby'];
    return $posts_clauses;    
}

this code worked for me (put in functions.php of your theme)这段代码对我有用(放入你主题的 functions.php)

/**
* 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_category() || is_product_tag())) {
$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.
*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM