2
votes

I'm having a great deal of trouble finding what I need, so I turn to the gurus over here!

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.

Can someone point me in the right direction? I've stared myself blind...

1
You'll have to show us the code to get any kind of meaningful answer.mypetlion
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

1 Answers

1
votes

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' );