【问题标题】:if function_exists unset it? [duplicate]如果 function_exists 取消设置它? [复制]
【发布时间】:2014-09-02 21:30:08
【问题描述】:

我有一个类似的功能:

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

在特殊情况下,我想以某种方式取消设置。我有类似的东西:

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

当我收到“致命错误:无法在写入上下文中使用函数返回值”时,无法使用 unset。

如何取消设置功能?我已经在runkit_function_remove 进行了检查(不确定这是否可以解决我的问题),但这需要安装我的主机不会为我安装的 PECL。

有什么建议吗?

编辑:

我将更新更多代码(使用 wordpress 和 woocommerce):

我显示一个这样的复选框:

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']));
}

我希望此复选框仅在选择“local_deliver”时可见:

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'
);

如何解决这个问题?

【问题讨论】:

  • 你不能。只是不要一开始就包括它。
  • 为什么需要这样的东西?如果您遇到名称冲突,请考虑使用namespace
  • 无论您想解决什么问题,都可以通过其他方式解决。
  • 你的用例我不清楚,但可能是related to this
  • @JohanNdiyoLinnarsson HTML 复选框与 PHP 函数完全不同。现在我完全不明白你的意思。

标签: php


【解决方案1】:

试图取消设置函数是很奇怪的,PHP 不会让你这样做的。

【讨论】:

    猜你喜欢
    • 2012-09-19
    • 2017-02-07
    • 2018-07-23
    • 2017-06-20
    • 2014-03-25
    • 2012-09-29
    • 2018-07-04
    • 2019-10-21
    • 1970-01-01
    相关资源
    最近更新 更多