PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 10, 2022

[FIXED] WooCommerce show content after variation labels

 January 10, 2022     hook-woocommerce, php, woocommerce, woocommerce-theming, wordpress     No comments   

Issue

I want to add content after variation name.

I find this hook and saw it's working before, but no on my test it's not :(

I want to ask, is there another hook/working method I can use to add content?

add_filter('woocommerce_attribute_label', 'custom_attribute_label', 10, 3);
function custom_attribute_label($label, $name, $product){
    $taxonomy = 'pa_' . $name;

    if ($taxonomy == 'pa_size')
        $label .= '<div class="custom-label">' . __('MY TEXT', 'woocommerce') . '</div>';

    return $label;
}

Code reference:
Adding a custom text next to a specific product attribute label in WooCommerce


Solution

If you use $label variable, it will return the name without "pa_" prefix. Some variations have "pa_" prefixes and some don't.

So you don't need to create an extra variable (i.e $taxonomy). Just use $label variable, like this:

add_filter('woocommerce_attribute_label', 'custom_attribute_label', 10, 3);

function custom_attribute_label($label, $name, $product)
{

    if ('Logo' == $label) 
    {
        $label .= '<div class="custom-label">' . __('MY TEXT', 'woocommerce') . '</div>';
    }

    return $label;
}

enter image description here


You could use switch statement for checking multiple conditions like this:

add_filter('woocommerce_attribute_label', 'custom_attribute_label', 10, 3);

function custom_attribute_label($label, $name, $product)
{

    switch ($label) 
    {
        case 'Logo':
            $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;
}

enter image description here



Answered By - Ruvee
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing