• Resolved bhasic

    (@bhasic)


    I’m trying to do calculations with simple and variable products length, width and height for shipping, but I can’t get it work with variable products. When I var_dump, $dim_weight or $length etc they only display at product-page and there they are correct. With simple products it works like it’s supposed to. How can I get it to work with simple and variable products? This is what I tried last time:

    add_filter( 'woocommerce_product_get_weight', 'custom_get_weight_from_dimensions', 10, 2 );
    function custom_get_weight_from_dimensions( $weight, $product ) {
        $dim_weight = 0;
        if ( ! is_admin() ) {
    
            $targetted_shipping_ids  = array( 'myshipping' );
    
            $chosen_shipping_methods  = (array) WC()->session->get('chosen_shipping_methods');
            $matched_shipping_methods = array_intersect( $chosen_shipping_methods, $targetted_shipping_ids );
    
            if( ! empty($matched_shipping_methods) ) {
    
                foreach(WC()->cart->get_cart() as $cart_item) { 
    
                    $qty = $cart_item['quantity'];
                    $width = $cart_item['data']->get_width();
                    $length = $cart_item['data']->get_length();
                    $height = $cart_item['data']->get_height();
    
                    $dim_weight += $width * $length * $height * $qty; 
                }
            }
      }
    
    return isset($dim_weight) && $dim_weight > $weight ? $dim_weight : $weight;
    }
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Calculating shipping in cart with variable product dimensions’ is closed to new replies.