Skip to content

Instantly share code, notes, and snippets.

@ejntaylor
Created September 17, 2014 15:12
Show Gist options
  • Save ejntaylor/d7b0275018d8b78a5492 to your computer and use it in GitHub Desktop.
Save ejntaylor/d7b0275018d8b78a5492 to your computer and use it in GitHub Desktop.
WooCommerce Shipping Calculated after Coupon
<?php
// WooCommerce Shipping Calculated after Coupon
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'filter_shipping', 10, 2 );
function filter_shipping( $is_available, $package ) {
if ( WC()->cart->prices_include_tax )
$total = WC()->cart->cart_contents_total + array_sum( WC()->cart->taxes );
else
$total = WC()->cart->cart_contents_total;
$total = $total - ( WC()->cart->get_order_discount_total() + WC()->cart->get_cart_discount_total() );
// You can hardcode the number or get the setting from the shipping method
$shipping_settings = get_option('woocommerce_free_shipping_settings');
$min_total = $shipping_settings['min_amount'] > 0 ? $shipping_settings['min_amount'] : 0;
if ( 50 > $total ) {
$is_available = false;
}
return $is_available;
}
// This basically recalculates totals after the discount has been added
add_action( 'woocommerce_calculate_totals', 'change_shipping_calc' );
function change_shipping_calc( $cart ) {
$packages = WC()->cart->get_shipping_packages();
// Calculate costs for passed packages
$package_keys = array_keys( $packages );
$package_keys_size = sizeof( $package_keys );
for ( $i = 0; $i < $package_keys_size; $i ++ ) {
unset( $packages[ $package_keys[ $i ] ]['rates'] );
$package_hash = 'wc_ship_' . md5( json_encode( $packages[ $package_keys[ $i ] ] ) );
delete_transient( $package_hash );
}
// Calculate the Shipping
$cart->calculate_shipping();
// Trigger the fees API where developers can add fees to the cart
$cart->calculate_fees();
// Total up/round taxes and shipping taxes
if ( $cart->round_at_subtotal ) {
$cart->tax_total = $cart->tax->get_tax_total( $cart->taxes );
$cart->shipping_tax_total = $cart->tax->get_tax_total( $cart->shipping_taxes );
$cart->taxes = array_map( array( $cart->tax, 'round' ), $cart->taxes );
$cart->shipping_taxes = array_map( array( $cart->tax, 'round' ), $cart->shipping_taxes );
} else {
$cart->tax_total = array_sum( $cart->taxes );
$cart->shipping_tax_total = array_sum( $cart->shipping_taxes );
}
// VAT exemption done at this point - so all totals are correct before exemption
if ( WC()->customer->is_vat_exempt() ) {
$cart->remove_taxes();
}
}
add_filter( 'woocommerce_table_rate_query_rates_args', 'filter_shipping_2', 10 );
function filter_shipping_2( $arguments ) {
if ( WC()->cart->prices_include_tax )
$total = WC()->cart->cart_contents_total + array_sum( WC()->cart->taxes );
else
$total = WC()->cart->cart_contents_total;
$total = $total - ( WC()->cart->get_order_discount_total() + WC()->cart->get_cart_discount_total() );
$arguments['price'] = $total;
return $arguments;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment