Skip to content

Instantly share code, notes, and snippets.

@xlplugins
Last active July 12, 2023 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xlplugins/fb1d988bc15e69a11a082424994f25fa to your computer and use it in GitHub Desktop.
Save xlplugins/fb1d988bc15e69a11a082424994f25fa to your computer and use it in GitHub Desktop.
Set Shipping method pre selected when cart total is greater than 80
if ( ! class_exists( 'WFACP_TT_FREE_Shipping' ) ) {
class WFACP_TT_FREE_Shipping {
private $run = false;
public function __construct() {
add_action( 'woocommerce_after_calculate_totals', [ $this, 'set_shipping' ] );
}
public function set_shipping() {
$packages = WC()->shipping()->get_packages();
$total = (float) WC()->cart->cart_contents_total;
if($total <=80){
return;
}
$methods = WC()->session->chosen_shipping_methods;
$update_shipping = false;
foreach ( $packages as $i => $package ) {
$chosen_method = isset( WC()->session->chosen_shipping_methods[ $i ] ) ? WC()->session->chosen_shipping_methods[ $i ] : '';
if ( false !== strpos( $chosen_method, 'free_shipping' ) ) {
break;
}
if ( true == $update_shipping ) {
break;
}
if ( isset( $package['rates'] ) ) {
foreach ( $package['rates'] as $key => $pack ) {
if ( false !== strpos( $key, 'free_shipping' ) ) {
$methods[ $i ] = $key;
$update_shipping = true;
break;
}
}
}
}
$already_set = WC()->session->get( 'wfacp_already_set_free_shipping_' . WFACP_Common::get_id(), '' );
$already_set='no';
if ( true == $update_shipping && 'yes' !== $already_set ) {
WC()->session->set( 'chosen_shipping_methods', $methods );
WC()->session->set( 'wfacp_already_set_free_shipping_' . WFACP_Common::get_id(), 'yes' );
}
}
}
new WFACP_TT_FREE_Shipping();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment