• I have installed woocommerce. I want to override the woocommerce function.

    1-i have my child theme.
    2- I added functions.php file
    3- i added my function like below

    [ Moderator note: code fixed. Please wrap code in the backtick character or use the code button. ]

    add_filter('woocommerce_cart_totals_order_total_html','test_func');
    function test_func() {
        $value = '<strong>Test' . WC()->cart->get_total() . '</strong> ';
        return $value;
    }

    4- I have copied woocommerce theme in my child theme
    5- I call my new function like below

    <tr class="order-total">
           <th><?php _e( 'Total', 'woocommerce' ); ?></th>
    	    <td data-title="<?php esc_attr_e( 'Total', 'woocommerce' ); ?>">
                    <?php test_func(); ?>
                </td>
       </tr>

    6- But i gave my error

    Fatal error: Call to undefined function test_func()

Viewing 8 replies - 1 through 8 (of 8 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Does the function declaration have to be before the filter?

    function test_func() {
        $value = '<strong>Test' . WC()->cart->get_total() . '</strong> ';
        return $value;
    }
    add_filter('woocommerce_cart_totals_order_total_html','test_func');

    Thread Starter shahidibex

    (@shahidibex)

    @andrew still not working

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Which theme are you using?

    Thread Starter shahidibex

    (@shahidibex)

    I am using storefront theme

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    The storefront theme has good support, I would recommend posting this question on their forum: https://www.ads-software.com/support/theme/storefront#postform

    Hi shahidibex,

    Please try the following code and let me know if it is working or not:

    <tr class="order-total">
           <th><?php _e( 'Total', 'woocommerce' ); ?></th>
    	    <td data-title="<?php esc_attr_e( 'Total', 'woocommerce' ); ?>">
                    <?php echo $value = apply_filter('woocommerce_cart_totals_order_total_html'); ?>
                </td>
       </tr>

    Regards,
    Rahul

    Thread Starter shahidibex

    (@shahidibex)

    Hi Rahul,
    Thanks for the answer. I did your trick and got below error.

    Fatal error: Call to undefined function apply_filter()

    Sorry

    My Mistake. Please use apply_filters() instead of apply_filter(). So the code will be as below:

    <tr class="order-total">
           <th><?php _e( 'Total', 'woocommerce' ); ?></th>
    	    <td data-title="<?php esc_attr_e( 'Total', 'woocommerce' ); ?>">
                    <?php echo $value = apply_filters('woocommerce_cart_totals_order_total_html'); ?>
                </td>
       </tr>

    Let me know if this works!

    Regards,
    Rahul

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘function is not working.’ is closed to new replies.