Created
April 12, 2021 18:46
-
-
Save cccamuseme/4aad4cd28756105577ce5d4e3d6012e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//additional_shipping_fee | |
//add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_add_cart_fee' ); | |
function bbloomer_add_cart_fee() { | |
$surcharge = 2; // 5% surcharge based on shipping cost | |
WC()->cart->add_fee( __('Handling', 'woocommerce'), $surcharge ); | |
} | |
add_filter('woocommerce_package_rates', 'custom_shipping_costs', 10, 2 ); | |
function custom_shipping_costs( $rates, $package ){ | |
$items = WC()->cart->get_cart(); | |
$price = 0; | |
foreach($items as $item => $values) { | |
$additional = get_post_meta($values['product_id'] , 'additional_shipping_fee', true); | |
if(!empty($additional) ){ | |
$price = $price + $additional; | |
} | |
} | |
// Loop through shipping methods rates | |
foreach ( $rates as $rate_key => $rate ){ | |
// Targeting all shipping methods except "Free shipping" | |
if ( 'free_shipping' !== $rate->method_id ) { | |
$has_taxes = false; | |
$taxes = []; | |
$rates[$rate_key]->cost = $price + $rates[$rate_key]->cost ; | |
// Taxes rate cost (if enabled) | |
foreach ($rates[$rate_key]->taxes as $key => $tax){ | |
if( $tax > 0 ){ | |
$has_taxes = true; | |
$taxes[$key] = 0; // Set to 0 (zero) | |
} | |
} | |
if( $has_taxes ) | |
$rates[$rate_key]->taxes = $taxes; | |
} | |
} | |
return $rates; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment