Adding code to a php function with a separate php function

I have a php function called wc_print_notices that is in the core files of woocommerce, and I don’t wish to edit the core files if I can avoid it.

So I would like to write something in my functions.php file that will add more functionality to this function.

Read More

I’ve tried using filters, however, those only work with woocommerce functions that act as hooks, and this is a function that is called through a hook, so do_action() and add_filter do not work with wc_print_notices

I want to add related products to this function, and have tried something like this

function popup_products( $popup ){
    $popup = woocommerce_output_related_products();
  return $popup;
}

The function itself works perfectly, however, I want to add this to the function wc_print_notices

the filters I tried were:

add_filter( 'wc_print_notices', 'popup_products', 1, 1);
add_filter( 'wc_print_notices', 'popup_products', 99, 99);
add_filter( 'wc_print_notices', 'popup_products', 1);
add_filter( 'wc_print_notices', 'popup_products', 99);

Related posts

1 comment

  1. Since wc_print_notices is being called by add_action( 'woocommerce_before_shop_loop', 'wc_print_notices', 10 ); i think you need to remove that action with remove_action then add_action( 'woocommerce_before_shop_loop', 'my_custom_wc_print_notices', 10 );

    https://docs.woothemes.com/wc-apidocs/source-function-wc_print_notices.html#106-129

    Maybe adding additional notices.

    https://docs.woothemes.com/wc-apidocs/source-function-wc_print_notices.html#75-91

    I think in your case if all you want to do is add additional information all you have todo is add it in one of the Woocommerce Loop’s.

    add_action('woocommerce_before_shop_loop','popup_products');

    With OP Clarification

    function custom_wc_print_notices() {
    if ( ! did_action( 'woocommerce_init' ) ) {
             _doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
             return;
         }
    
         $all_notices  = WC()->session->get( 'wc_notices', array() );
         $notice_types = apply_filters( 'woocommerce_notice_types', array( 'error', 'success', 'notice' ) );
    
         foreach ( $notice_types as $notice_type ) {
             if ( wc_notice_count( $notice_type ) > 0 ) {
                 // Call your function here and make sure it only output once
                 woocommerce_output_related_products();
                 /********************************************/
                 wc_get_template( "notices/{$notice_type}.php", array(
                     'messages' => $all_notices[$notice_type]
                 ) );
             }
         }
    
         wc_clear_notices();
    }
    

    I think the best bet is like i suggest before to remove the default add_action place it back with your modified version of wc_print_notices.

Comments are closed.