如何添加按销售折扣过滤产品的woocommerce过滤器?

时间:2018-11-21 20:42:46

标签: wordpress woocommerce

我有woocommerce产品,并且增加了一些折扣。是否有可能,以及如何添加一个过滤器来获得折扣产品?

谢谢

1 个答案:

答案 0 :(得分:0)

这就是我通过可用折扣过滤产品的方式,使用操作

<块引用>

woocommerce_product_query

add_action( 'woocommerce_product_query', 'mam_action_woocommerce_product_query'); 
function mam_action_woocommerce_product_query($q) { 

    global $wpdb;
    $prefix = $wpdb->prefix;

    $discount_low =  10;
    $discount_high = 20;
    $query = "SELECT p.ID 
FROM ".$prefix."posts p, ".$prefix."postmeta pm1, ".$prefix."postmeta pm2, ".$prefix."postmeta pm3
WHERE 
    p.post_type='product' 
AND p.post_status='publish' 
AND p.ID=pm1.post_id 
AND p.ID=pm2.post_id
AND p.ID=pm3.post_id 
AND ( (pm1.meta_key='_sale_price' AND pm1.meta_value > 0)  ) 
AND pm3.meta_key='_regular_price' AND (  (  (   (CAST(pm3.meta_value AS DECIMAL) - CAST(pm1.meta_value AS DECIMAL)) /  NULLIF(CAST(pm3.meta_value AS DECIMAL),0) )*100) ) >= '$discount_low' 
AND pm3.meta_key='_regular_price' AND (  (  (   (CAST(pm3.meta_value AS DECIMAL) - CAST(pm1.meta_value AS DECIMAL)) /  NULLIF(CAST(pm3.meta_value AS DECIMAL),0) )*100) ) <= '$discount_high' 
GROUP BY p.ID ";
    

        
        
        
    $all_salable_products = $wpdb->get_results($query);
        
    $posts_in_arr = array();

    foreach($all_salable_products as $all_salable_product){
        $posts_in_arr[] = $all_salable_product->ID;
        
    }
    
    $q->set( 'post__in', $posts_in_arr );
    
    if(count($posts_in_arr) == 0)
    wc_add_notice( 'No item matches your criteria', 'notice' );     



return;


    
    
}

您可以从 Webostock Marketplace 获得完整的插件。包括要在商店页面上使用的小部件。