【问题标题】:Woocommerce get attribute custom field in wc attribute labels functionWoocommerce在wc属性标签功能中获取属性自定义字段
【发布时间】:2022-01-19 16:27:45
【问题描述】:
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 class="custom-label">' . __('MY TEXT AFTER LOGO', 'woocommerce') . '</div>';
            break;
        case 'color':
            $label .= '<div class="custom-label">' . __('MY TEXT AFTER COLOR', 'woocommerce') . '</div>';
            break;
    }

    return $label;
}

您好,是否可以在woocommerce_attribute_label 挂钩中将自定义注册字段放入属性后端页面。 字段的 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" );
*/

【问题讨论】:

  • 能否向我们展示您是如何注册“自定义字段”的?
  • @Ruvee 当然,主题已更新:)
  • 我打算完全按照您设置的方式复制它。

标签: php wordpress woocommerce hook-woocommerce woocommerce-theming


【解决方案1】:

如何保存Attribute Description 有点棘手,因为您将它保存在选项表中。

您正在使用属性wc_attribute_attribute_description-$id 的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 class="custom-label">' . $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 属性,所以不要忘记在woocommerce_attribute_label 过滤器挂钩中将pa_test2 更改为您的属性名称。

经过测试且有效

后端分类名称

后端属性说明

单一产品页面

【讨论】:

  • 整洁的解决方案...
猜你喜欢
  • 1970-01-01
  • 2015-05-03
  • 2021-09-08
  • 2012-11-02
  • 2016-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多