• I need to set the weight of specific cart items to 0 before UPS (or FedEx for that matter) rates are calculated. The code below successfully changes the cart item weight, but it isn’t taken into consideration when calculating shipping rates.

    function cart_core_charge_weight_change( $cart ){
    	if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    		return;
    	
    	foreach( $cart->get_cart() as $cart_item ){
    		$itemId = wc_get_product($cart_item['data']->get_id());
    		$itemName = $itemId->get_title();
            if ( strpos( $itemName, '-$$$' ) !== false ) {
                $cart_item['data']->set_weight(0);
            }
        }
    }
    add_action( 'woocommerce_before_calculate_totals', 'cart_core_charge_weight_change', 25, 1 );

    In this case, if the cart item name ends with “-$$$”, the weight is set to 0. Shipping rates do not change, however.

  • The topic ‘Changing cart item weight before rate return’ is closed to new replies.