[英]How to Order related products by Product ID in WooCommerce?
我安裝了一個插件,可以在產品頁面-WooCommerce自定義相關產品上添加和自定義相關產品 。
該插件將相關ID存儲在post_meta
表中:
meta_key
=> _wcrp_related_ids
meta_values
=> a:3:{i:0; s:3:“ 159”; i:1; s:3:“ 158”; i:2; s:3:“ 244”;}
我想根據相關產品的存儲順序來訂購它們,例如159、158、244。
我已經可以使用以下代碼通過產品ID訂購相關產品。 但是我不確定如何修改以按meta_values
排序。
add_filter('woocommerce_output_related_products_args', 'jk_related_products_args');
function jk_related_products_args($args)
{
$args['orderby'] = 'id'; // This works
return $args;
}
請檢查WP_Query文檔。 您應該通過定制元查找需要訂購的東西。
當您正在尋找類似的東西時 ,WordPress具有此功能,您需要在
orderby
傳遞post__in
。
add_filter('woocommerce_output_related_products_args', 'wh_related_products_args');
function wh_related_products_args($args)
{
global $post;
$related = get_post_meta($post->ID, '_wcrp_related_ids', true); //replace it with your metakey
//if a product do not have any realted product then don't alter order by
if ($related)
{
$args['orderby'] = 'post__in';
}
return $args;
}
參考: 按特定帖子ID排序&包含數組
代碼進入您的活動子主題(或主題)的functions.php
文件中。 或在任何插件php文件中。
代碼已經過測試並且可以正常工作。
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.