Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FrancoStino/e17c4dda578eea46cb6b5a29768bb7ca to your computer and use it in GitHub Desktop.
Save FrancoStino/e17c4dda578eea46cb6b5a29768bb7ca to your computer and use it in GitHub Desktop.
A PHP function to calculate and set volumetric weight for shipping packages in WooCommerce based on product dimensions.

Calculate Volumetric Weight Shipping - Woocommerce

Preview:
<?php 

add_filter('woocommerce_cart_shipping_packages', 'calculate_volumetric_weight_shipping');

function calculate_volumetric_weight_shipping($packages) {
    $conversion_factor = 200; // Il fattore di conversione: 200 kg per m³
   
    foreach ($packages as &$package) {
        foreach ($package['contents'] as &$item) {
            $product = wc_get_product($item['product_id']);
            
            // Ottieni le dimensioni del prodotto e verifica se esistono
            $length = $product->get_length() ? $product->get_length() / 100 : 0; // Converti in metri
            $width = $product->get_width() ? $product->get_width() / 100 : 0;   // Converti in metri
            $height = $product->get_height() ? $product->get_height() / 100 : 0; // Converti in metri
            
            // Calcola il volume e il peso volume tariffario solo se tutte le dimensioni sono disponibili
            if ($length > 0 && $width > 0 && $height > 0) {
                $volume = $length * $width * $height;
                $volumetric_weight = $volume * $conversion_factor;
                
                // Confronta con il peso reale e sovrascrivi se il peso volume tariffario è maggiore
                if ($volumetric_weight > $product->get_weight()) {
                    $item['data']->set_weight($volumetric_weight);
                }
            }
        }
    }
   
    return $packages;
}
Associated Context
Type Code Snippet ( .php )
Associated Tags add_filter woocommerce_cart_shipping_packages calculate_volumetric_weight_shipping packages conversion_factor wc_get_product product_id width height Framework: WooCommerce woocommerce shipping volumetric weight product dimensions conversion factor
💡 Smart Description This code snippet adds a filter to the 'woocommerce_cart_shipping_packages' collection. It iterates through each package in an array and calculates the weight of all product objects based on their dimensions, width, height, or volume for each item
A PHP function to calculate and set volumetric weight for shipping packages in WooCommerce based on product dimensions.
🔎 Suggested Searches How to calculate volume weight for shipping packages in Woocommerce?
How to use wc_get_product() function usage in PHP?
How to check if a product has been created by the Cookbook?
How to convert multiple products into an array using Ottieni le dimensioni del prodotto e verifica se esistono and Converti in metri ?
volumetric weight calculation PHP
WooCommerce shipping package dimensions
calculate shipping weight based on dimensions
Related Links No Related Links
Related People Davide Ladisa
Sensitive Information No Sensitive Information Detected
Shareable Link https://davideladisa.pieces.cloud/?p=00014886d3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment