Skip to content

Instantly share code, notes, and snippets.

@WhiteHatJoker
Created October 18, 2021 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WhiteHatJoker/7b9a11bd6dbb7b7eba0112e69ddb9f75 to your computer and use it in GitHub Desktop.
Save WhiteHatJoker/7b9a11bd6dbb7b7eba0112e69ddb9f75 to your computer and use it in GitHub Desktop.
Add fee for specific products while adjusting the shipping costs in the cart

Add fee for specific products while adjusting the shipping costs in the cart

If you have a specific product that you would like to have a fixed fee for and adjust the shipping cost not to take that product into account accordingly, then you need the code from this repository. There are two seperate functions involved:

  1. function wc_ninja_manipulate_shipping which takes care of adjusting the shipping cost not to take into account a particular vendor products or product
  2. function woocommerce_custom_surcharge that adds $30 surcharge fee below the shipping methods on its own line. Keep in mind: I am using the Table Rate Shipping plugin as well as my shipping costs are based upon order total for this example.

Installation for one product

  1. Update line 27 with your own product ID.
  2. Update line 40 by removing $tsar_check || as you only need to check for a product.
  3. Update line 42 by removing - $tsar_products_total.
  4. Set your own rates on lines 43-67 based on what is the adjusted_subtotal.
  5. Change code on lines 68-101 based on the IDs you have for shipping methods (mine is set as such by Table Rate Shipping plugin).
  6. Again update line 125 with your own product ID.
  7. Finally, on line 136 update 'Tonewood Shipping Fee', $fixed with 'Your preffered naming', fee amount.
  8. Now you can remove unnecessary lines of code lines 15-16, 33-37, 116, 129-131, 135-137.

