• Resolved MarieveC

    (@marievec)


    When I execute this code to get attributes one by one:

    $product = new WC_Product( $produit_id );
    $tous_produits[$produit_id]['Lien_Photo'] = $product->get_attribute( 'Lien_Photo' );
    $tous_produits[$produit_id]['Lien_Specsheet'] = $product->get_attribute( 'Lien_Specsheet' );
    $tous_produits[$produit_id]['Photometrie'] = $product->get_attribute( 'Photometrie' );
    $tous_produits[$produit_id]['ID_Famille'] = $product->get_attribute( 'ID_Famille' );
    $tous_produits[$produit_id]['ID_Sous_Famille'] = $product->get_attribute( 'ID_Sous_Famille' );
    $tous_produits[$produit_id]['ID_Manufacturier'] = $product->get_attribute( 'ID_Manufacturier' );
    $tous_produits[$produit_id]['ID_Marque'] = $product->get_attribute( 'ID_Marque' );
    $tous_produits[$produit_id]['ID_Serie'] = $product->get_attribute( 'ID_Serie' );
    $tous_produits[$produit_id]['ID_Voltage'] = $product->get_attribute( 'ID_Voltage' );
    $tous_produits[$produit_id]['ID_Source'] = $product->get_attribute( 'ID_Source' );
    $tous_produits[$produit_id]['ID_Type_de_source'] = $product->get_attribute( 'ID_Type_de_source' );
    $tous_produits[$produit_id]['ID_Installation'] = $product->get_attribute( 'ID_Installation' );
    $tous_produits[$produit_id]['guide_stock'] = $product->get_attribute( 'ID_GD' );
    $tous_produits[$produit_id]['Prix_Budget'] = $product->get_attribute( 'Prix_Budget' );
    $tous_produits[$produit_id]['Lumen_emis'] = $product->get_attribute( 'Lumen_emis' );
    $tous_produits[$produit_id]['Wattage'] = $product->get_attribute( 'Wattage' );
    $tous_produits[$produit_id]['id_es'] = $product->get_attribute( 'ID_ES' );
    $tous_produits[$produit_id]['id_dlc'] = $product->get_attribute( 'ID_DLC' );
    $tous_produits[$produit_id]['id_dlcp'] = $product->get_attribute( 'ID_DLCP' );

    It works:

    Array ( [1078] => Array ( [title] => H1499T-1495P [description] => LUMINAIRE ENCASTRÉ TYPE GIMBAL MR16 DE COULEUR BLANC [Lien_Photo] => HALO_1495.jpg [Lien_Specsheet] => HALO 1495.pdf [Photometrie] => Non Disponible [ID_Famille] => Luminaires Downlight [ID_Sous_Famille] => Orientable [ID_Manufacturier] => Eaton [ID_Marque] => Halo [ID_Serie] => HALO 1495 [ID_Voltage] => 120V [ID_Source] => INC [ID_Type_de_source] => MR16 [ID_Installation] => Encastré Rond 4'' [guide_stock] => O [Prix_Budget] => $65 [Lumen_emis] => ? [Wattage] => ? [id_es] => N [id_dlc] => N [id_dlcp] => N ) )

    When I try this code to get all the attributes in one call:

    $product->get_attributes();

    It doesn’t work, all values are empty:

    Array ( [pa_id_gs] => Array ( [name] => pa_id_gs [value] => [position] => 0 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_id_gd] => Array ( [name] => pa_id_gd [value] => [position] => 1 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_id_t20] => Array ( [name] => pa_id_t20 [value] => [position] => 2 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_id_es] => Array ( [name] => pa_id_es [value] => [position] => 3 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_id_dlc] => Array ( [name] => pa_id_dlc [value] => [position] => 4 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_id_dlcp] => Array ( [name] => pa_id_dlcp [value] => [position] => 5 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_numero_de_catalogue] => Array ( [name] => pa_numero_de_catalogue [value] => [position] => 6 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_id_voltage] => Array ( [name] => pa_id_voltage [value] => [position] => 7 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_id_manufacturier] => Array ( [name] => pa_id_manufacturier [value] => [position] => 8 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_id_marque] => Array ( [name] => pa_id_marque [value] => [position] => 9 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_id_serie] => Array ( [name] => pa_id_serie [value] => [position] => 10 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_id_famille] => Array ( [name] => pa_id_famille [value] => [position] => 11 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_id_sous_famille] => Array ( [name] => pa_id_sous_famille [value] => [position] => 12 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_id_installation] => Array ( [name] => pa_id_installation [value] => [position] => 13 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_id_source] => Array ( [name] => pa_id_source [value] => [position] => 14 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_id_type_de_source] => Array ( [name] => pa_id_type_de_source [value] => [position] => 15 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_lumen_emis] => Array ( [name] => pa_lumen_emis [value] => [position] => 16 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_wattage] => Array ( [name] => pa_wattage [value] => [position] => 17 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_description] => Array ( [name] => pa_description [value] => [position] => 18 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_prix_budget] => Array ( [name] => pa_prix_budget [value] => [position] => 19 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_lien_photo] => Array ( [name] => pa_lien_photo [value] => [position] => 20 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_lien_specsheet] => Array ( [name] => pa_lien_specsheet [value] => [position] => 21 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_photometrie] => Array ( [name] => pa_photometrie [value] => [position] => 22 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_id_produit] => Array ( [name] => pa_id_produit [value] => [position] => 23 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_id_actif] => Array ( [name] => pa_id_actif [value] => [position] => 24 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) )

    I also tried this:

    $attr = get_post_meta( $produit_id, '_product_attributes' );

    Same thing: all values are empty.

    And I’m sure I have values in it because when I call the values one by one, it works.

    This one works too:
    get_the_terms( $produit_id, 'pa_id_famille')

    But I would really like to get all the attributes with one call, because it will help a lot for the loading time.

    This one only returns the attributes, not the values of it:
    wc_get_attribute_taxonomies()

    I need help! I don’t understand why I can retrieve all the datas with get_attributes() when get_attribute() works just fine. Thanks!

    https://ww.wp.xz.cn/plugins/woocommerce/

