Avoid checkout for mixed backorder and normal items in Woocommerce

筅森魡賤 提交于 2019-12-11 01:18:55

问题


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;
}

回答1:


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.




回答2:


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;
}


来源:https://stackoverflow.com/questions/51280740/avoid-checkout-for-mixed-backorder-and-normal-items-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!