1
votes

I'm trying to find a function that automatically adds a fee to the cart if the height of the products in it is over 2,9 cm.

I'm using Woocommerce for our simple non-profit comic bookstore. We use weight based shipping as a standard in Sweden, with a bulky fee if something is 3 cm or over.

I've tried modifying this answer of LoicTheAztec regarding fees based on total cart weight, but I really don't know what I'm doing as I am getting a blank page after saving the code.

The code I'm trying to modify is this one:

add_action( 'woocommerce_cart_calculate_fees', 'shipping_weight_fee', 30, 1 );
function shipping_weight_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Convert cart weight in grams
    $cart_weight = $cart->get_cart_contents_weight() * 1000;
    $fee = 50; // Starting Fee below 500g

    // Above 500g we add $10 to the initial fee by steps of 1000g
    if( $cart_weight > 1500 ){
        for( $i = 1500; $i < $cart_weight; $i += 1000 ){
            $fee += 10;
        }
    }
    // Setting the calculated fee based on weight
    $cart->add_fee( __( 'Weight shipping fee' ), $fee, false );
}

My experience with php is not more than being able to paste actions into my child-theme's functions.php.

I appreciate any help I can get.

1

1 Answers

2
votes

The following code will add a specific fee if any cart item has a height up to 3 cm (dimensions unit setting in Woocommerce need to be in cm):

add_action( 'woocommerce_cart_calculate_fees', 'shipping_height_fee', 10, 1 );
function shipping_height_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Your settings (here below)
    $height = 3; // The defined height in cm (equal or over)
    $fee    = 50; // The fee amount
    $found  = false; // Initializing

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item ){
        if( $cart_item['data']->get_height() >= $height ) {
            $found = true;
            break; // Stop the loop
        }
    }
    // Add the fee
    if( $found ) {
        $cart->add_fee( __( 'Height shipping fee' ), $fee, false );
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.


Addition: Code based on cart items total height:

add_action( 'woocommerce_cart_calculate_fees', 'shipping_height_fee', 10, 1 );
function shipping_height_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Your settings (here below)
    $target_height = 3; // The defined height in cm (equal or over)
    $total_height  = 0; // Initializing
    $fee           = 50; // The fee amount

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item ){
        $total_height += $cart_item['data']->get_height() * $cart_item['quantity'];
    }
    // Add the fee
    if( $total_height >= $target_height ) {
        $cart->add_fee( __( 'Height shipping fee' ), $fee, false );
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.