How to adjust Prices based on Product class in ELEX WooCommerce Australia Post Shipping Plugin with Print Label & Tracking?

You are here:

The following code snippet facilitates you to adjust prices of the service(s) for required Product classes. With this code snippet, you can adjust the price for the fixed amount that is assigned in the code snippet.

Add the following code to your functions.php of your website theme folder, or anywhere relevant.

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;
}

In the above code, $adjustments array contains services, product classes and the required price(fixed) which will be added/subtracted to adjust the cost. For existing Product class, $adjusted_price is updated and it is stored in $rate_service. After adjusting the value function returns $rate_service.

 


To explore more details about the plugins, go check out ELEX WooCommerce Australia Post Shipping Plugin with Print Label & Tracking.

Read the product setting up article to understand the plugin, in detail. Or check out the product documentation section for more related articles.

You can also check out other WooCommerce and WordPress plugins in ELEX.

    Leave a Reply

    WordPress Image Lightbox Plugin