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.

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料