Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

php - Avoid checkout for mixed backorder and normal items in Woocommerce

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

enter image description here

On checkout page:

enter image description here


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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...