Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save torbentschechne/a7a7b16c0df57ec4091968b425a9c4e5 to your computer and use it in GitHub Desktop.
Save torbentschechne/a7a7b16c0df57ec4091968b425a9c4e5 to your computer and use it in GitHub Desktop.
WooCommerce - free shipping notice
function free_shipping_cart_notice_zones() {
global $woocommerce;
// Get Free Shipping Methods for Rest of the World Zone & populate array $min_amounts
$default_zone = new WC_Shipping_Zone(0);
$default_methods = $default_zone->get_shipping_methods();
foreach( $default_methods as $key => $value ) {
if ( $value->id === "free_shipping" ) {
if ( $value->min_amount > 0 ) $min_amounts[] = $value->min_amount;
}
}
// Get Free Shipping Methods for all other ZONES & populate array $min_amounts
$delivery_zones = WC_Shipping_Zones::get_zones();
foreach ( $delivery_zones as $key => $delivery_zone ) {
foreach ( $delivery_zone['shipping_methods'] as $key => $value ) {
if ( $value->id === "free_shipping" ) {
if ( $value->min_amount > 0 ) $min_amounts[] = $value->min_amount;
}
}
}
// Find lowest min_amount
if ( is_array($min_amounts) ) {
$min_amount = min($min_amounts);
// Get Cart Subtotal inc. Tax excl. Shipping
$current = WC()->cart->subtotal;
// If Subtotal < Min Amount Echo Notice
// and add "Continue Shopping" button
if ( $current < $min_amount ) {
$added_text = esc_html__('Noch ', 'woocommerce' ) . wc_price( $min_amount - $current ) . esc_html__(' und wir liefern versandkostenfrei!', 'woocommerce' );
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) );
$notice = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), esc_html__( 'Weiter einkaufen', 'woocommerce' ), $added_text );
wc_print_notice( $notice, 'notice' );
}
}
}
add_action( 'woocommerce_before_cart', 'free_shipping_cart_notice_zones' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment