Using a filter with multiple parameters and $this

Question

I have seen various questions about this but I am unsure how to apply those methods in this particular case.

I am trying to add a filter to a plugin which returns output with the apply_filters hook:

public function get_shipment_tracking_column( $order_id ) {
        ob_start();

        $tracking_items = $this->get_tracking_items( $order_id );

        if ( count( $tracking_items ) > 0 ) {
            echo '<ul>';

            foreach ( $tracking_items as $tracking_item ) {
                $formatted = $this->get_formatted_tracking_item( $order_id, $tracking_item );
                printf(
                    '<li><a href="%s" target="_blank">%s</a></li>',
                    esc_url( $formatted['formatted_tracking_link'] ),
                    esc_html( $tracking_item['tracking_number'] )
                );
            }
            echo '</ul>';
        } else {
            echo '–';
        }

        return apply_filters( 'woocommerce_shipment_tracking_get_shipment_tracking_column', ob_get_clean(), $order_id, $tracking_items );
    }

In my particular case I need to change the contents within the <a> tag, so I tried the following:

add_filter('woocommerce_shipment_tracking_get_shipment_tracking_column', 'woocommerce_shipment_tracking_get_shipment_tracking_column_custom', 10, 2);
function woocommerce_shipment_tracking_get_shipment_tracking_column_custom($order_id, $tracking_items){
    $tracking_items = $this->get_tracking_items( $order_id );

    if ( count( $tracking_items ) > 0 ) {
        echo '<ul>';

        foreach ( $tracking_items as $tracking_item ) {
            $formatted = $this->get_formatted_tracking_item( $order_id, $tracking_item );
            printf(
                '<li><a href="%s" target="_blank">%s</a></li>',
                esc_url( $formatted['formatted_tracking_link'] ),
                'MY NEW TEXT'
            );
        }
        echo '</ul>';
    } else {
        echo '–';
    }
}

Note the “My new text”. While I can get content to output if I just remove everything within the function and just do echo "something", I really need to output information specific to an order. I am just unsure how to do this if I can’t use $this.

So my question – how can I correctly use add_filters in this case, and is it possible to use $this in my function to get the order data that is originally being sent to this filter

0
, , wickywills 2 years 2019-10-21T08:36:02-05:00 0 Answers 88 views 0

Leave an answer

Browse
Browse