add_filter('woocommerce_attribute_label', 'custom_attribute_label', 10, 3);
function custom_attribute_label($label, $name, $product) {
$term_meta = get_term_meta($name, 'attribute_description', true);
switch ($label) {
case 'size':
$label .= '<div >' . __('MY TEXT AFTER LOGO', 'woocommerce') . '</div>';
break;
case 'color':
$label .= '<div >' . __('MY TEXT AFTER COLOR', 'woocommerce') . '</div>';
break;
}
return $label;
}
您好,是否可以在woocommerce_attribute_labelhook中將自定義注冊欄位獲取到屬性后端頁面中。欄位的 id 為“ attribute_description ”,我堅持使用一種顯示前端的方法。
任何幫助將不勝感激!

注冊欄位的代碼(我關注在線主題并從中學習):
// Add variation description
function my_edit_wc_attribute_my_field() {
$id = isset( $_GET['edit'] ) ? absint( $_GET['edit'] ) : 0;
$value = $id ? get_option( "wc_attribute_attribute_description-$id" ) : '';
?>
<div id="attr-desc">
<tr class="form-field">
<th scope="row" valign="top">
<label for="attribute-description">Attribute Description</label>
</th>
<td>
<textarea name="attribute_description" id="attribute-description" rows="5" cols="40"><?php echo esc_attr( $value ); ?></textarea>
<!--<input name="my_field" id="my-field" value="<?php //echo esc_attr( $value ); ?>" />-->
<p class="description">Add unique description that will appear in balloon field in single product page.</p>
</td>
</tr>
</div>
<script>
jQuery(function($) {
$('#attr-desc').appendTo('.form-field:eq(1)');
//$('#attr-desc').hide();
});
</script>
<?php
}
add_action( 'woocommerce_after_add_attribute_fields', 'my_edit_wc_attribute_my_field' );
add_action( 'woocommerce_after_edit_attribute_fields', 'my_edit_wc_attribute_my_field' );
// Save variation description
function my_save_wc_attribute_my_field( $id ) {
if ( is_admin() && isset( $_POST['attribute_description'] ) ) {
$option = "wc_attribute_attribute_description-$id";
update_option( $option, sanitize_text_field( $_POST['attribute_description'] ) );
}
}
add_action( 'woocommerce_attribute_added', 'my_save_wc_attribute_my_field' );
add_action( 'woocommerce_attribute_updated', 'my_save_wc_attribute_my_field' );
// Delete option, when attribute is deleted
add_action( 'woocommerce_attribute_deleted', function ( $id ) {
delete_option( "wc_attribute_attribute_description-$id" );
});
// Then for example, on a taxonomy/term archive page, you can get the My Field value like so:
/*
$term = get_queried_object();
$attr_id = wc_attribute_taxonomy_id_by_name( $term->taxonomy );
$my_field = get_option( "wc_attribute_my_field-$attr_id" );
*/
uj5u.com熱心網友回復:
保存方式有點棘手,Attribute Description因為您將其保存在選項表中。
您正在使用屬性 id 保存選項,wc_attribute_attribute_description-$id因此您必須遵循相同的程序才能從選項中獲取,為此,您需要該屬性的 id。
在woocommerce_attribute_label過濾器鉤子中,您只能獲得$name屬性的 。所以我們必須獲得屬性分類 id。并且,為此您可以使用wc_attribute_taxonomy_id_by_name()函式。
$id = wc_attribute_taxonomy_id_by_name( $name );
完整的代碼。
function custom_attribute_label($label, $name, $product) {
$id = wc_attribute_taxonomy_id_by_name( $name );
$value = get_option( "wc_attribute_attribute_description-$id" );
switch ($name) {
case 'pa_test2':
$label .= '<div >' . $value . '</div>';
break;
}
return $label;
}
add_filter('woocommerce_attribute_label', 'custom_attribute_label', 10, 3);
// Add variation description
function my_edit_wc_attribute_my_field() {
$id = isset( $_GET['edit'] ) ? absint( $_GET['edit'] ) : 0;
$value = $id ? get_option( "wc_attribute_attribute_description-$id" ) : '';
?>
<div id="attr-desc">
<tr class="form-field">
<th scope="row" valign="top">
<label for="attribute-description">Attribute Description</label>
</th>
<td>
<textarea name="attribute_description" id="attribute-description" rows="5" cols="40"><?php echo esc_attr( $value ); ?></textarea>
<p class="description">Add unique description that will appear in balloon field in single product page.</p>
</td>
</tr>
</div>
<script>
jQuery(function($) {
$('#attr-desc').appendTo('.form-field:eq(1)');
});
</script>
<?php
}
add_action( 'woocommerce_after_add_attribute_fields', 'my_edit_wc_attribute_my_field' );
add_action( 'woocommerce_after_edit_attribute_fields', 'my_edit_wc_attribute_my_field' );
// Save variation description
function my_save_wc_attribute_my_field( $id ) {
if ( is_admin() && isset( $_POST['attribute_description'] ) ) {
$option = "wc_attribute_attribute_description-$id";
update_option( $option, sanitize_text_field( $_POST['attribute_description'] ) );
}
}
add_action( 'woocommerce_attribute_added', 'my_save_wc_attribute_my_field' );
add_action( 'woocommerce_attribute_updated', 'my_save_wc_attribute_my_field' );
筆記
我已經復制了整個案例,所以我添加了test2屬性,所以不要忘記pa_test2在woocommerce_attribute_label過濾器掛鉤中更改為您的屬性名稱。
測驗和作業
后端分類名稱

后端屬性說明

單品頁面

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/385211.html
標籤:php WordPress的 求购 钩子商务 woocommerce-主题
上一篇:如何隱藏帖子下的類別選項卡?
