<?php add_action( 'woocommerce_before_cart', 'auto_select_free_shipping_by_default' ); function auto_select_free_shipping_by_default() { if ( isset(WC()->session) && ! WC()->session->has_session() ) WC()->session->set_customer_session_cookie( true ); // Check if "free shipping" is already set if ( strpos( WC()->session->get('chosen_shipping_methods')[0], 'woocommerce_flatrate_perpostcode' ) !== false ) return; // Loop through shipping methods foreach( WC()->session->get('shipping_for_package_0')['rates'] as $key => $rate ){ if( $rate->method_id === 'shipping_method_0_woocommerce_flatrate_perpostcode' ){ // Set "Free shipping" method WC()->session->set( 'chosen_shipping_methods', array($rate->id) ); return; } } } ?>
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter