Last active
December 4, 2023 03:29
-
-
Save vikaskhunteta/660a1fe15ab12e8f936ce5d42f6b59d8 to your computer and use it in GitHub Desktop.
Get available shipping methods before products added into cart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Get available shipping methods before products added into cart | |
// below function is intended to work with single shipping zone and their methods | |
// Place this code into functions.php file of your WP active theme | |
if ( ! function_exists( 'get_available_shipping_methods' ) ) { | |
function get_available_shipping_methods() | |
{ | |
global $wpdb; | |
$available_methods = []; | |
$active_methods = (array) $wpdb->get_results( "SELECT t2.instance_id, t2.method_id, t1.zone_order FROM {$wpdb->prefix}woocommerce_shipping_zones AS t1 INNER JOIN {$wpdb->prefix}woocommerce_shipping_zone_methods AS t2 ON t1.zone_id = t2.zone_id WHERE t2.is_enabled = '1' ORDER BY t2.method_order ASC " ); | |
if ( $active_methods ) { | |
foreach ( $active_methods as $active_method ) { | |
$method_settings = (array)get_option( 'woocommerce_' . $active_method->method_id . '_' . $active_method->instance_id . '_settings' ); | |
$active_method_array = (array)$active_method; | |
foreach ($active_method_array as $key => $value) { | |
$method_settings[$key] = $value; | |
} | |
$available_methods[] = $method_settings; | |
} | |
} | |
return $available_methods; | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Check the shipping methods if cookie available | |
function check_and_set_shipping_method() | |
{ | |
$('input[name^="shipping_method"]').on('change', function(event){ | |
document.cookie = 'wp_woocommerece_shipping_method=' + $(this).val() + ';path=/;max-age=' + 60*60*24; | |
}); | |
$('.archive.woocommerce input[name^="shipping_method"]').on('change', function(event){ | |
$('input[name^="shipping_method"]').parent().addClass('btn-default').removeClass('btn-primary active'); | |
$(this).parent().addClass('btn-primary active').removeClass('btn-default'); | |
}); | |
var shipping_method = document.cookie.replace(/(?:(?:^|.*;\s*)wp_woocommerece_shipping_method\s*\=\s*([^;]*).*$)|^.*$/, "$1"); | |
if ( shipping_method !== '' ) { | |
$('.archive.woocommerce input[name^="shipping_method"]').removeAttr('checked').prop('checked', false); | |
$('input[name^="shipping_method"][value="'+shipping_method+'"]').attr('checked', 'checked').prop('checked', true).siblings('label').trigger('click'); | |
$('.archive.woocommerce input[name^="shipping_method"][value="'+shipping_method+'"]').parent().addClass('btn-primary active').removeClass('btn-default'); | |
} | |
// wc_cart_params is required to continue, ensure the object exists | |
if ( typeof wc_cart_params !== 'undefined' ) { | |
var shipping_methods = {}; | |
$( 'select.shipping_method, :input[name^=shipping_method][type=radio]:checked, :input[name^=shipping_method][type=hidden]' ).each( function() { | |
shipping_methods[ $( this ).data( 'index' ) ] = $( this ).val(); | |
} ); | |
$( 'div.cart_totals' ).addClass( 'processing' ).block( { | |
message: null, | |
overlayCSS: { | |
background: '#fff', | |
opacity: 0.6 | |
} | |
} ); | |
var data = { | |
security: wc_cart_params.update_shipping_method_nonce, | |
shipping_method: shipping_methods | |
}; | |
$.ajax( { | |
type: 'post', | |
url: wc_cart_params.wc_ajax_url.toString().replace( | |
'%%endpoint%%', | |
'update_shipping_method'), | |
data: data, | |
dataType: 'html', | |
success: function( response ) { | |
$( '.cart_totals' ).replaceWith( response ); | |
$( document.body ).trigger( 'updated_cart_totals' ); | |
}, | |
complete: function() { | |
$( 'div.cart_totals' ).removeClass( 'processing' ).unblock(); | |
$( document.body ).trigger( 'updated_shipping_method' ); | |
} | |
} ); | |
} | |
} | |
jQuery(document).ready(function($) { | |
check_and_set_shipping_method(); | |
// For cart and checkout page | |
$(document).on('updated_shipping_method update_checkout', function(event){ | |
document.cookie = 'wp_woocommerece_shipping_method=' + $('input[name^="shipping_method"]:checked').val() + ';path=/;max-age=' + 60*60*24; | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$available_methods = array_filter( get_available_shipping_methods() ); | |
if ( $available_methods ) { | |
?> | |
<div class="shipping_methods"> | |
<div class="btn-group" data-toggle="buttons"> | |
<?php foreach ( $available_methods as $method ) { ?> | |
<a href="#" class="btn btn-default" data-toggle="tab"> | |
<input type="radio" name="shipping_method[<?php echo $method['zone_order']; ?>]" data-index="<?php echo $method['zone_order']; ?>" id="shipping_method_<?php echo $method['zone_order']; ?>_<?php echo $method['method_id']; ?><?php echo $method['instance_id']; ?>" value="<?php echo $method['method_id'] . ":" . $method['instance_id']; ?>" class="shipping_method"> | |
<label for="shipping_method_<?php echo $method['zone_order']; ?>_<?php echo $method['method_id']; ?><?php echo $method['instance_id']; ?>"><?php echo $method['title']; ?></label> | |
</a> | |
<?php } ?> | |
</div> <!-- /.btn-group --> | |
</div> <!-- /.shipping_methods --> | |
<?php } ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment