Created
May 13, 2016 14:47
-
-
Save WooForce/d1c1058f0358f30b728715613654ec28 to your computer and use it in GitHub Desktop.
Australia Post - Price adjustment based on shipping class
This file contains hidden or 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
add_filter( 'wf_australia_post_rate_services', 'alter_service_rate_adjustments', 10, 2 ); | |
function alter_service_rate_adjustments( $rate_service, $rate_code){ | |
// Array of service codes with combination of shipping class and price adjustment | |
$adjustments = array( | |
'AUS_PARCEL_REGULAR' => array( | |
'16' => 4, // Shipping Class ID => Adjustment Price | |
'17' => 3, | |
), | |
'AUS_PARCEL_EXPRESS' => array( | |
'16' => 20, | |
'17' => 21, | |
), | |
); | |
$existing_class_ids = array(); | |
foreach( WC()->cart->cart_contents as $key => $values ) { | |
$class_id = $values[ 'data' ]->get_shipping_class_id(); | |
if($class_id && !in_array($class_id, $existing_class_ids)){ | |
$existing_class_ids[] = $class_id; | |
} | |
} | |
if(array_key_exists($rate_code,$adjustments)){ | |
$class_adjustments = $adjustments[$rate_code]; | |
$adjusted = false; | |
$adjusted_price = 0; | |
foreach($existing_class_ids as $existing_class_id){ | |
if(array_key_exists($existing_class_id,$class_adjustments)){ | |
if($class_adjustments[$existing_class_id] > $adjusted_price){ | |
$adjusted_price = $class_adjustments[$existing_class_id]; | |
$adjusted = true; | |
} | |
} | |
} | |
if($adjusted){ | |
$rate_service['adjustment'] = $adjusted_price; | |
} | |
} | |
return $rate_service; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment