WordPress/Woocommerce ‘add_filter’ to ALL Variation Prices

I’m running a third-party inventory updater that includes MY prices. So I want to ‘add_filter’ to all prices to include the markup. Simple Products has been a breeze.

function change_single_price($price, $product){ return $price  * 1.5;}
add_filter('get_price','change_single_price', 10, 2);
add_filter('woocommerce_get_price','change_single_price', 10, 2);
add_filter('woocommerce_get_regular_price','change_single_price', 10, 2);
add_filter('woocommerce_get_sale_price','change_single_price', 10, 2);

I was up until late last night trying to get ALL variation prices to do the same thing, but all the tips I can find only show how to edit the HTML around variable prices. Here’s one version of the code I’ve been trying to edit/make work.

function custom_variable_price( $price, $product ) {
            // Main Price
            $prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
            $price = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

            // Sale Price
            $prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
            sort( $prices );
            $saleprice = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

            if ( $price !== $saleprice ) {
                $price = '' . $saleprice . ' ' . $price . '';
            }

            return $price;

        }

        add_filter( 'woocommerce_variable_sale_price', 'wc_ninja_custom_variable_price', 10, 2 );
        add_filter( 'woocommerce_variable_price', 'wc_ninja_custom_variable_price', 10, 2 );

I’ve tried 5-6 variations of this code & editing it myself. I’ve only caused “0” prices and 502 loops.

Read more here: WordPress/Woocommerce ‘add_filter’ to ALL Variation Prices

Leave a Reply

Your email address will not be published. Required fields are marked *