Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果所购买的商品处于缺货状态,则为最低订单金额_Php_Wordpress_Woocommerce_Product_Cart - Fatal编程技术网

Php 如果所购买的商品处于缺货状态,则为最低订单金额

Php 如果所购买的商品处于缺货状态,则为最低订单金额,php,wordpress,woocommerce,product,cart,Php,Wordpress,Woocommerce,Product,Cart,我使用的答案代码,它的作品像一个魅力 尽管我只希望在购物车中的产品没有库存(缺货)的情况下有最低订购量。如果购物车中的产品有库存,则不应有最低订购量。有人能帮我吗?若要使代码仅在存在缺货项目时工作,您需要在代码中包含对缺货项目的检查,如下所示: add_action( 'woocommerce_check_cart_items', 'set_min_total_per_user_role' ); function set_min_total_per_user_role() { // On

我使用的答案代码,它的作品像一个魅力


尽管我只希望在购物车中的产品没有库存(缺货)的情况下有最低订购量。如果购物车中的产品有库存,则不应有最低订购量。有人能帮我吗?

若要使代码仅在存在缺货项目时工作,您需要在代码中包含对缺货项目的检查,如下所示:

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' );
        }
    }
}
add_action('woocommerce_check_cart_items','set_min_total_per_user_role');
函数集\最小\总计\每个用户\角色(){
//仅在购物车或结帐页面中运行
if(is_cart()| | is_checkout()){
//设置最低购物车总数(按用户角色)
$minimum_cart_total=当前用户_can(“公司”)?250:100;
//总计(税前和运费前)
$total=WC()->购物车->小计;
$has\u backordered\u items=false;
//检查缺货购物车项目
foreach(WC()->cart->get_cart()作为$cart_项目){
如果($cart\u item['data']->是延期交货($cart\u item['quantity'])){
$has\u backordered\u items=true;
break;//停止循环
}
}
//如果购物车总数小于所需的最小值,则添加错误通知

如果($has_backordered_items&&$total感谢您帮助我处理此代码!