Viewing 10 replies - 1 through 10 (of 10 total)
  • Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    Seems that if it’s a global attribute, the value will be empty. They are terms at that point. So you’d need to call get_attribute() to get the term’s values.

    Or you can use wc_get_product_terms(): https://github.com/woothemes/woocommerce/blob/1c333b4a18243f89817a701ec84253a173505c50/includes/wc-term-functions.php#L28

    Thread Starter MarieveC

    (@marievec)

    But wc_get_product_terms() return one attribute at a time…

    $p_terms = wc_get_product_terms($produit_id, 'pa_lien_photo');
    Array ( [0] => HALO_1495.jpg )

    There is no way for me to get all the attributes with one call?

    Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    Not looking like you can. I’d try re-purposing the current layered nav widget code. That’s been through the fire with some performance enhancements already.

    Thread Starter MarieveC

    (@marievec)

    Is it possible if I declare the attributes another way ? Like to use get_attributes and have the values ?

    Thread Starter MarieveC

    (@marievec)

    I think it’s probably the case (my attributes are not declared correctly) because when I try to get a listing of products with a certain attribute, it returns nothing.

    This doesn’t work:

    $params = array('post_type' => 'product', 'meta_query' => array(
    array(
    'key' => 'ID_T20',
    'value' => 'O',
    'compare' => '='
    ),
    ));
    $wc_query = new WP_Query($params);

    So I have to get all the products and do some “if” after, in the loop, like this:

    $params = array('post_type' => 'product',
    'posts_per_page' => -1,
    );
    $wc_query = new WP_Query($params);
    while ($wc_query->have_posts()) :
    $wc_query->the_post();
    $produit_id = get_the_ID();
    $product = new WC_Product( $produit_id );
    if ($product->get_attribute( 'ID_T20' ) == 'O')
    {
    ...

    It’s probably something that I don’t understand and don’t do correctly… or it’s a bug but I doubt it.

    Thanks again for your help.

    Plugin Contributor Mike Jolley

    (@mikejolley)

    Combine $product->get_attributes(); to get attribute names with https://codex.ww.wp.xz.cn/Function_Reference/wp_get_object_terms to get the actual terms that are set.

    Thread Starter MarieveC

    (@marievec)

    I have the attributes names, I’m trying to get the values of all attributes with one call instead of 19.

    Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    wp_get_object_terms() accepts an array in the first parameter. So one call for get_attributes(), and one call to get the terms with wp_get_object_terms().

    Thread Starter MarieveC

    (@marievec)

    It works Caleb!! 😀

    Here is the code:

    $taxo = array
    (
    'pa_id_gs',
    'pa_id_gd',
    'pa_id_t20',
    'pa_id_es',
    'pa_id_dlc',
    'pa_id_dlcp',
    'pa_numero_de_catalogue',
    'pa_id_voltage',
    'pa_id_manufacturier',
    'pa_id_marque',
    'pa_id_serie',
    'pa_id_famille',
    'pa_id_sous_famille',
    'pa_id_installation',
    'pa_id_source',
    'pa_id_type_de_source',
    'pa_lumen_emis',
    'pa_wattage',
    'pa_description',
    'pa_prix_budget',
    'pa_lien_photo',
    'pa_lien_specsheet',
    'pa_photometrie',
    'pa_id_produit',
    'pa_id_actif'
    );
    $product_terms = wp_get_object_terms( $produit_id, $taxo );
    foreach ($product_terms as $k => $v)
    {
    $tous_produits[$produit_id][$v->taxonomy] = $v->name;
    }

    And for my other problem, I found a way to do the query, now that I know that attributes are taxonomy:

    $params = array(
    'post_type' => 'product',
    'posts_per_page' => -1,
    'tax_query' => array(
    array(
    'taxonomy' => 'pa_id_t20',
    'field' => 'slug',
    'terms' => 'O',
    ),
    ));
    $wc_query = new WP_Query($params);

    Thank you very much for your help!! 😀

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘get_attributes() problem when get_attribute() works fine’ is closed to new replies.