如果购买的商品处于延期交货状态,则 WooCommerce 最低订购量

WooCommerce minimum order amount if an item purchased is on backorders

我正在使用 答案代码,它非常有用!

尽管如果放在购物车中的产品没有库存(延期交货),我希望只有最低订购量。如果购物车中的产品有库存,则不应设置最低订购量。有人可以帮我吗?

要使代码仅在存在延期交货商品时才起作用,您需要在代码中包含对延期交货商品的检查,如下所示:

add_action( 'woocommerce_check_cart_items', 'set_min_total_per_user_role' );
function set_min_total_per_user_role() {
    // Only run in the Cart or Checkout pages
    if( is_cart() || is_checkout() ) {

        // Set minimum cart total (by user role)
        $minimum_cart_total = current_user_can('company') ? 250 : 100;

        // Total (before taxes and shipping charges)
        $total = WC()->cart->subtotal;
        
        $has_backordered_items = false;
        
        // Check for backordered cart items
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            if ( $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
                $has_backordered_items = true;
                break; // stop the loop
            }
        }

        // Add an error notice is cart total is less than the minimum required
        if( $has_backordered_items && $total <= $minimum_cart_total  ) {
            // Display our error message
            wc_add_notice( sprintf( '<strong>Dear customer, minimum order of %s is required to make a purchase on your site.</strong> <br>
                Your actual cart amount is: %s',
                wc_price($minimum_cart_total),
                wc_price($total)
            ), 'error' );
        }
    }
}

代码进入活动子主题(或活动主题)的 functions.php 文件。它应该有效。

基于: