dsf12123 2018-10-09 11:32
浏览 66
已采纳

基于Woocommerce中嵌套IF语句中的多个数组的条件

Basically I'm trying to get the second IF statement to work if the criteria of the first If statement is met.

The fisrt IF statement is meant to check against user roles and if it matches with "bba" or "duk", then then second IF statement will check for specific product ids that should not match avoiding a custom cart wide bulk discount.

I know the second IF statement works as it works by itself but it disables the bulk discount for all users instead of specific defined ones.

This is my code:

if ( in_array( 'bba', 'duk' (array) $user->roles ) ) {

    if( ! in_array($values['product_id'], array('493','387'))){
      $quantiy_total += $values['quantity'];  
      //$price = get_post_meta($values['product_id'] , '_price', true);

      $price = $values['line_subtotal'];

     // echo "####".$price."####";


      $cart_total += $price;
    }   

  }
  }

Or my second attempt that doesn’t work either:

if ( ! in_array($user['roles'], array('bba','duk'))){

    if( ! in_array($values['product_id'], array('493','387'))){
      $quantiy_total += $values['quantity'];  
      //$price = get_post_meta($values['product_id'] , '_price', true);

      $price = $values['line_subtotal'];

     // echo "####".$price."####";


      $cart_total += $price;
    }   

  }
  }

With both codes the bulk discount is disabled for product IDs 439 and 387 without taking into account the First IF statement.

How can I make the First IF statement working and checking for the targeted user roles?

Any help is appreciated.

  • 写回答

1条回答 默认 最新

  • doupi8598 2018-10-09 14:17
    关注

    In your first IF with 2 arrays you need to use array_intersect() instead of in_array().

    There is also some other mistakes. Try the following:

    $cart = WC()->cart; // (If needed) The cart object
    
    $total_quantity = $total_amount = 0; // Initializing variables
    
    if ( array_intersect( array('bba','duk'),  $user['roles'] ) ) {
        // Loop through cart items
        foreach( $cart-get_cart() as $cart_item )
            if ( ! in_array( $cart_item['data']->get_id(), array('493','387') ) ) {
                // Cumulated total quantity of targeted cart items
                $total_quantity += $cart_item['quantity'];
    
                // The product price
                // $price = $cart_item['data']->get_price();
    
                // The line item subtotal not discounted (product price x quantity)
                $subtotal_price = $cart_item['line_subtotal'];
    
                // The line item total discounted ( (product price x quantity) - coupon discount )
                // $total_price = $cart_item['line_total'];
    
                // Cumulated line subtotals amount of targeted cart items
                $subtotal_amount += $subtotal_price;
            }
        }
    }
    
    // Testing output
    echo '<p>Total quantity is ' . $total_quantity . ' and Subtotal amount is ' . $subtotal_amount . '</p>';
    

    It should better work now.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改