Title: Why doesn&#8217;t this simple hook function work?
Last modified: January 31, 2022

---

# Why doesn’t this simple hook function work?

 *  Resolved [KarlWolfschtagg](https://wordpress.org/support/users/karlwolfschtagg/)
 * (@karlwolfschtagg)
 * [4 years, 4 months ago](https://wordpress.org/support/topic/why-doesnt-this-simple-hook-function-work/)
 * In my child function.php i have this:
 *     ```
       function adjust_tax() {
       	echo "start!";
       	global $woocommerce;
       	$items = $woocommerce->cart->get_cart();
   
           if ( is_admin() && ! defined( 'DOING_AJAX' ) )
               echo "(adm.)";
               return;
   
           if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
               echo "(did.)";
               return;
   
           if ( ! WC()->cart && WC()->cart->is_empty() )
               echo "(Cart empty).";
               return;
   
           $targeted_product_ids = array(221, 224); // Here define your specific products
           $subtotal = 0;
   
           // Loop through cart items (1st loop - get cart subtotal)
           foreach ( $items as $cart_item ) {
       		echo "yes";
               $subtotal += $cart_item['line_total'];
           }
   
           echo "Totalt: ";
           echo "Totalt: " . $subtotal;
   
       }
   
       function wc_cart_debug_all( $cart ) {
   
       global $woocommerce;
       $items = $woocommerce->cart->get_cart();
         echo "<pre>";
         print_r($items);
         echo "</pre>";
       } 
   
       add_action( 'woocommerce_before_cart', 'adjust_tax');
       add_action( 'woocommerce_before_cart', 'wc_cart_debug_all' );
       ```
   
 * After putting something in cart, the wc_cart_debug_all works fine, printing all.
 * For some reason, the adjust_tax function is acting weird.
    It prints “start!”
   on the page right after the debug lines (and before the actual products showing),
   but nothing more.
 * Obviously the function is not finished, and probably logic errors, but why doesn’t
   any of the “Totalt:” strings gets echoed?? Not even any “yes” is echoed.
    -  This topic was modified 4 years, 4 months ago by [KarlWolfschtagg](https://wordpress.org/support/users/karlwolfschtagg/).
    -  This topic was modified 4 years, 4 months ago by [KarlWolfschtagg](https://wordpress.org/support/users/karlwolfschtagg/).

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [kayart](https://wordpress.org/support/users/kayart/)
 * (@kayart)
 * [4 years, 4 months ago](https://wordpress.org/support/topic/why-doesnt-this-simple-hook-function-work/#post-15312734)
 * _woocommerce\_before\_cart_ action is just a template hook that allows you to
   display something before the cart.
 * Probably, _woocommerce\_before\_calculate\_totals_ is what you’re looking for.
 *  Thread Starter [KarlWolfschtagg](https://wordpress.org/support/users/karlwolfschtagg/)
 * (@karlwolfschtagg)
 * [4 years, 4 months ago](https://wordpress.org/support/topic/why-doesnt-this-simple-hook-function-work/#post-15312861)
 * [@kayart](https://wordpress.org/support/users/kayart/) Thx for info.
 * But even so, why isn’t any of the other echo:s shown in that function?
 * And if switching to woocommerce_before_calculate_totals instead, can I echo random
   string there and if so, where will it show ? on cart page?
 *  [Mirko P.](https://wordpress.org/support/users/rainfallnixfig/)
 * (@rainfallnixfig)
 * [4 years, 4 months ago](https://wordpress.org/support/topic/why-doesnt-this-simple-hook-function-work/#post-15312989)
 * Hi [@karlwolfschtagg](https://wordpress.org/support/users/karlwolfschtagg/),
 * These forums are more focused towards general support with the core functionality
   of WooCommerce and I would recommend posting your questions on more development-
   oriented channels like:
    - WooCommerce Slack Community ([https://woocommerce.com/community-slack/](https://woocommerce.com/community-slack/))
    - Advanced WooCommerce group on Facebook ([https://www.facebook.com/groups/advanced.woocommerce/](https://www.facebook.com/groups/advanced.woocommerce/))
 * That said, I’m going to leave this thread open for a bit to see if anyone is 
   able to chime in to help you out.
 * If you require further help with your custom-coded solution and you don’t get
   many inputs on the above channels, we highly recommend contacting one of the 
   services on our customizations page: [https://woocommerce.com/customizations/](https://woocommerce.com/customizations/).
 * Cheers.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Why doesn’t this simple hook function work?’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

 * 3 replies
 * 3 participants
 * Last reply from: [Mirko P.](https://wordpress.org/support/users/rainfallnixfig/)
 * Last activity: [4 years, 4 months ago](https://wordpress.org/support/topic/why-doesnt-this-simple-hook-function-work/#post-15312989)
 * Status: resolved