woocommerce_variation_option_name on frontend

125

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;

}
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;

}

Comments

Submit
0 Comments