WooCommerce在可变价格后显示可变描述 [英] WooCommerce displaying variable description after variable price
问题描述
我正在尝试将变量描述显示到woocommerce产品页面中. 我安装了一个名为woocommerce单选按钮的插件,用于将我的可变产品和价格显示为单选按钮而不是单选按钮.
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.
我正在此插件中编辑variable.php文件(完成后将其传输到我的子主题中),基本上我需要将变量描述显示在标签中,作为名为$ variable_description的变量.
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 );
}
}
谢谢
推荐答案
To get post meta data you can use WordPress get_post_meta()
function instead (It's much shorter and affordable).
因此您的代码应为:
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 );
}
}
这篇关于WooCommerce在可变价格后显示可变描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!