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

基于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.

基本上我正试图让第二个 IF code>语句工作,如果标准为 第一个 If code>语句。 p>

fisrt IF code>语句用于检查用户角色以及是否与“bba”匹配 “或”duk“,然后第二个 IF code>语句将检查不应匹配的特定产品ID,以避免自定义购物车广泛批量折扣。 p>

我知道第二个 IF code>语句可以单独工作,但它会禁用所有用户的批量折扣,而不是特定的定义折扣。 p>

这是我的代码: p>

  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; 
} 
 
} 
} 
  code>  pre> 
 \  n 

或者我的第二次尝试也不起作用: p>

  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; 
} 
 
} 
} 
  code>  pre> 
 \  n 

使用这两个代码,产品ID 439和387的批量折扣被禁用,而不考虑第一个 IF code>语句。 p>

我如何制作 第一个 IF code>语句工作并检查目标用户角色? p>

感谢任何帮助。 p> div>

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.