Last active
February 29, 2024 13:41
-
-
Save BusinessBloomer/edac325b7c9034a509cf956ea6b541e9 to your computer and use it in GitHub Desktop.
Redirect to cart if last item is removed from checkout
This file contains 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
// REDIRECT TO SHOP ON EMPTY CHECKOUT | |
add_action( 'wp_footer', function() { | |
if ( is_checkout() ) { | |
$redirect_page_id = get_option( 'bbcpqcp_redirect', wc_get_page_id( 'shop' ) ); // or 'cart' | |
$redirect_page = get_permalink( $redirect_page_id ); | |
?> | |
<script> | |
jQuery( function($){ | |
jQuery(document.body).on( 'updated_checkout', function(event) { | |
var cart_item_count = 0; | |
var qty_field_inputs = document.getElementsByClassName("qty"); | |
for (var i = 0; i < qty_field_inputs.length; i++) { | |
cart_item_count = cart_item_count + Number( qty_field_inputs.item(i).value ); | |
} | |
if ( 0 == cart_item_count ) { | |
location.href = '<?php echo $redirect_page; ?>'; | |
} | |
}); | |
}); | |
</script> | |
<?php | |
} | |
}); | |
// CHECKOUT PATCH FOR CHANGE CHECKOUT QUANTITY PLUGIN | |
add_action( 'woocommerce_review_order_after_submit', function() { | |
echo '<input id="qtyhidden" type="hidden" name="qtyhidden" value="check">'; | |
wc_enqueue_js( " | |
$(document).on('change','input[name^=\'shipping_method_qty\']',function(){ | |
$('#qtyhidden').val($(this).parent().next().val()+':'+$(this).val()); | |
}); | |
"); | |
}); | |
add_action( 'woocommerce_checkout_update_order_review', function( $post_data ) { | |
parse_str( $post_data, $post_data_array_2 ); | |
$updated_qty = false; | |
foreach ( $post_data_array_2 as $key => $value ) { | |
if ( $key == 'qtyhidden' && $value !== 'check' ) { | |
$values = explode( ':', $value ); | |
WC()->cart->set_quantity( $values[0], $values[1], false ); | |
$updated_qty = true; | |
} | |
} | |
if ( $updated_qty ) { | |
WC()->cart->calculate_totals(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment