Cannot declare function twice

时间:2021-02-28 作者:Carrie

我在代码片段插件中使用了两个挂钩:

1.

    add_filter( \'woocommerce_package_rates\', \'businessbloomer_hide_free_shipping_for_shipping_class\', 10, 2 );
       
    function businessbloomer_hide_free_shipping_for_shipping_class( $rates, $package ) {
      
    
     $shipping_class_target = \'1173\'; // This is Deluxe Tables shipping class ID 
       $in_cart = false;
       foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
          if ( $values[ \'data\' ]->get_shipping_class_id() == $shipping_class_target ) {
             $in_cart = true;
             break;
          } 
       }
       if ( $in_cart ) {
          unset( $rates[\'local_pickup:45\'] ); // This is Local Pick up shipping method with ID 
       }
       return $rates;
    }
add_filter( \'woocommerce_package_rates\', \'businessbloomer_hide_free_shipping_for_shipping_class\', 10, 2 ); function businessbloomer_hide_free_shipping_for_shipping_class( $rates, $package ) { $shipping_class_target = \'1750\'; // This is Stag & Doe shipping class ID $in_cart = false; foreach ( WC()->cart->get_cart_contents() as $key => $values ) { if ( $values[ \'data\' ]->get_shipping_class_id() == $shipping_class_target ) { $in_cart = true; break; } } if ( $in_cart ) { unset( $rates[\'WB_Custom_WooCommerce_Shipping_Method52\'] ); // This is Delivery shipping method with ID } return $rates; } 如果一次只激活一个,它就会工作!但是它说我不能两次声明这个函数,那么如何将这两者结合起来呢?我尝试了这么多的变化,但都没有效果。请帮忙!

1 个回复
SO网友:Rup

我同意Pat的意见:最简单的方法是在两个代码片段中的一个片段中重命名函数,在函数定义和add\\u过滤器行中更改名称。但是,如果您确实想将这两者结合到一个片段中,可以执行以下操作:

function businessbloomer_hide_free_shipping_for_shipping_class( $rates, $package ) {
    $deluxe_tables_shipping_class_target = \'1173\';
    $stag_and_doe_shipping_class_target = \'1750\';

    $hide_local_pickup = false;
    $hide_delivery_shipping = false;

    foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
        $shipping_class_id = $values[ \'data\' ]->get_shipping_class_id();
        if ( $deluxe_tables_shipping_class_target == $shipping_class_id ) {
            $hide_local_pickup = true;
        } elseif ( $stag_and_doe_shipping_class_target == $shipping_class_id ) {
            $hide_delivery_shipping = true;
        }
    }
    if ( $hide_local_pickup ) {
        // This is Local Pick up shipping method with ID
        unset( $rates[\'local_pickup:45\'] );
    }
    if ( $hide_delivery_shipping ) {
        // This is Delivery shipping method with ID
        unset( $rates[\'WB_Custom_WooCommerce_Shipping_Method52\'] );
    }
    return $rates;
}
add_filter( \'woocommerce_package_rates\',
            \'businessbloomer_hide_free_shipping_for_shipping_class\', 10, 2 );
(您还可以将代码更改为在两个== $shipping_class_id 行,但我不知道WooCommerce是否将该ID存储为字符串或数字,因此==更安全。)

相关推荐

Functions.php上未定义$_GET和&_REQUEST索引

我最近尝试学习为我的自定义主题创建一个主题选项页面,这是按照stackoverflow和其他资源的教程进行的。但脚本显示错误if ($_GET[\'page\'] == basename(__FILE__)) { if (\'save\' == $_REQUEST[\'formaction\']) { foreach ($options as $value) { if( isset( $_REQUEST[ $value[\'id\']