Installation for products from one vendor

  1. Update line 33 with your own vendor ID.
  2. Update line 40 by removing || $tonewood_check as you only need to check for vendor products.
  3. Update line 42 by removing - $tonewood_products_cost.
  4. Set your own rates on lines 43-67 based on what is the adjusted_subtotal.
  5. Change code on lines 68-101 based on the IDs you have for shipping methods (mine is set as such by Table Rate Shipping plugin).
  6. Again update line 129 with your own vendor ID.
  7. Finally, on line 139 update 'Tsar Nicoulai Shipping Fee', $fixed with 'Your preffered naming', fee amount.
  8. Now you can remove unnecessary lines of code [lines 13, 14] (functions.php#L13-L14), [27-31] (functions.php#L27-L31), 115, 125-127, 138-140.
<?php
//Exclude Tsar Nicoulai products from subtotal to adjust shipping cost
//Exclude Tonewood Adoption products from subtotal to adjust shipping cost
function wc_ninja_manipulate_shipping( $rates, $package ) {
// All available rates and their costs
//print_r($rates);
// All products in the cart and their costs
//print_r($package);
$tonewood_products_cost = 0;
$tonewood_check = false;
$tsar_products_total = 0;
$tsar_check = false;
//Add new products total=0 and check=false here
$adj_cart_subtotal = 0;
$new_std_rate = 0;
$new_day_rate = 0;
$new_over_rate = 0;
$cart_products = $package['contents'];
// Loop through each product in the cart
foreach ( $cart_products as $product ) {
//Create new conditionals for new products in cart below
if ( $product['product_id'] =='29543' ) {
// If product ID 29543 is in cart or if Tonewood in cart remember its line total and turn tonewood_check true
$tonewood_products_cost = $product['line_total'];
$tsar_check = true;
}
if ($product['data']->post->post_author == 40) {
//If products with vendor id of 40 are in the cart, store the total of their costs and turn tsar_check true
$tsar_products_total = $tsar_products_total + $product['line_total'];
$tsar_check = true;
}
}
//Update the line below with boolean for new products
if ($tsar_check || $tonewood_check) {
//Update the line below with totals variable for new product
$adj_cart_subtotal = WC()->cart->subtotal - $tonewood_products_cost - $tsar_products_total;
if ($adj_cart_subtotal>=1 && $adj_cart_subtotal<=24) {
$new_std_rate=7.5;
$new_day_rate=17.5;
$new_over_rate=27.5;
} elseif ($adj_cart_subtotal>=25 && $adj_cart_subtotal<=39) {
$new_std_rate=10;
$new_day_rate=20;
$new_over_rate=30;
} elseif ($adj_cart_subtotal>=40 && $adj_cart_subtotal<=79) {
$new_std_rate=12.5;
$new_day_rate=22.5;
$new_over_rate=32.5;
} elseif ($adj_cart_subtotal>=80 && $adj_cart_subtotal<=149) {
$new_std_rate=15;
$new_day_rate=25;
$new_over_rate=35;
} else {
$new_std_rate=0.11*$adj_cart_subtotal;
$new_day_rate=0.2*$adj_cart_subtotal;
$new_over_rate=0.3*$adj_cart_subtotal;
}
//Update with your own shipping method ids
if (isset($rates['table_rate-9 : 23'])) {
$rates['table_rate-9 : 23']->cost = $new_over_rate;
} elseif (isset($rates['table_rate-9 : 22'])) {
$rates['table_rate-9 : 22']->cost = $new_over_rate;
} elseif (isset($rates['table_rate-9 : 21'])) {
$rates['table_rate-9 : 21']->cost = $new_over_rate;
} elseif (isset($rates['table_rate-9 : 20'])) {
$rates['table_rate-9 : 20']->cost = $new_over_rate;
} elseif (isset($rates['table_rate-9 : 19'])) {
$rates['table_rate-9 : 19']->cost = $new_over_rate;
}
if (isset($rates['table_rate-8 : 18'])) {
$rates['table_rate-8 : 18']->cost = $new_day_rate;
} elseif (isset($rates['table_rate-8 : 17'])) {
$rates['table_rate-8 : 17']->cost = $new_day_rate;
} elseif (isset($rates['table_rate-8 : 16'])) {
$rates['table_rate-8 : 16']->cost = $new_day_rate;
} elseif (isset($rates['table_rate-8 : 15'])) {
$rates['table_rate-8 : 15']->cost = $new_day_rate;
} elseif (isset($rates['table_rate-8 : 14'])) {
$rates['table_rate-8 : 14']->cost = $new_day_rate;
}
if (isset($rates['table_rate-7 : 13'])) {
$rates['table_rate-7 : 13']->cost = $new_std_rate;
} elseif (isset($rates['table_rate-7 : 12'])) {
$rates['table_rate-7 : 12']->cost = $new_std_rate;
} elseif (isset($rates['table_rate-7 : 11'])) {
$rates['table_rate-7 : 11']->cost = $new_std_rate;
} elseif (isset($rates['table_rate-7 : 10'])) {
$rates['table_rate-7 : 10']->cost = $new_std_rate;
} elseif (isset($rates['table_rate-7 : 9'])) {
$rates['table_rate-7 : 9']->cost = $new_std_rate;
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'wc_ninja_manipulate_shipping', 10, 2 );
/* Tsar Nicoulai Handling Fee*/
/*Tonewood Adoption Fee*/
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
$tonewoodfee = false;
$tsarfee = false;
//Add new check for a new product here
$fixed = 30;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
//Add the check for a new product in the cart
if ($values['data']->id == 29543) {
$tonewoodfee= true;
}
if ($values['data']->post->post_author == 40) {
$tsarfee= true;
}
}
if ($tsarfee) {
$woocommerce->cart->add_fee( 'Tsar Nicoulai Shipping Fee', $fixed, true, '' );
}
if ($tonewoodfee) {
$woocommerce->cart->add_fee( 'Tonewood Shipping Fee', $fixed, true, '' );
}
//Add a conditional for new products in the cart as above
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment