2016-12-07 57 views
2

我試圖在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); 

    } 
} 

謝謝

回答

1

爲了獲得後元數據,可以使用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); 
    } 
}