Skip to content

Instantly share code, notes, and snippets.

@vovadocent
Last active February 22, 2017 13:30
Show Gist options
  • Save vovadocent/54ad518746cc044d33423c1a396b0147 to your computer and use it in GitHub Desktop.
Save vovadocent/54ad518746cc044d33423c1a396b0147 to your computer and use it in GitHub Desktop.
Cart Delivery Fee
<?php
add_action('wp_ajax_myCartDeliveryFee', 'myCartDeliveryFee');
add_action('wp_ajax_nopriv_myCartDeliveryFee', 'myCartDeliveryFee');
function myCartDeliveryFee() {
$fee = isset($_POST['delivery_fee']) && !empty($_POST['delivery_fee']) ? filter_input(INPUT_POST, 'delivery_fee') : 0;
session_start();
$_SESSION['cart_fee_val'] = floatval(str_replace('$', '', $fee));
}
add_action('woocommerce_cart_calculate_fees', 'woo_add_cart_fee');
function woo_add_cart_fee() {
session_start();
$extracost = $_SESSION['cart_fee_val'];
WC()->cart->add_fee('Deivery Fee', $extracost);
}
?>
<script>
function setDeliveryFee(fee) {
$.ajax({
type: "POST",
url: $('html').data('admin'),
data: 'action=myCartDeliveryFee&delivery_fee=' + fee,
success: function (resp) {
$('.cart_form [name="update_cart"]:eq(0)').removeAttr('disabled').click();
}
});
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment