Product Weight Fail Safe

Weight in oz is sent to carrier (USPS,UPS,Fedex etc) to pull a dynamic shipping rate at checkout keeing shipping costs accurate and inexpensive. If a weight is not entered for an item this function can be added to a themes functions.php file and will create a “fall back” weight based on item category. This is ideal for websites with hundreds of products missing weights as a fail safe.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//item weights - use variation input first, if none available use parent weight.
add_action( 'woocommerce_before_calculate_totals', 'add_custom_weight', 10, 1);
function add_custom_weight( $cart_object ) {
   
foreach(WC()->cart->get_cart() as $cart_item){
global $wpdb;
$pID = $cart_item['product_id'];
$variID = $cart_item['variation_id'];
$product_cats = wp_get_post_terms( $pID, 'product_cat' );
//use array for products with more then one category assigned
$single_cat = array($product_cats);
$cat_id = $single_cat->term_id;
$term_list = wp_get_post_terms($pID,'product_cat',array('fields'=>'ids'));


    //get parent weight
    $get_parent_weight ='Select meta_value from wp_postmeta where meta_key = "_weight" && post_id = '.$pID.'';
        $result = $wpdb->get_results($get_parent_weight);
        foreach($result as $row) {  
        $parent_weight = $row->meta_value;
        $pweight = $parent_weight;
        }
       
       
    if(isset($variID)){
        //get variation weight
        $get_variation_weight ='Select meta_value from wp_postmeta where meta_key = "_weight" && post_id = '.$variID.'';
        $result = $wpdb->get_results($get_variation_weight);
            foreach($result as $row) {  
                    $vari_weight = $row->meta_value;
                    if($vari_weight != NULL){
                        $pweight = $vari_weight;
                    }
            }
    }
//for each category, ignore featured category
foreach ($term_list as $key => $val) {
    if ($val != 1387){
    //no weight get category and use category weight rules
    if ($pweight < 1){
        //$pweight = 'y';
        //rules
            switch($val){

        //mod
            case 1379:
                $pweight = 12;
                break;
        //desktop
            case 45:
                $pweight = 93;
            break;
        //accessories
            case 1384:
                $pweight = 8;
            break;
        //juice
            case 1395:
                $pweight = 11;
            break;
        //batteries
            case 1383:
                $pweight = 5;
            break;
        //tanks
            case 1381:
                $pweight = 8;
            break;
        //coils
            case 1382:
                $pweight = 5;
            break;
        //kits
            case 1380:
                $pweight = 18;
            break;
        //cartridges
            case 1385:
                $pweight = 5;
            break;
       
        //pod systems
            case 1393:
                $pweight = 12;
            break; 
           
        //dry
            case 1388:
                $pweight = 12;
            break; 
            }
   
    }
    }
}

$cart_item['data']->set_weight( $pweight );
//echo $pweight.'<br>';
}

}

Created with PHP & SQL