E
E
Egor2020-08-31 20:54:47
WordPress
Egor, 2020-08-31 20:54:47

An incomprehensible wordpress / woocommerce error pops up, how to fix it?

Here is the error: Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'wdm_empty_cart' not found or invalid function name in /home/skazka/domains/skazkamini.ru/public_html/wp-includes/class-wp-hook .php on line 289

289 line in this file:

$value = call_user_func_array( $the_['function'], array_slice( $args, 0, (int) $the_['accepted_args'] ) );


The error appeared after adding this code (adding multiple items to the cart by default)
Here is the code itself:

add_action( 'template_redirect', 'add_product_to_cart' );
  function add_product_to_cart($quantity) {
    if ( ! is_admin() ) {
      $articles = array(302, 306, 328, 331);
      $found = false;

        // check if product already in cart
      if ( sizeof( WC()->cart->get_cart() ) > 0 ) {

        foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
          $_product = $values['data'];

          if (($key = array_search($_product->id, $articles)) !== false)
            unset($articles[$key]);
        }

            // if product not found, add it
        if ( count($articles) > 0 ) {

          foreach ($articles as $article) {
            WC()->cart->remove_cart_item($article);
          }
        }
      } else {

            // if no products in cart, add it
        foreach ($articles as $article) {
          WC()->cart->add_to_cart($article);
          
        }
      }
    }
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pychev Anatoly, 2020-08-31
@e_mooshka

Look in your code for the line that hangs this handler 'wdm_empty_cart'
. The error says that the handler cannot find the function, or the function is declared incorrectly. For example, when a function is a member of a class, and when it was bound to the hook (add_action, add_filter), they forgot to specify $this or self::
In the code that you attached, references to this function are not visible, most likely it is indirectly connected.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question