• Resolved carmolim

    (@carmolim)


    Hello, I’m trying to get the titles of the variations inside the content-product.php but I can’t find a way to retrieve it, I looked inside the variable.php file but didn’t understood how it gets the de title. Someone could help me?

    Thanks!

    https://www.ads-software.com/plugins/woocommerce/

Viewing 15 replies - 1 through 15 (of 18 total)
  • ancawonka

    (@ancawonka)

    Variations don’t really have titles – they get automatically saved when you add a variation, and are something like “Variation 1245 of Productname”.

    What are you trying to do? I’ve done some stuff to show variation information in a single product page and could maybe give you some pointers.

    How familiar are you w/ working with hooks and filters in WordPress?

    Thread Starter carmolim

    (@carmolim)

    Actually I found out later that the information I wanted was the variation name. After some research I come up with this code. It solves my problem but I don’t know if is the best solution.

    <?php
    
        if ( $product->is_type( 'variable' ) ) {
    
        	$attributes = $product->get_attributes();
    
        	$attribute_name = '';
    
        	foreach ( $attributes as $attribute ) {
    
                $attribute_name = $attribute['name'];
            }
    
        	$terms = wc_get_product_terms( $product->id, $attribute_name, array( 'fields' => 'all' ) );
    
        	for ( $i=0; $i < count($terms); $i++ ) {
    
        		if ( $i == count($terms)-1 ) {
        			echo $terms[$i]->name ;
        		}
        		else {
        			echo $terms[$i]->name . ' | ';
        		}
        	}
        }
    ?>

    How could I improve it? The major problem is in the loop for find the attribute name. In my case I just have one atribute, so thats ok, but in other situations, with more attributes that may cause a problem.

    Plugin Contributor Mike Jolley

    (@mikejolley)

    I think the confusion is between variable and variation.

    Each variation can have a title based on its assigned attributes with https://github.com/woothemes/woocommerce/blob/master/includes/class-wc-product-variation.php#L741

    however, the variable product itself is assigned ALL attributes of the variations inside, so displaying this in the title would be confusing.

    Thread Starter carmolim

    (@carmolim)

    I got confused about name and title because this function. It calls $this->get_title()
    And this function returns 3 informations, I just need the $this->get_title() but I couldn’t figure out how to do that.

    Plugin Contributor Mike Jolley

    (@mikejolley)

    Maybe you can explain what you’re trying to do and where?

    Thread Starter carmolim

    (@carmolim)

    I’m trying to modify the content-product.php.I added a new hook under the product title to show the variations available for that product. In my case I have a variable product that has two kinds of package, a can and a pouch and I want to add this information in under the title, like this: Can | Pouch.

    Thanks!

    Plugin Contributor Mike Jolley

    (@mikejolley)

    So you can use ->get_children() to get the variation IDs. Then loop over each ID, use wc_get_product() to get the variation object, and then ->get_title() on each. Should work?

    Thread Starter carmolim

    (@carmolim)

    I just tested, but the get->title() returns the title of the “master” product, not the variation title.

    Plugin Contributor Mike Jolley

    (@mikejolley)

    I said you need to loop over each variation (child) and call get_title on each. Not call get_title on the variable product parent.

    Thread Starter carmolim

    (@carmolim)

    yes I did that:

    $childrens = $product->get_children();
    
    for ( $i=0; $i < count( $childrens ); $i++ ) {
    
        $variation = wc_get_product( $childrens[$i] );
    
        echo = $variation->get_title();
    
    }
    ancawonka

    (@ancawonka)

    @carmolim, try this

    echo $variation->get_formatted_name();

    instead of

    echo $variation->get_title();

    Thread Starter carmolim

    (@carmolim)

    This function almost work. It returns more information than I need. Would be great to get just the “title” or “name”.

    ancawonka

    (@ancawonka)

    Variations don’t actually have names or titles by default – as you can see from the results you’re getting.

    If you want to give your variations specific “names” (instead of a description based on their attributes), you can use the description field, or add your own custom attribute.

    To can get the contents of the description field, you can do the following in your code where you’re getting the title now.

    echo $variation->get_variation_description();

    Thread Starter carmolim

    (@carmolim)

    Actually I already achieved what I wanted to do in the first code I posted. I just wanted to know if maybe a better solution was available.

    I my case I have the global attribute “Embalagem”, inside “Embalagem” I have 3 variations: “Lata 45g”, “Pouch 45g”, “Pouch 65g”.

    I want to list the variations avaliable for a product under the title in the default content-product.php.

    I think the solution @ancawonka is a good alternative, at least is a direct call to a property, don’t need to iterate and anything else. I’ll probably use it!

    Thanks!

    @carmolim

    May I have the code you have written to achieve this please. I also want exactly the same for my woocommerce store.

    Thanks in advance

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘How to get the variation title?’ is closed to new replies.