1

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
KD_1983
  • 71
  • 4

1 Answers1

2

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • I found an issue... I was probably unclear in my problem description. Right now the fee is only added to the cart if one item greater than 3 cm is added, if I add 3 (or more) items that are 1 cm each no fee is added... – KD_1983 Feb 25 '19 at 19:49
  • Or really, the action does work exactly as it should. Just like the action says. I'm the one being unclear. Sorry. I need a calculation on total cart height, is that easily modified in the action? @LoicTheAztec – KD_1983 Feb 25 '19 at 19:59
  • @KD_1983 I have added a version code that works on cart items total height… – LoicTheAztec Feb 25 '19 at 20:10
  • @LoicTheAztec Hi! An additional problem has arisen in my shop, regarding a new fee for total width. I've been trying to play around with the code I got from you, but I just break it, not knowing what I'm doing. I want to also add a fee for cart items total height and/or width to your code. ie if the cart is wider than 25 cm a fee applies regardless of if the height is over 3 cm and vice versa. I want them to work together but still be independent of each other, not stacking. Do I create a new question including your code, or is that copyright infringement? Thank you! – KD_1983 Sep 11 '19 at 17:52