Last active
September 19, 2019 06:53
-
-
Save xadapter/288aff384db2a04fb3b96ec53239dbc0 to your computer and use it in GitHub Desktop.
Snippet to add handling charge to WooCommerce Shipping Methods based on Shipping Class. PluginHive WooCommerce Shipping Plugins - https://www.pluginhive.com/product/multiple-carrier-shipping-plugin-woocommerce/, https://www.pluginhive.com/product/woocommerce-ups-shipping-plugin-with-print-label/, https://www.pluginhive.com/product/woocommerce-fe…
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
/** | |
* Snippet to add handling charge to WooCommerce Shipping Methods based on Shipping Class. | |
* Created at : 06 Aug 2018 | |
* Updated at : 06 Aug 2018 | |
* PluginHive Plugins : https://www.pluginhive.com/plugins/ | |
* Gist Link : https://gist.github.com/xadapter/288aff384db2a04fb3b96ec53239dbc0 | |
*/ | |
add_filter( 'woocommerce_package_rates', 'ph_add_extra_cost_based_on_shipping_class', 10, 2); | |
if( ! function_exists('ph_add_extra_cost_based_on_shipping_class') ) { | |
function ph_add_extra_cost_based_on_shipping_class( $shipping_rates, $package ){ | |
$handling_fee_based_on_shipping_class = array( | |
array( | |
'shipping_classes' => array( 'smart_phone', 'test'), // Shipping Class slug array | |
'adjustment' => 70, // Adjustment | |
), | |
array( | |
'shipping_classes' => array( 'shoe', 'electronic' ), | |
'adjustment' => 50, | |
), | |
); | |
$shipping_method_ids = array( 'wf_fedex_woocommerce_shipping' ); // Shipping methods on which adjustment has to be applied | |
$adjustment = null; | |
foreach( $package['contents'] as $line_item ) { | |
$line_item_shipping_class = $line_item['data']->get_shipping_class(); | |
if( ! empty($line_item_shipping_class) ) { | |
foreach( $handling_fee_based_on_shipping_class as $adjustment_data ) { | |
if( in_array( $line_item_shipping_class, $adjustment_data['shipping_classes']) ) { | |
$adjustment = ( $adjustment_data['adjustment'] > $adjustment ) ? $adjustment_data['adjustment'] : $adjustment; | |
} | |
} | |
} | |
} | |
if( ! empty($adjustment) ) { | |
foreach( $shipping_rates as $shipping_rate ) { | |
$shipping_method_id = $shipping_rate->get_method_id(); | |
if( in_array($shipping_method_id, $shipping_method_ids) ) { | |
$shipping_rate->set_cost( (float) $shipping_rate->get_cost() + $adjustment ); | |
} | |
} | |
} | |
return $shipping_rates; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment