douyou7797 2017-07-04 14:53
浏览 50

在WooCommerce中编辑许多产品变体的权重

How can I edit the weight of many product variations in WooCommerce? Please.

Thanks

  • 写回答

1条回答 默认 最新

  • dou12352 2017-07-04 18:16
    关注

    First you get an array of your Product Variations IDs this way:

    global $wpdb;
    $posts_table = $wpdb->prefix . "posts";
    $variation_ids = $wpdb->get_col( "SELECT `ID` FROM $posts_table WHERE `post_status` LIKE 'publish' AND `post_type` LIKE 'product_variation'");
    

    Then you can edit the weight for this product variations this way:

    // Iterating through all product variations
    foreach( $variation_ids as $variation_id ){
    
        // Get an instance of the product variation object
        $variation_object = wc_get_product($variation_id);
    
        // Get the variation weight
        $weight = $variation_object->get_weight();
    
    
        ## -- USE CONDITIONS TO SET / UPDATE WEIGHT AS YOU NEED -- ##
    
        // Example of conditions below
        if( $weight <= 0.2 && $weight > 0.3 ){
            $variation_object->set_weight(0.28);
        } elseif( $weight <= 0.3 && $weight > 0.5 ){
            $variation_object->set_weight(0.42);
        }
    
        // Example of empty weight condition
        if( empty($weight) ){
            $variation_object->set_weight(0.16);
        }
    
        // Example Based on variation IDs from 125 to 129
        if($variation_id >= 125 &&  $variation_id < 130 ){
            $variation_object->set_weight(0.45);
        }
    }
    

    Then you can customize the conditions to match your needs and to embed this in a function to run it once.

    You can use any WC_Product and WC_Product_Variation methods on the Product Variation object $variation_object in your conditions too.

    This way you will set / update all your multiple needed variations weight

    Code goes in any php file of your active child theme (or theme) or also in any plugin php file.

    This code is tested and works on WooCommerce 3.0+

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集