• Resolved Phil Emery

    (@noise)


    I’ve been trying to add some code to display a variation’s price in the WooCommerce variation popup menu.

    The suggested code (which is old) uses 'woocommerce_price( $_product->get_price() )". This outputs the price but also displays all the HTML code as well. Looking at the WC documentation this call isn’t mentioned anymore. Is this no longer supported?

    Here is the code in full, in case anyone is interested

    add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
    
    function display_price_in_variation_option_name( $term ) {
        global $wpdb, $product;
    
        $result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
    
        $term_slug = ( !empty( $result ) ) ? $result[0] : $term;
    
        $query = "SELECT postmeta.post_id AS product_id
                    FROM {$wpdb->prefix}postmeta AS postmeta
                        LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
                    WHERE postmeta.meta_key LIKE 'attribute_%'
                        AND postmeta.meta_value = '$term_slug'
                        AND products.post_parent = $product->id";
    
        $variation_id = $wpdb->get_col( $query );
    
        $parent = wp_get_post_parent_id( $variation_id[0] );
    
        if ( $parent > 0 ) {
            $_product = new WC_Product_Variation( $variation_id[0] );
            return $term . ' (' . woocommerce_price( $_product->get_price() ) . ')';
        }
        return $term;
    
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support con

    (@conschneider)

    Engineer

    Hi there,

    Looking at the WC documentation this call isn’t mentioned anymore. Is this no longer supported?

    It is deprecated, yes. I would now use:

    $product->get_regular_price();
    $product->get_sale_price();
    $product->get_price(); // gets current active price. Can be sale or regular price.
    $product->get_price_html(); // gets formatted price.

    Kind regards,

    Thread Starter Phil Emery

    (@noise)

    Thanks for the speedy reply.

    Do you mean that “woocommerc_price” in :

    return $term . ' (' . woocommerce_price( $_product->get_price() ) . ')';

    is depreciated? or the “$_product->get_price()” ?

    Because even when you use the “$product” it still outputs:

    Product Variaion (<span class="woocommerce-Price-amount amount"><bdi><span class="woocommerce-Price-currencySymbol">$</span>0.00</bdi></span>)

    Perhaps it’s some security thing so you can’t put wacky stuff in a pop up menu?

    • This reply was modified 3 years, 3 months ago by Phil Emery.
    Thread Starter Phil Emery

    (@noise)

    replacing “woocommerce_price” with “wc_price” still outputs all the same code.

    Thread Starter Phil Emery

    (@noise)

    Fixed it.

    Use the strip_tags() to get rid of all that wacky un-html’ed html code:

    strip_tags( wc_price( $product->get_price() ) )

    Plugin Support con

    (@conschneider)

    Engineer

    Hi again,

    or the “$_product->get_price()” ?

    The underscore in $_product is the same as $product. The leading underscore changes the scope of the variable to private / protected, it is quite old school (PHP4).

    Because even when you use the “$product” it still outputs:

    I think that happens due to woocommerce_price() (which was the deprecated part, I should have made that clearer), not because of your method get_price().

    Use the strip_tags() to get rid of all that wacky un-html’ed html code:
    strip_tags( wc_price( $product->get_price() ) )

    Practical approach, thank you for sharing your solution. You can also let WordPress handle this: https://developer.wordpress.org/reference/functions/wp_strip_all_tags/

    Kind regards,

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Is “woocommerce_price( $_product->get_price() )” depreciated?’ is closed to new replies.