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 still can checkout the order.
We are using Preorder plugin and on the settings, the preorder and onhand cant be mixed in cart. Below are settings of plugin.
Prevent mixing products If you enable this option, the cart cannot contain Pre-Order products and regular products at the same time.(enabled) But it only work if there is no items present in the cart.
Allow sales of out of stock products By enabling this option, Pre-Order products with no stock can be purchased. (enabled and allowed backorder) All items can be Preorder once stocks become zero.
Problem is if there is already items in cart they can checkout preorder and regular product. Please check example below
I put Product A(5 stocks) and B(10 stocks) in the cart but I dont want to checkout right away.
Then some one purchased the Product A and stocks become 0 (and Product A turn to preorder)
But if I proceed to checkout Product A(0 stocks and preorder) and B(10 stocks)so its already mixed in cart and I can proceed to checkout because backorder is allowed in settings.
Is it possible to automatically delete the Product A in cart or disable checkout?
add_action( 'woocommerce_review_order_before_payment', 'es_checkout_add_cart_notice' );
function es_checkout_add_cart_notice() {
$message = "You have a PREORDER item/s in your cart! Do not mix it if you're ordering on-hand item/s or IGNORE this message if you are ordering all pre-order item/s.";
if ( es_check_cart_has_backorder_product() )
wc_add_notice( $message, 'error' );
}
function es_check_cart_has_backorder_product() {
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$cart_product = wc_get_product( $values['data']->get_id() );
if( $cart_product->is_on_backorder() )
return true;
}
return false;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…