If I gave you this code to put in functions.php, would you understand how to customize it? Or should I turn it into a simple plugin for you?
Heres a visual of it working on my local WP testing environment.
Installing into functions.php
Scroll down to where the actual code begins, and copy and paste that entire block, right onto the very bottom of your theme folders functions.php. Just a simple copy and paste.
Instructions for using.
Step 1. Create your shipping classes within WooComerce Settings > Shipping > Shipping Classes. For example on my test site I created 'bulky' and 'light' shipping classes within wooComerce settings. Remember the slug you set for step 2.
Step 2. At EX1
I place the case sensitive slug of a WooCommerce shipping class wrapped in ''.
At EX2
I place the description for the fee you would like displayed at checkout within ''. Finnaly at EX3
you simply place the numeric value for the fee, this does not go within ''.
//Example:
$shippingClasses['EX1'] = ['description' => 'EX2', 'fee' => EX3];
//How it will look:
$shippingClasses['bulky'] = ['description' => 'Bulky Fee', 'fee' => 7];
$shippingClasses['light'] = ['description' => 'Light Fee', 'fee' => 4];
And thats it! Thats all you have to do.
Code
function fees_fees_fees() {
$shippingClasses['bulky'] = ['description' => 'Bulky Fee', 'fee' => 5];
$shippingClasses['light'] = ['description' => 'Light Fee', 'fee' => 7];
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$shipping_class = get_the_terms( $values['product_id'], 'product_shipping_class' );
foreach($shippingClasses as $key => $val) {
if ( isset( $shipping_class[0]->slug ) && in_array( $shipping_class[0]->slug, [$key] ) ) {
WC()->cart->add_fee( __($val['description'], 'woocommerce'), $val['fee'] ); }
}
}
}
add_action( 'woocommerce_cart_calculate_fees', 'fees_fees_fees' );