3v4l.org

run code in 300+ PHP versions simultaneously
<?php add_action('woocommerce_cart_calculate_fees', 'vono_discount_based_on_cheapest_product', 10, 1); function vono_discount_based_on_cheapest_product($cart) { if (is_admin() && !defined('DOING_AJAX')) { return; } $discount = 0; $discount_percentage = 0; // Sepetteki ürün sayısını al $item_count = $cart->get_cart_contents_count(); // Sepetteki ürünleri fiyatlarına göre sırala $products = []; foreach ($cart->get_cart() as $cart_item) { $product = $cart_item['data']; $price = $product->get_price(); $products[] = [ 'price' => $price, 'cart_item' => $cart_item, ]; } // Fiyata göre sırala (en ucuzdan en pahalıya) usort($products, function ($a, $b) { return $a['price'] <=> $b['price']; }); // En ucuz ürünün fiyatını al $cheapest_product_price = $products[0]['price']; if ($item_count == 3) { // Sepette 3 ürün varsa %10 indirim $discount_percentage = 10; } elseif ($item_count >= 4) { // Sepette 4 veya daha fazla ürün varsa %15 indirim $discount_percentage = 15; } if ($discount_percentage > 0) { $discount = ($cheapest_product_price * $discount_percentage) / 100; $cart->add_fee(__("$discount_percentage% indirim (En ucuz ürün)", 'woocommerce'), -$discount); } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/InuXf
function name:  (null)
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   INIT_FCALL_BY_NAME                                       'add_action'
          1        SEND_VAL_EX                                              'woocommerce_cart_calculate_fees'
          2        SEND_VAL_EX                                              'vono_discount_based_on_cheapest_product'
          3        SEND_VAL_EX                                              10
          4        SEND_VAL_EX                                              1
          5        DO_FCALL                                      0          
   49     6      > RETURN                                                   1

Function vono_discount_based_on_cheapest_product:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 4, Position 2 = 7
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 9
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 77) Position 1 = 18, Position 2 = 29
Branch analysis from position: 18
2 jumps found. (Code = 78) Position 1 = 19, Position 2 = 29
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
Branch analysis from position: 29
2 jumps found. (Code = 43) Position 1 = 40, Position 2 = 42
Branch analysis from position: 40
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
2 jumps found. (Code = 43) Position 1 = 47, Position 2 = 61
Branch analysis from position: 47
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 61
Branch analysis from position: 42
2 jumps found. (Code = 43) Position 1 = 44, Position 2 = 45
Branch analysis from position: 44
2 jumps found. (Code = 43) Position 1 = 47, Position 2 = 61
Branch analysis from position: 47
Branch analysis from position: 61
Branch analysis from position: 45
Branch analysis from position: 29
Branch analysis from position: 7
filename:       /in/InuXf
function name:  vono_discount_based_on_cheapest_product
number of ops:  62
compiled vars:  !0 = $cart, !1 = $discount, !2 = $discount_percentage, !3 = $item_count, !4 = $products, !5 = $cart_item, !6 = $product, !7 = $price, !8 = $cheapest_product_price
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
    7     1        INIT_FCALL_BY_NAME                                       'is_admin'
          2        DO_FCALL                                      0  $9      
          3      > JMPZ_EX                                          ~10     $9, ->7
          4    >   DEFINED                                          ~11     'DOING_AJAX'
          5        BOOL_NOT                                         ~12     ~11
          6        BOOL                                             ~10     ~12
          7    > > JMPZ                                                     ~10, ->9
    8     8    > > RETURN                                                   null
   11     9    >   ASSIGN                                                   !1, 0
   12    10        ASSIGN                                                   !2, 0
   15    11        INIT_METHOD_CALL                                         !0, 'get_cart_contents_count'
         12        DO_FCALL                                      0  $15     
         13        ASSIGN                                                   !3, $15
   19    14        ASSIGN                                                   !4, <array>
   20    15        INIT_METHOD_CALL                                         !0, 'get_cart'
         16        DO_FCALL                                      0  $18     
         17      > FE_RESET_R                                       $19     $18, ->29
         18    > > FE_FETCH_R                                               $19, !5, ->29
   21    19    >   FETCH_DIM_R                                      ~20     !5, 'data'
         20        ASSIGN                                                   !6, ~20
   22    21        INIT_METHOD_CALL                                         !6, 'get_price'
         22        DO_FCALL                                      0  $22     
         23        ASSIGN                                                   !7, $22
   24    24        INIT_ARRAY                                       ~25     !7, 'price'
   25    25        ADD_ARRAY_ELEMENT                                ~25     !5, 'cart_item'
   23    26        ASSIGN_DIM                                               !4
   25    27        OP_DATA                                                  ~25
   20    28      > JMP                                                      ->18
         29    >   FE_FREE                                                  $19
   30    30        INIT_FCALL                                               'usort'
         31        SEND_REF                                                 !4
         32        DECLARE_LAMBDA_FUNCTION                          ~26     [0]
   32    33        SEND_VAL                                                 ~26
   30    34        DO_ICALL                                                 
   35    35        FETCH_DIM_R                                      ~28     !4, 0
         36        FETCH_DIM_R                                      ~29     ~28, 'price'
         37        ASSIGN                                                   !8, ~29
   37    38        IS_EQUAL                                                 !3, 3
         39      > JMPZ                                                     ~31, ->42
   39    40    >   ASSIGN                                                   !2, 10
   37    41      > JMP                                                      ->45
   40    42    >   IS_SMALLER_OR_EQUAL                                      4, !3
         43      > JMPZ                                                     ~33, ->45
   42    44    >   ASSIGN                                                   !2, 15
   45    45    >   IS_SMALLER                                               0, !2
         46      > JMPZ                                                     ~35, ->61
   46    47    >   MUL                                              ~36     !8, !2
         48        DIV                                              ~37     ~36, 100
         49        ASSIGN                                                   !1, ~37
   47    50        INIT_METHOD_CALL                                         !0, 'add_fee'
         51        INIT_FCALL_BY_NAME                                       '__'
         52        NOP                                                      
         53        FAST_CONCAT                                      ~39     !2, '%25+indirim+%28En+ucuz+%C3%BCr%C3%BCn%29'
         54        SEND_VAL_EX                                              ~39
         55        SEND_VAL_EX                                              'woocommerce'
         56        DO_FCALL                                      0  $40     
         57        SEND_VAR_NO_REF_EX                                       $40
         58        MUL                                              ~41     !1, -1
         59        SEND_VAL_EX                                              ~41
         60        DO_FCALL                                      0          
   49    61    > > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/InuXf
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $a, !1 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   31     2        FETCH_DIM_R                                      ~2      !0, 'price'
          3        FETCH_DIM_R                                      ~3      !1, 'price'
          4        SPACESHIP                                        ~4      ~2, ~3
          5      > RETURN                                                   ~4
   32     6*     > RETURN                                                   null

End of Dynamic Function 0

End of function vono_discount_based_on_cheapest_product

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
144.94 ms | 1011 KiB | 14 Q