Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

460
Visualizações
How to concatenate the name of a product with the name of its attributes after "woocommerce_thankyou" hook fires?
$order_items = $order->get_items();

foreach ($order_items as $item_key => $item) {
 $product = $item->get_product(); // Get the WC_Product Object

 if( $product->is_type( 'variation' ) ){
    $attributes = $product->get_attributes();
    $variation_names = array();

    if( $attributes ){
        foreach ( $attributes as $key => $value) {
            $variation_key =  end(explode('-', $key));
            $variation_names[] = ucfirst($variation_key) .' : '. $value;
        }
    }
    echo implode( '<br>', $variation_names );
 }
}

The output of this code is as follows:

After echo:

%da%86%d8%b1%d8%a8%db%8c

and Incompatible with Persian language

I need the end result to be something like this:

"product Name , color:red, size:85" . . . . . . . . . .

Thanks to all the friends;

This is my new solution and it has been tested and it works:

Note:attributes $value label added:

//اضافه کردن نام ویژگی به اسم محصول
//Add attribute label name to product name

if( $product->is_type( 'variation' ) ){
        
$s ='';
$s1 = '';
foreach ($product->get_attributes() as $taxonomy => $attribute_obj ) {

 // Get the attribute label
 $attribute_label_name = wc_attribute_label($taxonomy);

 //convert to array 
 $attribute_arr = json_decode(json_encode($attribute_obj),true);
 $term_name      = get_term_by( 'slug', $attribute_arr, $taxonomy)->name;

 $s = $s . $s1 .$attribute_label_name.':'.$term_name;
 $s1 = ', ';

}

$name = $name . '(' .$s. ')';
echo '<p>' . $name. '</p>';


}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can use wc_attribute_label() to get the proper label name of the attribute and you can use get_title() to get the product title. try the below code.

function display_attributes_and_name( $order_id ){
    $order = wc_get_order($order_id); //<--check this line
    $order_items = $order->get_items();
    foreach ($order_items as $item_key => $item) {
        $product = $item->get_product(); // Get the WC_Product Object
        if( $product->is_type( 'variation' ) ){
            $attributes = $product->get_attributes();
            $variation_names = array();
            if( $attributes ){
                foreach ( $attributes as $key => $value) {
                    $variation_key =  end(explode('-', $key));
                    $variation_names[] = wc_attribute_label( $variation_key ) .' : '. $value;
                }
            }
            echo $product->get_title().', '.implode( ',', $variation_names );
        }
    }
}

add_action( 'woocommerce_thankyou', 'display_attributes_and_name', 10, 1 );

Tested and works

enter image description here

over 4 years ago · Santiago Trujillo Relatório

0

What @Bhautik said

Plus

"Incompatible with Persian language"

To me, it looks like it's been encoded (by your browser perhaps!), so in order to decode it, we could use urldecode function, like so:

$test = '%da%86%d8%b1%d8%a8%db%8c';

$decode_test = urldecode($test);

echo $decode_test;

Which will output this:

چربی

Does that make sense?

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda