1

Hello I m trying to make available the prices for the variations on the dropdown, at some point I were able to achieve it when were only one variable, but now I have two variables for the product but the price on the dropdown is not updating.

enter image description here

Based on Variable product attribute: Customizing each displayed radio buttons text value answer code, here is the code I use to get on the first step, but I need help to update the price of variations up to the second variable selected.

add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
function display_price_in_variation_option_name( $term ) {
    global $wpdb, $product;
    if ( empty( $term ) ) return $term;
    if ( empty( $product->id ) ) return $term;
    $id = $product->get_id();
    $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_key != 'attribute_pa_alto'
                    AND postmeta.meta_value = '$term_slug'
                    AND products.post_parent = $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] );
        //EDITADO PARA MOSTRAR CUANDO NO HAY STOCK MUESTRA AGOTADO
        if($_product->managing_stock() && $_product->get_stock_quantity() < 1){
            return $term . ' - AGOTADO';
        }else{
            return $term . ' - (' . wp_kses( woocommerce_price( $_product->get_price() ), array() ) . ')';
        }
    }
    return $term;
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Guido
  • 401
  • 4
  • 7
  • I think @loictheaztec can help on this – Guido Apr 27 '21 at 21:59
  • 1
    This is just not possible with PHP… May be it could be possible using javascript in something much more complicated. – LoicTheAztec Apr 28 '21 at 02:44
  • yes, It must be done with javascript, maybe creating an array with all prices and names, then when the second variable is clicked reset the dropdown names. – Guido Apr 28 '21 at 15:24

0 Answers0