-2

I have a function like:

function custom_field( $something ) {
   // code goes here
}

In a special condition i'd like to unset it in some way. I have tride something like:

if (function_exists("custom_field")) {
    unset(custom_field());
}

The use of unset won't work as I get "Fatal error: Can't use function return value in write context".

How can I unset a function? I have checked at runkit_function_remove (not sure if that could solve my problem) but that needs PECL to be installed which my host won't install for me.

Any suggestions?

EDIT:

I'll update with more code (working with wordpress and woocommerce):

I display a checkbox like this:

add_action('woocommerce_after_order_notes', 'my_custom_checkout_field1');

function my_custom_checkout_field1( $checkout22 ) {


    echo '<div id="my-new-field"><h3>'.__('Homedelivery:').'</h3>';

    woocommerce_form_field( 'Homedelivery', array(
        'type'          => 'checkbox',
        'class'         => array('input-checkbox'),
        'label'         => __('Message here.'),
        'required'  => false,
        ), $checkout22->get_value( 'my_checkbox' ));

    echo '</div>';
}


add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process1');

function my_custom_checkout_field_process1() {
    global $woocommerce;

    // Check if set, if its not set add an error.
    if (!$_POST['my_checkbox']) {
         //$woocommerce->add_error( __('Please agree to my checkbox.') );
         $_POST['my_checkbox'] = "Customer wants to be home when package is delivered.";
         } else {
             $_POST['my_checkbox'] = "Customers sais its ok the leave the package outside the door.";
         }

}


add_action('woocommerce_checkout_update_order_meta1', 'my_custom_checkout_field_update_order_meta');

function my_custom_checkout_field_update_order_meta1( $order_id ) {
    if ($_POST['my_checkbox']) update_post_meta( $order_id, 'My Checkbox', esc_attr($_POST['my_checkbox']));
}

I would like this checkbox only to be visible when 'local_deliver' is chosen:

function payment_gateway_disable_country($available_gateways) {

    global $woocommerce;

    $packages = $woocommerce->shipping->get_packages();

    foreach ( $packages as $i => $package ) {
        $chosen_method = isset( $woocommerce->session->chosen_shipping_methods[ $i ] ) ?
        $woocommerce->session->chosen_shipping_methods[ $i ] :  '';

        // hämta i kyrkhult
        if ('local_pickup' == $chosen_method) {  
            global $woocommerce;
            foreach ($woocommerce->cart->cart_contents as $key => $values ) {

        if($values['product_id'] && get_field('farskvara',$values['product_id'])){

        unset($available_gateways['cod']);

        break;
        } else {

            unset($available_gateways['cod']);
        }
        }

        } elseif ('flat_rate' == $chosen_method) {  

    global $woocommerce;
    foreach ($woocommerce->cart->cart_contents as $key => $values ) {

        if($values['product_id'] && get_field('farskvara',$values['product_id'])){

        unset($available_gateways['cheque']);


        break;
        } else {
            unset($available_gateways['cheque']);

        }
        }

    } elseif ('local_delivery' == $chosen_method) {

    global $woocommerce;
    foreach ($woocommerce->cart->cart_contents as $key => $values ) {

        if($values['product_id'] && get_field('farskvara',$values['product_id'])){

        unset($available_gateways['cheque']);
        break;
        } else {
            unset($available_gateways['cheque']);
        }
        }


    }

    }

return $available_gateways;

}

add_filter(
    'woocommerce_available_payment_gateways',
    'payment_gateway_disable_country'
);

How could this be fixed?

JLR
  • 723
  • 1
  • 8
  • 17
  • 3
    You can't. Just don't include it to begin with. – AbraCadaver Sep 02 '14 at 21:31
  • 1
    Why do you need something like this? If you are experiencing name collisions, consider using a [namespace](http://php.net/manual/en/language.namespaces.php). – George Cummins Sep 02 '14 at 21:32
  • 1
    Whatever problem you are trying to solve, it can be solved another way. – Jon Sep 02 '14 at 21:32
  • If that would be possible (and I don't think it is), you would do it by either passing the function name without parentheses, or even pass it by name as a string. In your current syntax, the function is executed, and you unset its result. This could never work. – GolezTrol Sep 02 '14 at 21:32
  • 1
    Your use case isn't clear to me, but maybe it's [`related to this`](http://stackoverflow.com/questions/8691884/how-to-unset-a-function-definition-just-like-we-unset-a-variable). – GolezTrol Sep 02 '14 at 21:34
  • @GolezTrol: [The constraints of the OPs seem to match AFAICT](http://stackoverflow.com/questions/8691884/how-to-unset-a-function-definition-just-like-we-unset-a-variable#comment10810670_8691884). Neither is restricted to anonymous functions; don't confuse the question with the answers. – Lightness Races in Orbit Sep 02 '14 at 21:40
  • Well, I'm working on a webshop. In the checkout I have a checkbox that is visible. But i only want that checkbox to be visible when I select a certain freight alternative. Tha'ts why I would like to find a way to "unset" it in some way. I have checked that thread you link but I'm unable to set a $somethong before the function. – JLR Sep 02 '14 at 21:43
  • 1
    @JohanNdiyoLinnarsson An HTML checkbox is something completely different than a PHP function. And now I don't know what you mean at all. – GolezTrol Sep 02 '14 at 21:44
  • I updated the code with what I'm working on. Hope this can cleaqr some things out. – JLR Sep 02 '14 at 21:49

1 Answers1

1

Trying to unset functions is very bizarre, and PHP won't let you do it.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055