PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, May 15, 2022

[FIXED] How to only change the value from 'apply_filters' via child theme functions?

 May 15, 2022     php, woocommerce, woocommerce-theming, wordpress, wordpress-hook     No comments   

Issue

I have the following code that outpouts the text "via nameOfshippingMethod" in the order summary in emails which I want to get rid of:

$shipping .= apply_filters( 'woocommerce_order_shipping_to_display_shipped_via', '&nbsp;<small class="shipped_via">' . sprintf( __( 'via %s', 'woocommerce' ), $this->get_shipping_method() ) . '</small>', $this );

From the perspective of taking the least invasive method, is it possible to somehow change the value of this apply_filters or would I have to replace the entire function in my child functions.php?

I would truly appreciate if anyone could help me!

For completeness the entire function is (located in "abstract-wc-order.php):

public function get_shipping_to_display( $tax_display = '' ) {
    $tax_display = $tax_display ? $tax_display : get_option( 'woocommerce_tax_display_cart' );

    if ( 0 < abs( (float) $this->get_shipping_total() ) ) {

        if ( 'excl' === $tax_display ) {

            // Show shipping excluding tax.
            $shipping = wc_price( $this->get_shipping_total(), array( 'currency' => $this->get_currency() ) );

            if ( (float) $this->get_shipping_tax() > 0 && $this->get_prices_include_tax() ) {
                $shipping .= apply_filters( 'woocommerce_order_shipping_to_display_tax_label', '&nbsp;<small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>', $this, $tax_display );
            }
        } else {

            // Show shipping including tax.
            $shipping = wc_price( $this->get_shipping_total() + $this->get_shipping_tax(), array( 'currency' => $this->get_currency() ) );

            if ( (float) $this->get_shipping_tax() > 0 && ! $this->get_prices_include_tax() ) {
                $shipping .= apply_filters( 'woocommerce_order_shipping_to_display_tax_label', '&nbsp;<small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>', $this, $tax_display );
            }
        }

        /* translators: %s: method */
        $shipping .= apply_filters( 'woocommerce_order_shipping_to_display_shipped_via', '&nbsp;<small class="shipped_via">' . sprintf( __( 'via %s', 'woocommerce' ), $this->get_shipping_method() ) . '</small>', $this );

    } elseif ( $this->get_shipping_method() ) {
        $shipping = $this->get_shipping_method();
    } else {
        $shipping = __( 'Free!', 'woocommerce' );
    }

    return apply_filters( 'woocommerce_order_shipping_to_display', $shipping, $this, $tax_display );
}

Solution

With the helpful tip from Howard E I was able to produce the following WooCommerce filter that works:

add_filter( 'woocommerce_order_shipping_to_display_shipped_via', 'woocommerce_order_shipping_to_display_shipped_via_remove', 10);
   
function woocommerce_order_shipping_to_display_shipped_via_remove( $no_via ) {
    $no_via = '&nbsp;<small class="shipped_via"></small>' ;
    return $no_via;
}

This was quite helpful in creating it for the first time: https://www.hardworkingnerd.com/how-to-use-add_filter-and-apply_filters-in-woocommerce/



Answered By - AlphaX
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,207,454

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing