WooCommerce displaying variable description after

2019-07-11 06:18发布

问题:

I'm trying to display the variable description into a woocommerce product page. I installed a plugin named woocommerce radio buttons, for displaying my variable products and prices as a radio button instead of a select.

I am editing the variable.php file, in this plugin (then I will transfer it to my child theme once finished) and basically I need to show the variable description into a label, as a variable named $variable_description.

    printf( '<div>
    <input type="radio" name="%1$s" value="%2$s" id="%3$s" %4$s>
    <label for="%3$s">%5$s</label>
    <label>'.$variable_description.'</label>
    </div>', $input_name, $esc_value, $id, $checked, $filtered_label );

I am not able to recover this data from the database, because of the structure of the database that I don't understand.

Do you know any shortcode or function to recover it and displaying the variable description for each variable price?

In the function I'm editing the price is correctly displayed for each variation next to the radio button as the first label. The full code of the function is:

if ( ! function_exists( 'print_attribute_radio' ) ) {
    function print_attribute_radio( $checked_value, $value, $label, $name, $product_id ) {

        // This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
        $checked = sanitize_title( $checked_value ) === $checked_value ? checked( $checked_value, sanitize_title( $value ), false ) : checked( $checked_value, $value, false );

        $input_name = 'attribute_' . esc_attr( $name ) ;
        $esc_value = esc_attr( $value );
        $id = esc_attr( $name . '_v_' . $value );
        $filtered_label = apply_filters( 'woocommerce_variation_option_name', $label );

    //here is where I try to recover the variable_description

        global $wpdb;
        $post_id = $product_id + 3;

         $querystr = "SELECT wpostmeta.meta_value
                    FROM $wpdb->postmeta wpostmeta
                    WHERE wpostmeta.post_id = '$post_id'
                    AND wpostmeta.meta_key = '_variation_description'
                    ORDER BY wpostmeta.meta_value DESC
                    "; 


     $variable_description = $wpdb->get_var($querystr);

        printf( '<div>
        <input type="radio" name="%1$s" value="%2$s" id="%3$s" %4$s>
        <label for="%3$s">%5$s</label>
        <label>'.$variable_description.'</label>
        </div>', $input_name, $esc_value, $id, $checked, $filtered_label );

    }
}

Thank you

回答1:

To get post meta data you can use WordPress get_post_meta() function instead (It's much shorter and affordable).

So your code should be now:

if ( ! function_exists( 'print_attribute_radio' ) ) {
    function print_attribute_radio( $checked_value, $value, $label, $name, $product_id ) {

        // This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
        $checked = sanitize_title( $checked_value ) === $checked_value ? checked( $checked_value, sanitize_title( $value ), false ) : checked( $checked_value, $value, false );

        $input_name = 'attribute_' . esc_attr( $name ) ;
        $esc_value = esc_attr( $value );
        $id = esc_attr( $name . '_v_' . $value );
        $filtered_label = apply_filters( 'woocommerce_variation_option_name', $label );

        // HERE the product variation description
        $variation_id = $product_id + 3;
        $variable_description = get_post_meta( $variation_id, '_variation_description', true );

        printf( '<div>
        <input type="radio" name="%1$s" value="%2$s" id="%3$s" %4$s>
        <label for="%3$s">%5$s</label>
        <label>'.$variable_description.'</label>
        </div>', $input_name, $esc_value, $id, $checked, $filtered_label );
    }
}