An error occurred while trying to activate
-
Hi, I have cloned a code and changed a code number but it won’t let me activate it. Message: “An error occurred while trying to activate.”. Help, please.
The page I need help with: [log in to see the link]
-
Hi @rodrigor17, can you please the post the code you’re using here? It’s likely this is caused by a duplicate function name or similar, which can be pretty easily fixed up.
Hi, this is the code:
/* This piece of code will hide fields for the chosen method. .hide_pickup { display: none !important; } */ // Hide Local Pickup shipping method add_filter( 'woocommerce_checkout_fields', 'hide_local_pickup_method' ); function hide_local_pickup_method( $fields_pickup ) { // change below for the method $shipping_method_pickup ='local_pickup:23'; // change below for the list of fields. Add (or delete) the field name you want (or don’t want) to use $hide_fields_pickup = array( 'billing_company', 'billing_country', 'billing_postcode', 'billing_address_1', 'billing_address_2' , 'billing_city', 'billing_state', 'billing_wooccm11', 'billing_provin'); $chosen_methods_pickup = WC()->session->get( 'chosen_shipping_methods' ); $chosen_shipping_pickup = $chosen_methods_pickup[0]; foreach($hide_fields_pickup as $field_pickup ) { if ($chosen_shipping_pickup == $shipping_method_pickup) { $fields_pickup['billing'][$field_pickup]['required'] = false; $fields_pickup['billing'][$field_pickup]['class'][] = 'hide_pickup'; } $fields_pickup['billing'][$field_pickup]['class'][] = 'billing-dynamic_pickup'; } return $fields_pickup; } // Local Pickup - hide fields add_action( 'wp_head', 'local_pickup_fields', 999 ); function local_pickup_fields() { if (is_checkout()) : ?> <style> .hide_pickup {display: none!important;} </style> <script> jQuery( function( $ ) { if ( typeof woocommerce_params === 'undefined' ) { return false; } $(document).on( 'change', '#shipping_method input[type="radio"]', function() { // change local_pickup:4 accordingly $('.billing-dynamic_pickup').toggleClass('hide_pickup', this.value == 'local_pickup:23'); }); }); </script> <?php endif; }Thanks.
The problem here is definitely the use of named functions. They will work correctly if you only have one snippet activated, but as soon as you clone and activate then run into conflict.
An easy solution is to convert to using anonymous functions, like this:
// Hide Local Pickup shipping method add_filter( 'woocommerce_checkout_fields', function ( $fields_pickup ) { // change below for the method $shipping_method_pickup = 'local_pickup:23'; // change below for the list of fields. Add (or delete) the field name you want (or don’t want) to use $hide_fields_pickup = array( 'billing_company', 'billing_country', 'billing_postcode', 'billing_address_1', 'billing_address_2', 'billing_city', 'billing_state', 'billing_wooccm11', 'billing_provin' ); $chosen_methods_pickup = WC()->session->get( 'chosen_shipping_methods' ); $chosen_shipping_pickup = $chosen_methods_pickup[0]; foreach ( $hide_fields_pickup as $field_pickup ) { if ( $chosen_shipping_pickup == $shipping_method_pickup ) { $fields_pickup['billing'][ $field_pickup ]['required'] = false; $fields_pickup['billing'][ $field_pickup ]['class'][] = 'hide_pickup'; } $fields_pickup['billing'][ $field_pickup ]['class'][] = 'billing-dynamic_pickup'; } return $fields_pickup; } ); // Local Pickup - hide fields add_action( 'wp_head', function () { if ( ! is_checkout() ) { return; } ?> <style> .hide_pickup { display: none !important; } </style> <script> jQuery(function ($) { if (typeof woocommerce_params === 'undefined') { return false } $(document).on('change', '#shipping_method input[type="radio"]', function () { // change local_pickup:4 accordingly $('.billing-dynamic_pickup').toggleClass('hide_pickup', this.value == 'local_pickup:23') }) }) </script> <?php }, 999 );Hi, thanks for response. But, I’m still have the problem. How can I do for fix it please?
The topic ‘An error occurred while trying to activate’ is closed to new replies.