WooCommerce在变量价格后显示变量描述
问题描述:
我试图在woocommerce产品页面中显示变量描述。 我安装了一个名为woocommerce单选按钮的插件,用于将我的变量产品和价格显示为单选按钮而不是选择。WooCommerce在变量价格后显示变量描述
我正在编辑这个插件中的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);
}
}
谢谢
答
为了获得后元数据,可以使用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);
}
}