No WooCommerce, defini alguns atributos de produto com seus nomes e escrevi um código que deve me permitir definir nomes alternativos de rótulos de atributos em nível de produto variável.
Assim, em páginas de produtos individuais, por exemplo, para alguns produtos o atributo “Cor” pode ser exibido como “Cor da camiseta” e em alguns outros, “cor do chapéu”. Dessa forma, tenho a flexibilidade de usar os mesmos atributos para diferentes tipos de itens, mas exibindo diferentes nomes de rótulos de atributos no frontend.
// 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;
}
}
}
Mas não consigo fazer funcionar. Se eu inserir alguns "ecos" dentro da função "rótulo de atributo personalizado", poderei ver os rótulos de atributos alternativos salvos apenas no formulário de variações e repetidos quantas vezes for o número de variações que tenho, para aquele produto. Mas o nome do rótulo do atributo do produto permanece o antigo.