Summarize:
I'm using the following code that allow customers know calculation of an extra cost depending on product weight and then add to the cart summary.
Problem:
Code rendundant. I think there is a way to obtain the same output with a more clean and anized code.
Description: This code add a cost name "Contributo costo pedane" on each unit depending from product weight setted up from woocommerce procuct page.
For each unit weight add 2,00 Euros
/*
*Add cart fees by cart total weight Add_pedane
*/
add_action( 'woocommerce_cart_calculate_fees', 'a_man_ltd_wc_add_cart_fees' );
if ( ! function_exists( 'a_man_ltd_wc_add_cart_fees' ) ) {
function a_man_ltd_wc_add_cart_fees( $cart ) {
$weight = $cart->get_cart_contents_weight();
if ( $weight <= 1 ) {
$name = 'Contributo costo pedane';
$amount = 2;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 1 and $weight <=2 ) {
$name = 'Contributo costo pedane';
$amount = 4;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 2 and $weight <=3 ) {
$name = 'Contributo costo pedane';
$amount = 6;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 3 and $weight <=4 ) {
$name = 'Contributo costo pedane';
$amount = 8;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 4 and $weight <=5 ) {
$name = 'Contributo costo pedane';
$amount = 10;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 5 and $weight <=6 ) {
$name = 'Contributo costo pedane';
$amount = 12;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 6 and $weight <=7 ) {
$name = 'Contributo costo pedane';
$amount = 14;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 7 and $weight <=8 ) {
$name = 'Contributo costo pedane';
$amount = 16;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 8 and $weight <=9 ) {
$name = 'Contributo costo pedane';
$amount = 18;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 9 and $weight <=10 ) {
$name = 'Contributo costo pedane';
$amount = 20;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 10 and $weight <=11 ) {
$name = 'Contributo costo pedane';
$amount = 22;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 11 and $weight <=12 ) {
$name = 'Contributo costo pedane';
$amount = 24;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 12 and $weight <=13 ) {
$name = 'Contributo costo pedane';
$amount = 26;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 13 and $weight <=14 ) {
$name = 'Contributo costo pedane';
$amount = 28;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 14 and $weight <=15 ) {
$name = 'Contributo costo pedane';
$amount = 30;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 15 and $weight <=16 ) {
$name = 'Contributo costo pedane';
$amount = 32;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 16 and $weight <=17 ) {
$name = 'Contributo costo pedane';
$amount = 34;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 17 and $weight <=18 ) {
$name = 'Contributo costo pedane';
$amount = 36;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 18 and $weight <=19 ) {
$name = 'Contributo costo pedane';
$amount = 38;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 19 and $weight <=20 ) {
$name = 'Contributo costo pedane';
$amount = 40;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 20 and $weight <=21 ) {
$name = 'Contributo costo pedane';
$amount = 42;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 21 and $weight <=22 ) {
$name = 'Contributo costo pedane';
$amount = 44;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 22 and $weight <=23 ) {
$name = 'Contributo costo pedane';
$amount = 46;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 23 and $weight <=24 ) {
$name = 'Contributo costo pedane';
$amount = 48;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 24 and $weight <=25 ) {
$name = 'Contributo costo pedane';
$amount = 50;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 25 and $weight <=26 ) {
$name = 'Contributo costo pedane';
$amount = 52;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 26 and $weight <=27 ) {
$name = 'Contributo costo pedane';
$amount = 54;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 27 and $weight <=28 ) {
$name = 'Contributo costo pedane';
$amount = 56;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 28 and $weight <=29 ) {
$name = 'Contributo costo pedane';
$amount = 58;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 29 and $weight <=30 ) {
$name = 'Contributo costo pedane';
$amount = 60;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 30 and $weight <=31 ) {
$name = 'Contributo costo pedane';
$amount = 62;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 31 and $weight <=32 ) {
$name = 'Contributo costo pedane';
$amount = 64;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 32 and $weight <=33 ) {
$name = 'Contributo costo pedane';
$amount = 66;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
}
}
Limit and expectation: In this example of coding posted below I have mapped costs for max weight 32-33 units manually but I think it is possible to do something oriented to infinite units with incremental steps of 2 Euros for each unit.
Summarize:
I'm using the following code that allow customers know calculation of an extra cost depending on product weight and then add to the cart summary.
Problem:
Code rendundant. I think there is a way to obtain the same output with a more clean and anized code.
Description: This code add a cost name "Contributo costo pedane" on each unit depending from product weight setted up from woocommerce procuct page.
For each unit weight add 2,00 Euros
/*
*Add cart fees by cart total weight Add_pedane
*/
add_action( 'woocommerce_cart_calculate_fees', 'a_man_ltd_wc_add_cart_fees' );
if ( ! function_exists( 'a_man_ltd_wc_add_cart_fees' ) ) {
function a_man_ltd_wc_add_cart_fees( $cart ) {
$weight = $cart->get_cart_contents_weight();
if ( $weight <= 1 ) {
$name = 'Contributo costo pedane';
$amount = 2;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 1 and $weight <=2 ) {
$name = 'Contributo costo pedane';
$amount = 4;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 2 and $weight <=3 ) {
$name = 'Contributo costo pedane';
$amount = 6;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 3 and $weight <=4 ) {
$name = 'Contributo costo pedane';
$amount = 8;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 4 and $weight <=5 ) {
$name = 'Contributo costo pedane';
$amount = 10;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 5 and $weight <=6 ) {
$name = 'Contributo costo pedane';
$amount = 12;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 6 and $weight <=7 ) {
$name = 'Contributo costo pedane';
$amount = 14;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 7 and $weight <=8 ) {
$name = 'Contributo costo pedane';
$amount = 16;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 8 and $weight <=9 ) {
$name = 'Contributo costo pedane';
$amount = 18;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 9 and $weight <=10 ) {
$name = 'Contributo costo pedane';
$amount = 20;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 10 and $weight <=11 ) {
$name = 'Contributo costo pedane';
$amount = 22;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 11 and $weight <=12 ) {
$name = 'Contributo costo pedane';
$amount = 24;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 12 and $weight <=13 ) {
$name = 'Contributo costo pedane';
$amount = 26;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 13 and $weight <=14 ) {
$name = 'Contributo costo pedane';
$amount = 28;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 14 and $weight <=15 ) {
$name = 'Contributo costo pedane';
$amount = 30;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 15 and $weight <=16 ) {
$name = 'Contributo costo pedane';
$amount = 32;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 16 and $weight <=17 ) {
$name = 'Contributo costo pedane';
$amount = 34;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 17 and $weight <=18 ) {
$name = 'Contributo costo pedane';
$amount = 36;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 18 and $weight <=19 ) {
$name = 'Contributo costo pedane';
$amount = 38;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 19 and $weight <=20 ) {
$name = 'Contributo costo pedane';
$amount = 40;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 20 and $weight <=21 ) {
$name = 'Contributo costo pedane';
$amount = 42;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 21 and $weight <=22 ) {
$name = 'Contributo costo pedane';
$amount = 44;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 22 and $weight <=23 ) {
$name = 'Contributo costo pedane';
$amount = 46;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 23 and $weight <=24 ) {
$name = 'Contributo costo pedane';
$amount = 48;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 24 and $weight <=25 ) {
$name = 'Contributo costo pedane';
$amount = 50;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 25 and $weight <=26 ) {
$name = 'Contributo costo pedane';
$amount = 52;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 26 and $weight <=27 ) {
$name = 'Contributo costo pedane';
$amount = 54;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 27 and $weight <=28 ) {
$name = 'Contributo costo pedane';
$amount = 56;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 28 and $weight <=29 ) {
$name = 'Contributo costo pedane';
$amount = 58;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 29 and $weight <=30 ) {
$name = 'Contributo costo pedane';
$amount = 60;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 30 and $weight <=31 ) {
$name = 'Contributo costo pedane';
$amount = 62;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 31 and $weight <=32 ) {
$name = 'Contributo costo pedane';
$amount = 64;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
if ( $weight > 32 and $weight <=33 ) {
$name = 'Contributo costo pedane';
$amount = 66;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
}
}
Limit and expectation: In this example of coding posted below I have mapped costs for max weight 32-33 units manually but I think it is possible to do something oriented to infinite units with incremental steps of 2 Euros for each unit.
Share Improve this question edited Mar 20 at 17:42 LoicTheAztec 255k24 gold badges399 silver badges446 bronze badges asked Mar 20 at 17:03 NightmareNightmare 114 bronze badges 1 |1 Answer
Reset to default 3You can calculate the fee dynamically. Here's a more clean and anized version of your code:
/*
* Add cart fees by cart total weight Add_pedane
*/
add_action('woocommerce_cart_calculate_fees', 'a_man_ltd_wc_add_cart_fees');
if (!function_exists('a_man_ltd_wc_add_cart_fees')) {
function a_man_ltd_wc_add_cart_fees($cart) {
$weight = $cart->get_cart_contents_weight();
if ($weight > 0) {
$fee_per_unit = 2; // 2 Euros per unit
$fee_amount = ceil($weight) * $fee_per_unit; // Calculate the fee based on the weight
$name = 'Contributo costo pedane';
$taxable = true;
$tax_class = '';
$cart->add_fee($name, $fee_amount, $taxable, $tax_class);
}
}
}
In this case only validated the weight is positive, because in real life not exist a product with negative weight, but you can validate before if the weight is negative re-writte it to value 1.
Dynamic Fee Calculation: Instead of manually mapping each weight range, the fee is calculated dynamically using the formula ceil($weight) * $fee_per_unit
. This ensures that the fee is calculated for any weight value, not just up to 33 units.
ceil($weight)
: This function rounds up the weight to the nearest whole number.
Some recommendations: The $cart should have if is taxable or not, but it depends of your logic business.
$amount = ceil($weight) * 2;
– Iłya Bursov Commented Mar 20 at 17:25