• Resolved pintillo86

    (@pintillo86)


    trying to add a new snippet, but it keeps giving the following message “An error occurred when saving the snippet.”
    How can I solve this issue?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    Hi @pintillo86,

    This message is usually caused by some buggy code in your snippet causing a PHP error.

    The solution is to fix the code before saving the snippet.

    If you post the code you’re trying to use here, I’m happy to help see if I can fix the problem.

    Thread Starter pintillo86

    (@pintillo86)

    No matter what snippet I try to save, it gives me the error. In this case I’m trying to save the following one:

    <?php // only copy if needed
    
    /**
     * Removes coupon form, order notes, and several billing fields if the checkout doesn't require payment.
     *
     * REQUIRES PHP 5.3+
     *
     * Tutorial: https://skyver.ge/c
     */
    function sv_free_checkout_fields() {
    
    	// first, bail if WC isn't active since we're hooked into a general WP hook
    	if ( ! function_exists( 'WC' ) ) {
    		return;	
    	}
    
    	// bail if the cart needs payment, we don't want to do anything
    	if ( WC()->cart && WC()->cart->needs_payment() ) {
    		return;
    	}
    
    	// now continue only if we're at checkout
    	// is_checkout() was broken as of WC 3.2 in ajax context, double-check for is_ajax
    	// I would check WOOCOMMERCE_CHECKOUT but testing shows it's not set reliably
    	if ( function_exists( 'is_checkout' ) && ( is_checkout() || is_ajax() ) ) {
    
    		// remove coupon forms since why would you want a coupon for a free cart??
    		remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );
    
    		// Remove the "Additional Info" order notes
    		add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
    
    		// Unset the fields we don't want in a free checkout
    		add_filter( 'woocommerce_checkout_fields', function( $fields ) {
    
    			// add or remove billing fields you do not want
    			// fields: https://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-2
    			$billing_keys = array(
    				'billing_company',
    				'billing_phone',
    				'billing_address_1',
    				'billing_address_2',
    				'billing_city',
    				'billing_postcode',
    				'billing_country',
    				'billing_state',
    			);
    
    			// unset each of those unwanted fields
    			foreach( $billing_keys as $key ) {
    				unset( $fields['billing'][ $key ] );
    			}
    
    			return $fields;
    		} );
    	}
    
    }
    add_action( 'wp', 'sv_free_checkout_fields' );
    Thread Starter pintillo86

    (@pintillo86)

    I even try copying and pasting one of the example snippets included with the pugin and still getting the error.
    But I finally managed to solve the issue, I deactivated and uninstall the plugin, and then reinstall and now it works!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘An error occurred when saving the snippet.’ is closed to new replies.