WooCommerce 在可变价格后显示变量描述

WooCommerce displaying variable description after variable price

我正在尝试将变量描述显示到 woocommerce 产品页面中。 我安装了一个名为 woocommerce 单选按钮的插件,用于将我的可变产品和价格显示为单选按钮而不是 select。

我正在这个插件中编辑 variable.php 文件(然后我会在完成后将其传输到我的子主题)并且基本上我需要将变量描述显示到标签中,作为一个名为 $ 的变量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 );

我无法从数据库中恢复此数据,因为我不了解数据库的结构。

你知道任何简码或函数来恢复它并显示每个可变价格的变量描述吗?

在我正在编辑的函数中,作为第一个标签的单选按钮旁边的每个变体都正确显示了价格。函数完整代码为:

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

    }
}

谢谢

要获取 post 元数据,您可以改用 WordPress get_post_meta() 函数(它更短且价格实惠)。

所以你的代码现在应该是:

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