Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save xlplugins/c48e52a40ef3fe961d53753fca620205 to your computer and use it in GitHub Desktop.

Select an option

Save xlplugins/c48e52a40ef3fe961d53753fca620205 to your computer and use it in GitHub Desktop.
Delivery or pickup unavailable on update checkout by fk
// --- server: return the correct options + min-spend notice for the CURRENT cart ---
add_action( 'wp_ajax_cr_relief_order_type', 'cr_relief_order_type_cb' );
add_action( 'wp_ajax_nopriv_cr_relief_order_type', 'cr_relief_order_type_cb' );
function cr_relief_order_type_cb() {
if ( ! function_exists( 'WC' ) || is_null( WC()->cart ) || ! class_exists( 'Coderockz_Woo_Delivery_Delivery_Option' ) ) {
wp_send_json_error();
}
WC()->cart->calculate_totals();
$settings = get_option( 'coderockz_woo_delivery_option_delivery_settings' );
$options = Coderockz_Woo_Delivery_Delivery_Option::delivery_option( $settings ); // exact plugin eval
$notice = '';
if ( class_exists( 'Coderockz_Woo_Delivery_Helper' ) ) {
$helper = new Coderockz_Woo_Delivery_Helper();
$ct = $helper->cart_total();
$sym = get_woocommerce_currency_symbol();
$min_d = ( isset( $settings['minimum_amount_cart_restriction'] ) && $settings['minimum_amount_cart_restriction'] !== '' ) ? (float) $settings['minimum_amount_cart_restriction'] : null;
$min_p = ( isset( $settings['minimum_amount_cart_restriction_pickup'] ) && $settings['minimum_amount_cart_restriction_pickup'] !== '' ) ? (float) $settings['minimum_amount_cart_restriction_pickup'] : null;
$d_fail = ! empty( $settings['enable_delivery_restriction'] ) && ! is_null( $min_d ) && (float) $ct['delivery'] < $min_d && ! isset( $options['delivery'] );
$p_fail = ! empty( $settings['enable_pickup_restriction'] ) && ! is_null( $min_p ) && (float) $ct['pickup'] < $min_p && ! isset( $options['pickup'] );
$d_txt = ! empty( $settings['delivery_restriction_notice'] ) ? stripslashes( $settings['delivery_restriction_notice'] ) : 'Your cart amount must be ' . $sym . $min_d . ' to get the Delivery Option';
$p_txt = ! empty( $settings['pickup_restriction_notice'] ) ? stripslashes( $settings['pickup_restriction_notice'] ) : 'Your cart amount must be ' . $sym . $min_p . ' to get the Pickup Option';
if ( $d_fail && $p_fail ) { $notice = ( $d_txt === $p_txt ) ? $d_txt : $d_txt . ' / ' . $p_txt; }
elseif ( $d_fail ) { $notice = $d_txt; }
elseif ( $p_fail ) { $notice = $p_txt; }
}
wp_send_json_success( array( 'options' => $options, 'notice' => $notice ) );
}
// --- server safety net: if the selection was lost client-side (fragment re-render /
// cleared select) but only ONE option is available for the current cart, accept it
// instead of blocking with "Please select order type". Runs before the plugin's
// validation (priority 10 on the same hook).
add_action( 'woocommerce_checkout_process', function () {
if ( ! empty( $_POST['coderockz_woo_delivery_delivery_selection_box'] ) || ! class_exists( 'Coderockz_Woo_Delivery_Delivery_Option' ) ) { // phpcs:ignore
return;
}
$settings = get_option( 'coderockz_woo_delivery_option_delivery_settings' );
if ( empty( $settings['enable_option_time_pickup'] ) ) {
return;
}
$options = Coderockz_Woo_Delivery_Delivery_Option::delivery_option( $settings );
if ( is_array( $options ) && 1 === count( $options ) ) {
$_POST['coderockz_woo_delivery_delivery_selection_box'] = array_key_first( $options );
}
}, 5 );
// --- client: refresh the select in place on every cart update ---
add_action( 'wp_footer', function () {
if ( is_admin() ) { return; }
?>
<script>
( function ( $ ) {
var CR_URL = <?php echo wp_json_encode( admin_url( 'admin-ajax.php' ) ); ?>;
var inFlight = false;
var lastChoice = '';
// Remember what the shopper picked so we can restore it after Aero
// fragment re-renders or an option list rebuild.
$( document ).on( 'change', '#coderockz_woo_delivery_delivery_selection_box', function () {
if ( $( this ).val() ) { lastChoice = $( this ).val(); }
} );
function crApply( data ) {
var $box = $( '#coderockz_woo_delivery_delivery_selection_box' );
if ( ! $box.length ) { return; }
var opts = data.options || {};
// Transient/failed evaluation: no options AND no restriction notice
// explaining why. Never wipe the shopper's state on that — a cleared
// select here is what causes "Please select order type" on submit.
if ( ! opts.hasOwnProperty( 'delivery' ) && ! opts.hasOwnProperty( 'pickup' ) && ! data.notice ) { return; }
var selected = $box.val() || lastChoice;
[ 'delivery', 'pickup' ].forEach( function ( key ) {
var $opt = $box.find( "option[value='" + key + "']" );
if ( opts.hasOwnProperty( key ) ) {
if ( ! $opt.length ) { $box.append( $( '<option>', { value: key, text: opts[ key ] } ) ); }
else { $opt.prop( 'disabled', false ); }
} else {
$opt.remove();
}
} );
$( '.coderockz_woo_delivery_dynamic_notice_check' ).remove();
if ( data.notice ) {
$( '#coderockz_woo_delivery_delivery_selection_field' ).append(
"<p class='coderockz_woo_delivery_dynamic_notice_check' style='color:#DD5246;font-size:12px;font-style:italic;font-weight:700;margin-top:-10px;margin-bottom:0;'>" + data.notice + "</p>"
);
}
if ( selected && ! opts.hasOwnProperty( selected ) ) {
$box.val( '' );
$( '#coderockz_woo_delivery_delivery_date_section, #coderockz_woo_delivery_delivery_time_section, #coderockz_woo_delivery_additional_field_section, #coderockz_woo_delivery_pickup_date_section, #coderockz_woo_delivery_pickup_time_section' ).hide();
$box.trigger( 'change' );
} else {
if ( selected && ! $box.val() ) {
// Selection was wiped (fragment re-render) but is still valid — restore
// it and fire the plugin's change handler so date/time sections return.
$box.val( selected ).trigger( 'change' );
}
$box.trigger( 'change.select2' ); // refresh the select2/selectWoo display
}
}
function crRefresh() {
if ( inFlight || ! $( '#coderockz_woo_delivery_delivery_selection_box' ).length ) { return; }
inFlight = true;
$.ajax( {
url: CR_URL, type: 'POST', data: { action: 'cr_relief_order_type' },
success: function ( r ) { if ( r && r.success && r.data ) { crApply( r.data ); } },
complete: function () { inFlight = false; }
} );
}
$( document.body ).on( 'updated_checkout', crRefresh );
} )( jQuery );
</script>
<?php
}, 99 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment