0
votes

I know I can add a fee to the cart using the below code, but it just adds a generic fee to the cart. Woocommerce recognizes shipping specific fees and I am wondering if there is a way to make the fees addes via the below method something that woocommerce sees as a "shipping" specific fee. Some sort of metadata that gets added to shipping specific fees. our API is not recognizing these fees as shipping charges and therefore does not log them and that is what I am trying to resolve.

add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
  global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $percentage = 0.01;
    $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;    
    $woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );

}
1
There is no shipping fees (no methods for the related WC classes), but you can add a fee directly to the cost of shipping methods like "flat rate", "local pickup" and some third party shipping methods via specific hooks. But they will not appear as a separated "shipping fee" in the totals like the cart fee…LoicTheAztec
why not make it as shipping fee instead? can you elaborate more?Reigel
my other question was the result of a php script that restricts URL's acting up; once I removed it it ran fine and as such it was not a woocommerce related issue. thanks for the feedback!Eolis
@LoicTheAztec I am just looking at un-deleting it now. if there is a solution logged will be helpfulEolis

1 Answers

1
votes

You can do something like this:

add_filter( 'woocommerce_package_rates', 'woocommerce_package_rates', 10, 2 );
function woocommerce_package_rates( $rates, $package ) {

    $percentage = 0.01;

    foreach($rates as $key => $rate ) {
        $surcharge = ( wc()->cart->cart_contents_total + $rates[$key]->cost ) * $percentage;
        $rates[$key]->label .= ": {$rates[$key]->cost} + Fee: {$surcharge}";
        $rates[$key]->cost += $surcharge;
    }
    return $rates;
}

add the surcharge to the rates of shipping. Code above will produce something like this:

reigelgallarde.me

More about shipping fees here: http://reigelgallarde.me/programming/woocommerce-shipping-fee-changes-condition-met/