woocommerce 在前端按库存和缺货对产品进行排序
woocommerce sort products by in stock and out of stock in front-end
我想首先在产品类别中显示库存产品,或者如果可能的话在任何地方显示,然后我也想在 Woocommerce 中显示缺货产品
其实有很多没有数量但是有库存的产品,所以需要查看库存状态,但我更喜欢先有更多数量
在这种情况下如何强制当前排序?
非常感谢
使用此代码:
<?php
/**
* Order product collections by stock status, instock 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, 2);
}
}
public function order_by_stock_status($posts_clauses, $wp_query)
{
global $wpdb;
// only change query on WooCommerce loops
if (false === is_admin() && $wp_query->is_post_type_archive('product')) {
$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;
?>
我在搜索中看到的其他代码有时会在商店页面中创建 404 错误,我编辑为:
add_filter('posts_clauses', 'avn_order_by_stock_status');
function avn_order_by_stock_status($posts_clauses) {
global $wpdb;
if (is_woocommerce() && (is_shop() || is_product_category() || is_product_tag() || is_product_taxonomy())) {
if(is_shop()){
$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;
}
我想首先在产品类别中显示库存产品,或者如果可能的话在任何地方显示,然后我也想在 Woocommerce 中显示缺货产品
其实有很多没有数量但是有库存的产品,所以需要查看库存状态,但我更喜欢先有更多数量
在这种情况下如何强制当前排序? 非常感谢
使用此代码:
<?php
/**
* Order product collections by stock status, instock 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, 2);
}
}
public function order_by_stock_status($posts_clauses, $wp_query)
{
global $wpdb;
// only change query on WooCommerce loops
if (false === is_admin() && $wp_query->is_post_type_archive('product')) {
$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;
?>
我在搜索中看到的其他代码有时会在商店页面中创建 404 错误,我编辑为:
add_filter('posts_clauses', 'avn_order_by_stock_status');
function avn_order_by_stock_status($posts_clauses) {
global $wpdb;
if (is_woocommerce() && (is_shop() || is_product_category() || is_product_tag() || is_product_taxonomy())) {
if(is_shop()){
$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;
}