Skip to content

Instantly share code, notes, and snippets.

@varun-pluginhive
Created January 2, 2019 14:04
Show Gist options
  • Save varun-pluginhive/82552099b55a500c4844df65d3e48723 to your computer and use it in GitHub Desktop.
Save varun-pluginhive/82552099b55a500c4844df65d3e48723 to your computer and use it in GitHub Desktop.
Snippet to Add Handling Fee based on number of UPS Packages.
/**
* Snippet to Add Handling Fee based on number of UPS Packages.
* Created at : 02 Jan 2018
* Updated at : 02 Jan 2018
* PluginHive Plugins : https://www.pluginhive.com/plugins/
* Gist Link : https://gist.github.com/varun-pluginhive/82552099b55a500c4844df65d3e48723
*/
// Get the number of ups packages and set in session
add_filter( 'ph_ups_generated_packages', function( $ups_packages ) {
global $woocommerce;
if( ! is_admin() ) $woocommerce->session->__set( 'ph_ups_packages_count', count($ups_packages) );
return $ups_packages;
} );
// Add the Handling Fee
add_action( 'woocommerce_cart_calculate_fees', function() {
$handling_fee_per_package = 3.18; // Set Handling fee per Ups Package
global $woocommerce;
if ( is_admin() )
return;
$ups_package_count = $woocommerce->session->__get( 'ph_ups_packages_count' );
if( empty($ups_package_count) ) $ups_package_count = 1;
$fee = $handling_fee_per_package * $ups_package_count;
$woocommerce->cart->add_fee( 'Handling Fee', $fee, true, 'standard' );
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment