2

I'm using Woocommerce and a plugin called Table Rate Shipping Plus (by mangohour) for our simple non-profit comic bookstore. We use weight based shipping as a standard in Sweden, so the flat rate system is not an option for our shop.

I have created a 'bulky' shipping class and I would need it to, upon choosing products in that class, automatically add a small fee to the cart (only once, without stacking) without the use of woocommerce's shipping zones and the flat rate system.

I can't seem to find anything that does this simple act anywhere, and I'm not php-savvy enough to be able to write functions or filters for the functions.php myself.

How can this be done?

starball
  • 20,030
  • 7
  • 43
  • 238
KD_1983
  • 71
  • 4
  • You'll have to show us the code to get any kind of meaningful answer. – mypetlion Feb 22 '19 at 19:46
  • If I'm clear, whenever someone adds a product from the "bulky" shipping class, a small fee gets added to the cart (just once not multiple times for multiple products)? I think I have an idea of what you're asking. Let me see if I can get something going, but could you clarify your question a bit, so I'm clear about what you're asking. – Brennan Walsh Feb 22 '19 at 20:03

1 Answers1

1

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.

Imgur


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' );
Brennan Walsh
  • 410
  • 4
  • 8
  • I’m glad it works for you! Feel free to take a little time to test it out. If you find a bug or so, just let me know in a comment here, and I’ll take a look! I might be packaging this script up into a plug-in. I’ll send you a GitHub link to it if I do. – Brennan Walsh Feb 23 '19 at 18:52
  • Yes, I will! The shop will go live again in a week and I'll test and troubleshoot and get back to you! GitHub link, yes! Thank you! – KD_1983 Feb 23 '19 at 19:33
  • Hi! I thought I'd let you know I found the time to make a simple plugin out of this. It adds a field to the shipping class table for the "fee". Here it's on github. https://github.com/iambrennanwalsh/woo-shipping-class-fees – Brennan Walsh Feb 25 '19 at 10:54
  • Thank you! I think this will help a lot of people! My coworker just realized that I did not really solve the issue with a shipping class fee. Stupidly I thought I needed to add a bulky fee to a shipping class, and this solving my problem, but I should’ve asked how to add a bulky fee automatically to the cart if the height of the products is over 2,9 cm. Is it too much to ask if you could adapt/extend the function/plugin? I don’t want to be rude and waste your time. If not I still appreciate this solution and I will use it until I figure my new problem out. – KD_1983 Feb 25 '19 at 12:45