无法在 woocommerce 上显示替代属性标签

问题描述 投票:0回答:1

在 woocommerce,我用它们的名称定义了一些属性。我编写了一段代码,希望能让我有机会为每个可变产品级别定义替代属性名称。因此,对于某些产品,在单个产品页面上,例如属性“颜色”可以显示为“T恤的颜色”,在另一个可以显示为“帽子的颜色”。这样我就可以灵活地对不同类型的项目使用相同的属性,但在客户端显示不同的属性名称。

为了实现我的目标,我写道:

/ Create text input where text for custom label will be entered
add_action('woocommerce_product_options_attributes', 'add_custom_attribute_label_fields');
function add_custom_attribute_label_fields() {
    global $post;

    // Get the product attributes
    $product_attributes = maybe_unserialize(get_post_meta($post->ID, '_product_attributes', true));

    if (!empty($product_attributes)) {
        echo '<div class="options_group">';

        foreach ($product_attributes as $attribute_name => $attribute) {
            if ($attribute['is_taxonomy']) {
                $field_id = 'custom_attribute_label_' . $attribute_name;
                $label = get_post_meta($post->ID, $field_id, true);
              
                woocommerce_wp_text_input(array(
                    'id' => $field_id,
                    'label' => 'Name of the ATTRIBUTE ' . esc_html(wc_attribute_label($attribute["name"])) . ' for that product.',
                    'description' => esc_html('Please enter the name of that You would like to e shown at the single product page.'),
                    'desc_tip' => true,
                    'value' => esc_attr($label),
                ));
            }
        }
        echo '</div>';
    }
}

// Save the custom attribute labels
add_action('woocommerce_admin_process_product_object', 'save_custom_attribute_labels');
function save_custom_attribute_labels($product) {
    $product_id = $product->get_id();

    // Get the product attributes
    $product_attributes = maybe_unserialize(get_post_meta($product_id, '_product_attributes', true));

    if (!empty($product_attributes)) {
        foreach ($product_attributes as $attribute_name => $attribute) {
            if ($attribute['is_taxonomy']) {
                $field_id = 'custom_attribute_label_' . $attribute_name;
                if (isset($_POST[$field_id])) {
                    update_post_meta($product_id, $field_id, sanitize_text_field($_POST[$field_id]));
                }
            }
        }
    }
}
// Filter to display custom attribute label
add_filter('woocommerce_attribute_label', 'custom_attribute_label', 100, 3);
function custom_attribute_label($label, $name, $attproduct) {
    // Ensure $attproduct is an instance of WC_Product
    if (is_a($attproduct, 'WC_Product')) {
        $product_id = $attproduct->get_id();
        
        // Retrieve the custom label
        $custom_label = get_post_meta($product_id, 'custom_attribute_label_' . $name, true);

        // Check if custom label is not empty and return it
        if (!empty($custom_label)) {
            return $custom_label;
        }
    }

但是它不起作用。如果我在函数“自定义属性标签”中插入一些“回声”,那么我就可以在变体表单中看到保存的替代属性标签,并重复与该产品的变体数量相同的次数。但产品属性标签还是旧的。

我被困住了。任何帮助将不胜感激。

woocommerce attributes
1个回答
0
投票

您的代码中有一些错误,请尝试以下修改后的代码:

// Add text input setting field where text for custom label will be entered
add_action('woocommerce_product_options_attributes', 'add_custom_attribute_label_setting_fields');
function add_custom_attribute_label_setting_fields() {
    global $post, $product_object;

    if ( $attributes = $product_object->get_attributes() ) {
        echo '<div class="options_group">';

        foreach ($attributes as $name => $attribute) {
            if ( $attribute->is_taxonomy() ) {
                $field_key = 'custom_attr_label_'.$name;
              
                woocommerce_wp_text_input(array(
                    'id' => $field_key,
                    'label' => sprintf(__('Product attribute "%s" displayed custom label name'), wc_attribute_label($name)),
                    'description' => esc_html('Please enter the name of that You would like to e shown at the single product page.'),
                    'desc_tip' => true,
                ));
            }
        }
        echo '</div>';
    }
}

// Save the custom attribute labels
add_action('woocommerce_admin_process_product_object', 'save_custom_attribute_labels_fields_values');
function save_custom_attribute_labels_fields_values($product) {
    if ( $attributes = $product->get_attributes() ) {
        foreach ($attributes as $name => $attribute) {
            if ( $attribute->is_taxonomy() ) {
                $field_key = 'custom_attr_label_'.$name;

                if ( isset($_POST[$field_key]) ) {
                    $product->update_meta_data($field_key, sanitize_text_field($_POST[$field_key]));
                }
            }
        }
    }
}

// Display product attributes custom labels in single product pages
add_filter('woocommerce_attribute_label', 'display_product_attributes_custom_labels', 20, 3);
function display_product_attributes_custom_labels( $label, $name, $product ) {
    $custom_label = ''; // Initialize

    if ( ! is_admin() && ! is_a($product, 'WC_Product') ) {
        global $product;

        if ( is_a($product, 'WC_Product') ) {
            $custom_label = $product->get_meta('custom_attr_label_'.$name);
        }
    }
    return $custom_label ? $custom_label : $label;
}

代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。

© www.soinside.com 2019 - 2024. All rights reserved.