dongshou6788 2018-01-30 00:24
浏览 46
已采纳

在WooCommerce中以最高价格获取一系列独特的产品属性/值

In WooCommerce, I would like to:

  1. Get the unique values for a certain product attribute (From all my products), assuming all products have that particular attribute set as something.

  2. Get the list of unique attribute values with the highest product price.

For Example:

Product 1:
Product-Attribute = Green
Price = $100

Product 2:
Product-Attribute = Red
Price = $50

Product 3:
Product-Attribute = Green
Price = $50

Expected result (an array):

Green : $100
Red : $50

Any ideas on how to code this so that it returns an array?

  • 写回答

1条回答 默认 最新

  • dongqie2355 2018-01-30 18:32
    关注

    For Variable products and its variation attributes name and term value name with the highest variation price in an array, you will try the following custom function:

    function get_variations_attributes_values_highest_price(){
        global $wpdb;
    
        // SQL query
        $all_attributes = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}woocommerce_attribute_taxonomies");
        $product_attributes = array();
        foreach( $all_attributes as $value ){
            $product_attributes = 'pa_'.$value->attribute_name;
        }
        //$pa_taxonomies = "'".implode("','", $product_attributes)."'";
    
        // The 2nd SQL query
        $query = $wpdb->get_results( "
            SELECT p.ID, pm.meta_key as attr, pm.meta_value as term, pm2.meta_value as price
            FROM {$wpdb->prefix}postmeta as pm
            INNER JOIN {$wpdb->prefix}posts as p ON pm.post_id = p.ID
            INNER JOIN {$wpdb->prefix}postmeta as pm2 ON pm.post_id = pm2.post_id
            WHERE p.post_type IN ('product_variation','product')
            AND p.post_status LIKE 'publish'
            AND pm.meta_key LIKE 'attribute_pa_%'
            AND pm2.meta_key LIKE '_price'
            AND pm2.meta_value != ''
            ORDER BY pm.meta_key ASC, pm.meta_value ASC, CAST(replace(pm2.meta_value, 'MDT ', '') AS UNSIGNED) ASC
        " );
        //print_pr($query);
    
        $ordered_results = $results = array();
    
        // Loop 1: Filtering and removing duplicate terms keeping highest price
        foreach( $query as $values ){
            if( !empty($values->price)){
            $filter_key = $values->attr .' '.$values->term;
                $ordered_results[$filter_key] = (object) array( 'attr' => $values->attr,
                    'term' => $values->term, 'price' => $values->price );
            }
        }
    
        // Loop 2: Get the real name values, formatting data
        foreach( $ordered_results as $result ){
            $taxonomy = str_replace('attribute_' , '', $result->attr );
            $attr_name = get_taxonomy( $taxonomy )->labels->singular_name; // Attribute name
            $value_name = get_term_by( 'slug', $result->term, $taxonomy )->name; // Attribute value term name
            $results[$attr_name.' - '.$value_name] = wc_price($result->price);
        }
        return $results; 
    }
    

    Code goes in function.php file of the active child theme (or active theme).

    Tested and works.


    ## --- USAGE --- ##
    
    // RAW ARRAY OUTPUT
    echo '<pre>'; print_r(get_variations_attributes_values_highest_price()); echo '</pre>';
    

    You can make little changes easily to match your needs (the honey for TheBear)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)