woocommerce_variation_option_name on frontend code example

Example 1: woocommerce_variation_option_name on frontend

add_filter( 'woocommerce_variation_option_name','display_price_in_variation_option_name');

function display_price_in_variation_option_name( $term ) {
    global $product;

    if ( empty( $term ) ) {
        return $term;
    }
    if ( empty( $product->id ) ) {
        return $term;
    }

    $variation_id = $product->get_children();

    foreach ( $variation_id as $id ) {
        $_product       = new WC_Product_Variation( $id );
        $variation_data = $_product->get_variation_attributes();

        foreach ( $variation_data as $key => $data ) {

            if ( $data == $term ) {
                $html  = wp_kses( woocommerce_price( $_product->get_price() ), array() );
                $html .= ' - ' . $term;
                $html .= ( $_product->get_stock_quantity() ) ? ' - ' . $_product->get_stock_quantity() : '';
                return $html;
            }
        }
    }

    return $term;

}

Example 2: woocommerce_variation_option_name on frontend

add_filter( 'woocommerce_variation_option_name','display_price_in_variation_option_name');

function display_price_in_variation_option_name( $term ) {
global $product;

if ( empty( $term ) ) {
    return $term;
}
if ( empty( $product->id ) ) {
    return $term;
}

$variation_id = $product->get_children();


foreach ( $variation_id as $id ) {
    $_product       = new WC_Product_Variation( $id );
    $variation_data = $_product->get_variation_attributes();
    $stock_status = $_product->get_stock_status();
    $stock_status = str_replace( array('instock','outofstock','onbackorder'), array('In Stock','Out of Stock','Please allow a few extra days for delivery'), $stock_status );

    foreach ( $variation_data as $key => $data ) {

        if ( $data == $term ) {
            $html  = wp_kses( woocommerce_price( $_product->get_price() ), array() );
            $html .= ' - ' . $term;
            $html .= ( $stock_status ) ? ' - ' . $stock_status : '';
            return $html;
        }
    }
}

return $term;

}

Tags:

Misc Example