• Resolved KarlWolfschtagg

    (@karlwolfschtagg)


    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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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

    (@karlwolfschtagg)

    @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.

    (@rainfallnixfig)

    Hi @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:

    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/.

    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.