Is it possible to disable checkout if there is backorder item mixed with in stock items. The code so far is displaying message if there is mixed items in the cart, but they
Try the following, that will really check for mixed items and will throw an error message avoiding:
The code:
// Display a custom notice when mixed items (backorder items and normal) avoiding checkout and "proceed to checkout" too
add_action( 'woocommerce_checkout_process', 'display_custom_error_notice' );
add_action( 'woocommerce_check_cart_items', 'display_custom_error_notice' );
function display_custom_error_notice() {
$message = __("You have a PREORDER item/s mixed with normal items. They can not be mixed.", "woocommerce");
if ( has_mixed_products() )
wc_add_notice( $message, 'error' );
}
// Utility function checking for mixed items (backorder items and normal)
function has_mixed_products() {
$on_backorder = $normal = false;
foreach( WC()->cart->get_cart() as $cart_item ) {
if( $cart_item['data']->is_on_backorder() )
$on_backorder = true;
else $normal = true;
}
return $on_backorder && $normal ? true : false;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
On cart page:
On checkout page:
Now is also possible to remove mixed items from cart throwing a notice…
With woocommerce mostly everything is possible, depending on your skills and on time to spend.
What about this?
...
if ( es_check_cart_has_backorder_product() ) {
add_filter('woocommerce_order_button_html', 'sg_remove_payment_button');
}
...
function sg_remove_payment_button ($button){
$output = '<div id="payments-disabled">';
$output .= 'Sorry, you cannot complete this order';
$output .= '</div>';
$output .= '<style>';
$output .= '.payment_methods, .wc-terms-and-conditions {display: none !important}';
$output .= '</style>';
return $output;
}