Skip to content

Instantly share code, notes, and snippets.

@pramodjodhani
Last active November 14, 2024 11:55
Show Gist options
  • Save pramodjodhani/a0f99afe2866db45f859c12e157e548f to your computer and use it in GitHub Desktop.
Save pramodjodhani/a0f99afe2866db45f859c12e157e548f to your computer and use it in GitHub Desktop.
Iconic WDS - Dynamically update the fee based on the current day and same day
<?php
add_action( 'woocommerce_cart_calculate_fees', 'iconic_wds_dynamically_add_custom_fee', 9 );
function iconic_wds_dynamically_add_custom_fee() {
global $iconic_wds;
if ( empty( $iconic_wds ) ) {
return;
}
$chosen_method = $iconic_wds->get_chosen_shipping_method();
// Todo change the fees.
$same_day_fees_amount = 10;
$saturday_fees_amount = 21;
// If the chosen method is flat rate, set the fee.
if ( strpos( $chosen_method, 'flat_rate' ) !== false ) {
WC()->session->set( 'jckwds_same_day_fee', $same_day_fees_amount );
} else {
WC()->session->__unset( 'jckwds_same_day_fee' );
}
if ( ! isset( $_POST['post_data'] ) ) {
return;
}
$checkout_fields = array();
parse_str( $_POST['post_data'], $checkout_fields );
if ( empty( $checkout_fields['jckwds-delivery-date-ymd'] ) ) {
return;
}
$delivery_date_ymd = $checkout_fields['jckwds-delivery-date-ymd'];
$delivery_date = DateTime::createFromFormat( 'Ymd', $delivery_date_ymd );
// if $delivery_date is saturday, set the saturday fee.
if ( $delivery_date->format( 'N' ) == 6 ) {
WC()->session->set( 'jckwds_day_fee', $saturday_fees_amount );
} else {
WC()->session->__unset( 'jckwds_day_fee' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment