• Resolved tpgtpg

    (@tpgtpg)


    Hi, my products vary in size and I would like to assign various shipping classes based on choice selected. My shipping classes cover different shipping costs to different countries.

    How would I do this without using variable products?

    Can I do this with some funtions.php action/hook or jQuery selecting another shipping method? (In my usecase I can substitute shipping classes with methods).

    Thank you.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor Marc Lacroix

    (@marcusig)

    Hi there,

    I took a look at what’s possible, and I’m sure it is doable, though it’s not a simple task.

    See the following code:

    add_action( 'woocommerce_before_calculate_totals', function( $cart_obj ) {
    		$cart_contents = $cart_obj->get_cart();
    		foreach ( $cart_contents as $key => $cart_item ) {
    			// Check the cart item selection
    			if ( mkl_pc_is_configurable( $cart_item['product_id'] ) && isset( $cart_item['configurator_data'] ) && is_array( $cart_item['configurator_data'] ) ) {
    				
    				// Check the configurator's choices
    				foreach( $cart_item['configurator_data'] as $selected_choice ) {
    					if ( 'Something special' === $selected_choice->get_choice( 'name' ) ) {
    						// Change the shipping class ID
    						$cart_item['data']->set_shipping_class_id( 67 );
    					}
    				}
    			}
    		}
    }, 10, 1);

    This should change the shipping class of the product depending on a selected choice. This seems to work, though it might not be a full solution.

    Otherwise if you are using the linked product / stock management add-on, you may be able to link a choice to a different product which has a specific class.

    Marc

    Thread Starter tpgtpg

    (@tpgtpg)

    Great! Thank you – it works exactly as it should. ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Shipping classes vary based on choices’ is closed to new replies.