From 58eaa704249c1f68fbc0f61d394730264f9f40ca Mon Sep 17 00:00:00 2001 From: Rasmus Date: Fri, 21 Oct 2016 12:38:35 +0200 Subject: [PATCH] Only print js for free shipping fields in admin Since WC_Shipping_Method->get_option() runs $this->get_instance_form_fields it prints out the JS even when getting the option outsite admin. --- .../class-wc-shipping-free-shipping.php | 46 ++++++++++--------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/includes/shipping/free-shipping/class-wc-shipping-free-shipping.php b/includes/shipping/free-shipping/class-wc-shipping-free-shipping.php index a595d908fef..bc8741f0e1d 100644 --- a/includes/shipping/free-shipping/class-wc-shipping-free-shipping.php +++ b/includes/shipping/free-shipping/class-wc-shipping-free-shipping.php @@ -107,31 +107,33 @@ class WC_Shipping_Free_Shipping extends WC_Shipping_Method { * @return array */ public function get_instance_form_fields() { - wc_enqueue_js( " - jQuery( function( $ ) { - function wcFreeShippingShowHideMinAmountField( el ) { - var form = $( el ).closest( 'form' ); - var minAmountField = $( '#woocommerce_free_shipping_min_amount', form ).closest( 'tr' ); - if ( 'coupon' === $( el ).val() || '' === $( el ).val() ) { - minAmountField.hide(); - } else { - minAmountField.show(); + if ( is_admin() ) { + wc_enqueue_js( " + jQuery( function( $ ) { + function wcFreeShippingShowHideMinAmountField( el ) { + var form = $( el ).closest( 'form' ); + var minAmountField = $( '#woocommerce_free_shipping_min_amount', form ).closest( 'tr' ); + if ( 'coupon' === $( el ).val() || '' === $( el ).val() ) { + minAmountField.hide(); + } else { + minAmountField.show(); + } } - } - $( document.body ).on( 'change', '#woocommerce_free_shipping_requires', function() { - wcFreeShippingShowHideMinAmountField( this ); + $( document.body ).on( 'change', '#woocommerce_free_shipping_requires', function() { + wcFreeShippingShowHideMinAmountField( this ); + }); + + // Change while load. + $( '#woocommerce_free_shipping_requires' ).change(); + $( document.body ).on( 'wc_backbone_modal_loaded', function( evt, target ) { + if ( 'wc-modal-shipping-method-settings' === target ) { + wcFreeShippingShowHideMinAmountField( $( '#wc-backbone-modal-dialog #woocommerce_free_shipping_requires', evt.currentTarget ) ); + } + } ); }); - - // Change while load. - $( '#woocommerce_free_shipping_requires' ).change(); - $( document.body ).on( 'wc_backbone_modal_loaded', function( evt, target ) { - if ( 'wc-modal-shipping-method-settings' === target ) { - wcFreeShippingShowHideMinAmountField( $( '#wc-backbone-modal-dialog #woocommerce_free_shipping_requires', evt.currentTarget ) ); - } - } ); - }); - " ); + " ); + } return parent::get_instance_form_fields(); }