我找到了一个插入我的functions.php的代码,用于组织我的产品" instock"最后" outstock"这很好,但是我有超过1000种产品,有些产品有一般图像,因为它们没有产品图像,这叫做" no2.png",我需要组织我的库存产品并没有一般形象" no2.png"
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
//SELECT * FROM redpuestosw.galio_posts where ID = '66138';
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;
以下是前一代码如何工作的示例,我需要显示那些具有该一般图像的产品,但是最后一些产品以及如果它们具有唯一图像的产品首先显示,我感谢您的帮助。< / p>
来自我的网站商店的示例图片:
答案 0 :(得分:0)
下午好,社区终于可以解决我的问题了,我希望这能为那些想要首先展示产品形象和价格的人提供服务。
在我的孩子主题的function.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
//SELECT * FROM redpuestosw.galio_posts where ID = '66138';
if (is_woocommerce() && (is_shop() || is_product_category() || is_product_tag())) {
$posts_clauses['fields'] .= " , IF(img.guid REGEXP 'no2.png' , '0' , '1' ) as imagen";
$posts_clauses['join'] .= " INNER JOIN $wpdb->postmeta istockstatus ON ($wpdb->posts.ID = istockstatus.post_id )";
$posts_clauses['join'] .= "LEFT JOIN $wpdb->postmeta pm ON ($wpdb->posts.ID = pm.post_id AND pm.meta_key = '_thumbnail_id' )";
$posts_clauses['join'] .= "LEFT JOIN $wpdb->posts img ON (pm.meta_value = img.ID)";
$posts_clauses['orderby'] = "imagen+0 DESC,istockstatus.meta_value ASC, " . $posts_clauses['orderby'];
$posts_clauses['where'] = "AND istockstatus.meta_key = '_stock_status' AND istockstatus.meta_key <> '' " . $posts_clauses['where'];
var_dump( $posts_clauses);
}
return $posts_clauses;
}
}
new iWC_Orderby_Stock_Status;
/**
* END - Order product collections by stock status, instock products first.
*/
添加LEFT JOIN以在postmeta'_thumbmail_id'中查找图像的id然后使用该id添加另一个LEFT JOIN以将该id与帖子的GUID进行比较,然后在字段[fields]中添加:IF(img。 guid REGEXP'no2.png','0','1')作为图像,我在其中创建一个正则表达式,在guid中找到非唯一图像的名称,如果它有一个'0'我加一个'0'唯一的图像它会有一个'1',所以按顺序我可以从最高到最低排序。