skip to Main Content

Using WooCoomerce along with WooCommerce Bookings plugin. In their API Reference, there’s a listed filter for modifying booking cost: woocommerce_bookings_calculated_booking_cost. To cut it short, here’s how it’s applied in the code:

return apply_filters( 'woocommerce_bookings_calculated_booking_cost', $booking_cost, $product, $data );

Now, I’ve added following code, to try and change the price:

function foobar_price_changer( $booking_cost, $product, $data ) {
   return $booking_cost;
}

add_filter( 'woocommerce_bookings_calculated_booking_cost', 'foobar_price_changer' );

Now, when I use that code, it throws an error in my logs:

PHP Fatal error:  Uncaught ArgumentCountError: Too few arguments to function sbnb_modify_wc_bookings_price(), 1 passed in /mywppath/wp-includes/class-wp-hook.php on line 290 and exactly 3 expected in /mywppath/wp-content/themes/enfold-child/functions.php:155

As far as I read it, 3 arguments are passed to the add_filter callbacks, but in my case it only passes one. What could be the issue here?

2

Answers


  1. Give it a try this way

    function foobar_price_changer( $booking_cost, $product, $data ) {
       return $booking_cost;
    }
    add_filter( 'woocommerce_bookings_calculated_booking_cost', 'foobar_price_changer', 10, 3 ); // Where $priority is 10, $args is 3.
    
    Login or Signup to reply.
  2. If you don’t specify the $accepted_args or the fourth parameter when calling add_filter function, it passes by default only one parameter to your callback function. So whenever there are more than one argument to pass to the callback function, you have to specify the number of the expected arguments.
    From wp-includes/plugin.php:

    * @global array $wp_filter A multidimensional array of all hooks and the callbacks hooked to them.
    *
    * @param string   $tag             The name of the filter to hook the $function_to_add callback to.
    * @param callable $function_to_add The callback to be run when the filter is applied.
    * @param int      $priority        Optional. Used to specify the order in which the functions
    *                                  associated with a particular action are executed.
    *                                  Lower numbers correspond with earlier execution,
    *                                  and functions with the same priority are executed
    *                                  in the order in which they were added to the action. Default 10.
    * @param int      $accepted_args   Optional. The number of arguments the function accepts. Default 1.
    * @return true
    */
    function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
       global $wp_filter;
       if ( ! isset( $wp_filter[ $tag ] ) ) {
           $wp_filter[ $tag ] = new WP_Hook();
       }
       $wp_filter[ $tag ]->add_filter( $tag, $function_to_add, $priority, $accepted_args );
       return true;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search