@@ -1022,17 +1022,49 @@ function ppom_update_get_prices() {
10221022 const apply_as_discount = ppom_pricematrix_discount == 'on' ? true : false ;
10231023
10241024 if ( ppom_pricematrix !== undefined ) {
1025- jQuery . each ( JSON . parse ( ppom_pricematrix ) , function ( range , meta ) {
1025+ const matrixData = JSON . parse ( ppom_pricematrix ) ;
1026+ const product_qty = parseInt ( ppom_get_order_quantity ( ) , 10 ) ;
1027+
1028+ // Pre-process keys ONCE (fix performance + ordering issue)
1029+ const parsedKeys = Object . keys ( matrixData ) . map ( ( range ) => {
1030+ if ( range . includes ( '-' ) ) {
1031+ const [ start , end ] = range . split ( '-' ) . map ( Number ) ;
1032+ return { type : 'range' , start, end, key : range } ;
1033+ } else {
1034+ return { type : 'single' , value : Number ( range ) , key : range } ;
1035+ }
1036+ } ) ;
1037+
1038+ parsedKeys . sort ( ( a , b ) => {
1039+ const aVal = a . type === 'range' ? a . start : a . value ;
1040+ const bVal = b . type === 'range' ? b . start : b . value ;
1041+ return aVal - bVal ;
1042+ } ) ;
1043+
1044+ const firstKey = parsedKeys [ 0 ] ?. key ;
1045+ const lastKey = parsedKeys [ parsedKeys . length - 1 ] ?. key ;
1046+
1047+ jQuery . each ( matrixData , function ( range , meta ) {
10261048 const option_price = { } ;
1049+ let isMatch = false ;
1050+
1051+ if ( range . indexOf ( '-' ) !== - 1 ) {
1052+ const [ range_from , range_to ] = range . split ( '-' ) . map ( Number ) ;
10271053
1028- const range_break = range . split ( '-' ) ;
1029- const range_from = parseInt ( range_break [ 0 ] ) ;
1030- const range_to = parseInt ( range_break [ 1 ] ) ;
1031- const product_qty = ppom_get_order_quantity ( ) ;
1054+ if ( product_qty >= range_from && product_qty <= range_to ) {
1055+ isMatch = true ;
1056+ }
1057+ } else {
1058+ const value = Number ( range ) ;
10321059
1033- // console.log(range, meta);
1060+ if ( range === firstKey && product_qty <= value ) {
1061+ isMatch = true ;
1062+ } else if ( range === lastKey && product_qty >= value ) {
1063+ isMatch = true ;
1064+ }
1065+ }
10341066
1035- if ( product_qty >= range_from && product_qty <= range_to ) {
1067+ if ( isMatch ) {
10361068 option_price . label = meta . label ;
10371069 option_price . price = meta . price ;
10381070 option_price . percent = meta . percent ;
@@ -1041,11 +1073,13 @@ function ppom_update_get_prices() {
10411073 ? 'matrix_discount'
10421074 : 'matrix' ;
10431075 option_price . data_name = ppom_pricematrix_id ;
1044- option_price . matrix_fixed =
1045- meta . matrix_fixed == 'on' ? true : false ;
1046- options_price_added . push ( option_price ) ;
1076+ option_price . matrix_fixed = meta . matrix_fixed === 'on' ;
1077+
1078+ options_price_added . push ( option_price ) ;
1079+
1080+ return false ;
10471081 }
1048- } ) ;
1082+ } ) ;
10491083 }
10501084
10511085 // Variation quantities
0 commit comments