Avoid checkout for mixed backorder and normal items in Woocommerce

后端 未结 2 1806
一生所求
一生所求 2020-12-21 07:56

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

相关标签:
2条回答
  • 2020-12-21 08:22

    Try the following, that will really check for mixed items and will throw an error message avoiding:

    • "proceed to checkout" (in cart page)
    • placing order (checkout page)

    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.

    0 讨论(0)
  • 2020-12-21 08:26

    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;
    }
    
    0 讨论(0)
提交回复
热议问题