How I can count with iteration for my product quantity if it exceeds more than can fit in one pallet.
Example one pallet limit is 100 and if that product quantity exceeds 101 then it adds 2 iteration like second pallet.
My code:
$tkp = 1;
foreach( WC()->cart->get_cart() as $cart_item ){
if (!empty(get_post_meta($cart_item['variation_id'], '_number_field', true))) { //this checks if pallet price is empty or not.
$a = $cart_item['quantity']; // these are products with pallets already.
} else {
$atk = strstr(get_post_meta($cart_item['variation_id'], '_alus_al', true), "units", true); // it checks how many products can be in pallet for this variation (ex. 100 units)
if ($cart_item['quantity'] > $atk) {
$tk = $tkp++;
}
$tkc = $tk * 8; // 8 is pallet price.
}
$calc = $a * 8 + $tkc;
WC()->cart->add_fee(__('Alused', 'txtdomain'), $calc);
}
My Answer:
You can modify your code to count the number of times the quantity exceeds the pallet limit by adding a counter variable and incrementing it whenever the condition is met. Here's an updated version of your code:
php
$tkp = 1;
$exceedCount = 0; // Counter for counting how many times quantity exceeds the pallet limit
foreach( WC()->cart->get_cart() as $cart_item ){
if (!empty(get_post_meta($cart_item['variation_id'], '_number_field', true))) { //this checks if pallet price is empty or not.
$a = $cart_item['quantity']; // these are products with pallets already.
} else {
$atk = strstr(get_post_meta($cart_item['variation_id'], '_alus_al', true), "units", true); // it checks how many products can be in pallet for this variation (ex. 100 units)
if ($cart_item['quantity'] > $atk) {
$exceedCount++; // Increment the counter if quantity exceeds the pallet limit
}
$tkc = $tkp * 8; // 8 is pallet price.
}
$calc = $a * 8 + $tkc;
WC()->cart->add_fee(__('Alused', 'txtdomain'), $calc);
}
echo "Number of times quantity exceeds pallet limit: " . $exceedCount;
This code will now keep track of how many times the quantity exceeds the pallet limit and store that count in the `$exceedCount` variable. You can then use this variable to display the count or perform any other actions based on the number of times the limit is exceeded.
Rate this post
4 of 5 based on 8160 votesComments