Afficher les attributs d’un produit simple dans les pages panier et commande (Créer un compte)

  • WordPress :6.1
  • Statut : non résolu
6 sujets de 1 à 6 (sur un total de 6)
  • Auteur
    Messages
  • #2440254
    Debrailly
    Participant
    Padawan WordPress
    58 contributions

    Bonjour,

    Je souhaite afficher les attributs dans le tableau de la page panier. Nos produits sont tous des simples produits.

    J’ai beau chercher un plugin ou du code, je ne trouve pas la solution. Je pense qu’il faut modifier le fichier cart.php mais n’étant pas développeuse, j’ai besoin d’aide.

    Si c’est plus simple d’afficher un tag ou un champ personnalisé, cela nous va aussi, nous recopierons l’attribut du produit dans le bon champ.

    Merci

    Ma configuration WP actuelle

    • Version de PHP/MySQL : 7.4/5.7
    • Thème utilisé : Hello
    • Extensions en place : Akismet, All in One Security, Collissimo shipping methods, CookiesYess, Elementor, Elementor pro, Happy elementor addons, Min and Max Quantity for woocommerce, PDF invoice & packaging slips for wc, Site kit by google, Up2pay e-commerce, UpdraftPlus, Woocommerce, WP-optimize, WPML, Yoast SEO
    • Nom de l’hébergeur : OVH
    • Ce sujet a été modifié le il y a 10 mois et 2 semaines par Debrailly.
    • Ce sujet a été modifié le il y a 10 mois et 2 semaines par Debrailly.
    #2440256
    Debrailly
    Participant
    Padawan WordPress
    58 contributions

    Je viens de trouver ce code à ajouter dans le fichier fonction.php et il fonctionne :

    <code class="hljs language-php"><span class="hljs-comment">/**
    * WooCommerce: show all product attributes, separated by comma, on cart page
    */</span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">isa_woo_cart_attribute_values</span>(<span class="hljs-params"> <span class="hljs-variable">$cart_item</span>, <span class="hljs-variable">$cart_item_key</span> </span>) </span>{
      
        <span class="hljs-variable">$item_data</span> = <span class="hljs-variable">$cart_item_key</span>[<span class="hljs-string">'data'</span>];
        <span class="hljs-variable">$attributes</span> = <span class="hljs-variable">$item_data</span>-><span class="hljs-title function_ invoke__">get_attributes</span>();
          
        <span class="hljs-keyword">if</span> ( ! <span class="hljs-variable">$attributes</span> ) {
            <span class="hljs-keyword">return</span> <span class="hljs-variable">$cart_item</span>;
        }
          
        <span class="hljs-variable">$out</span> = <span class="hljs-variable">$cart_item</span> . <span class="hljs-string">'<br />'</span>;
          
        <span class="hljs-variable">$count</span> = <span class="hljs-title function_ invoke__">count</span>( <span class="hljs-variable">$attributes</span> );
          
        <span class="hljs-variable">$i</span> = <span class="hljs-number">0</span>;
        <span class="hljs-keyword">foreach</span> ( <span class="hljs-variable">$attributes</span> <span class="hljs-keyword">as</span> <span class="hljs-variable">$attribute</span> ) {
       
            <span class="hljs-comment">// skip variations</span>
            <span class="hljs-keyword">if</span> ( <span class="hljs-variable">$attribute</span>-><span class="hljs-title function_ invoke__">get_variation</span>() ) {
                 <span class="hljs-keyword">continue</span>;
            }
       
            <span class="hljs-variable">$name</span> = <span class="hljs-variable">$attribute</span>-><span class="hljs-title function_ invoke__">get_name</span>();          
            <span class="hljs-keyword">if</span> ( <span class="hljs-variable">$attribute</span>-><span class="hljs-title function_ invoke__">is_taxonomy</span>() ) {
     
                <span class="hljs-variable">$product_id</span> = <span class="hljs-variable">$item_data</span>-><span class="hljs-title function_ invoke__">get_id</span>();
                <span class="hljs-variable">$terms</span> = <span class="hljs-title function_ invoke__">wp_get_post_terms</span>( <span class="hljs-variable">$product_id</span>, <span class="hljs-variable">$name</span>, <span class="hljs-string">'all'</span> );
                   
                <span class="hljs-comment">// get the taxonomy</span>
                <span class="hljs-variable">$tax</span> = <span class="hljs-variable">$terms</span>[<span class="hljs-number">0</span>]->taxonomy;
                   
                <span class="hljs-comment">// get the tax object</span>
                <span class="hljs-variable">$tax_object</span> = <span class="hljs-title function_ invoke__">get_taxonomy</span>(<span class="hljs-variable">$tax</span>);
                   
                <span class="hljs-comment">// get tax label</span>
                <span class="hljs-keyword">if</span> ( <span class="hljs-keyword">isset</span> ( <span class="hljs-variable">$tax_object</span>->labels->singular_name ) ) {
                    <span class="hljs-variable">$tax_label</span> = <span class="hljs-variable">$tax_object</span>->labels->singular_name;
                } <span class="hljs-keyword">elseif</span> ( <span class="hljs-keyword">isset</span>( <span class="hljs-variable">$tax_object</span>->label ) ) {
                    <span class="hljs-variable">$tax_label</span> = <span class="hljs-variable">$tax_object</span>->label;
                    <span class="hljs-comment">// Trim label prefix since WC 3.0</span>
                    <span class="hljs-variable">$label_prefix</span> = <span class="hljs-string">'Product '</span>;
                    <span class="hljs-keyword">if</span> ( <span class="hljs-number">0</span> === <span class="hljs-title function_ invoke__">strpos</span>( <span class="hljs-variable">$tax_label</span>,  <span class="hljs-variable">$label_prefix</span> ) ) {
                        <span class="hljs-variable">$tax_label</span> = <span class="hljs-title function_ invoke__">substr</span>( <span class="hljs-variable">$tax_label</span>, <span class="hljs-title function_ invoke__">strlen</span>( <span class="hljs-variable">$label_prefix</span> ) );
                    }
                }
                <span class="hljs-variable">$out</span> .= <span class="hljs-variable">$tax_label</span> . <span class="hljs-string">': '</span>;
     
                <span class="hljs-variable">$tax_terms</span> = <span class="hljs-keyword">array</span>();              
                <span class="hljs-keyword">foreach</span> ( <span class="hljs-variable">$terms</span> <span class="hljs-keyword">as</span> <span class="hljs-variable">$term</span> ) {
                    <span class="hljs-variable">$single_term</span> = <span class="hljs-title function_ invoke__">esc_html</span>( <span class="hljs-variable">$term</span>->name );
                    <span class="hljs-title function_ invoke__">array_push</span>( <span class="hljs-variable">$tax_terms</span>, <span class="hljs-variable">$single_term</span> );
                }
                <span class="hljs-variable">$out</span> .= <span class="hljs-title function_ invoke__">implode</span>(<span class="hljs-string">', '</span>, <span class="hljs-variable">$tax_terms</span>);
                  
                <span class="hljs-keyword">if</span> ( <span class="hljs-variable">$count</span> > <span class="hljs-number">1</span> && ( <span class="hljs-variable">$i</span> < (<span class="hljs-variable">$count</span> - <span class="hljs-number">1</span>) ) ) {
                    <span class="hljs-variable">$out</span> .= <span class="hljs-string">', '</span>;
                }
              
                <span class="hljs-variable">$i</span>++;
                <span class="hljs-comment">// end for taxonomies</span>
          
            } <span class="hljs-keyword">else</span> {
      
                <span class="hljs-comment">// not a taxonomy</span>
                  
                <span class="hljs-variable">$out</span> .= <span class="hljs-variable">$name</span> . <span class="hljs-string">': '</span>;
                <span class="hljs-variable">$out</span> .= <span class="hljs-title function_ invoke__">esc_html</span>( <span class="hljs-title function_ invoke__">implode</span>( <span class="hljs-string">', '</span>, <span class="hljs-variable">$attribute</span>-><span class="hljs-title function_ invoke__">get_options</span>() ) );
              
                <span class="hljs-keyword">if</span> ( <span class="hljs-variable">$count</span> > <span class="hljs-number">1</span> && ( <span class="hljs-variable">$i</span> < (<span class="hljs-variable">$count</span> - <span class="hljs-number">1</span>) ) ) {
                    <span class="hljs-variable">$out</span> .= <span class="hljs-string">', '</span>;
                }
              
                <span class="hljs-variable">$i</span>++;
                  
            }
        }
        <span class="hljs-keyword">echo</span> <span class="hljs-variable">$out</span>;
    }
    <span class="hljs-title function_ invoke__">add_filter</span>( <span class="hljs-string">'woocommerce_cart_item_name'</span>, isa_woo_cart_attribute_values, <span class="hljs-number">10</span>, <span class="hljs-number">2</span> );
    #2440580
    Debrailly
    Participant
    Padawan WordPress
    58 contributions

    Finalement, c’est le code ci-dessous qui fonctionne :

    Néanmoins, j’aimerais ne pas afficher le nom de l’attribut mais seulement sa valeur. Quelqu’un peut-il m’aider ? Je n’ose pas trop toucher au code :

    /**
    * WooCommerce: show all product attributes, separated by comma, on cart page
    */
    function isa_woo_cart_attribute_values( $cart_item, $cart_item_key ) {

    $item_data = $cart_item_key[‘data’];
    $attributes = $item_data->get_attributes();

    if ( ! $attributes ) {
    return $cart_item;
    }

    $out = $cart_item . ‘<br />’;

    $count = count( $attributes );

    $i = 0;
    foreach ( $attributes as $attribute ) {

    // skip variations
    if ( $attribute->get_variation() ) {
    continue;
    }

    $name = $attribute->get_name();
    if ( $attribute->is_taxonomy() ) {

    $product_id = $item_data->get_id();
    $terms = wp_get_post_terms( $product_id, $name, ‘all’ );

    // get the taxonomy
    $tax = $terms[0]->taxonomy;

    // get the tax object
    $tax_object = get_taxonomy($tax);

    // get tax label
    if ( isset ( $tax_object->labels->singular_name ) ) {
    $tax_label = $tax_object->labels->singular_name;
    } elseif ( isset( $tax_object->label ) ) {
    $tax_label = $tax_object->label;
    // Trim label prefix since WC 3.0
    $label_prefix = ‘Product ‘;
    if ( 0 === strpos( $tax_label, $label_prefix ) ) {
    $tax_label = substr( $tax_label, strlen( $label_prefix ) );
    }
    }
    $out .= $tax_label . ‘: ‘;

    $tax_terms = array();
    foreach ( $terms as $term ) {
    $single_term = esc_html( $term->name );
    array_push( $tax_terms, $single_term );
    }
    $out .= implode(‘, ‘, $tax_terms);

    if ( $count > 1 && ( $i < ($count – 1) ) ) {
    $out .= ‘, ‘;
    }

    $i++;
    // end for taxonomies

    } else {

    // not a taxonomy

    $out .= $name . ‘: ‘;
    $out .= esc_html( implode( ‘, ‘, $attribute->get_options() ) );

    if ( $count > 1 && ( $i < ($count – 1) ) ) {
    $out .= ‘, ‘;
    }

    $i++;

    }
    }
    echo $out;
    }
    add_filter( ‘woocommerce_cart_item_name’, isa_woo_cart_attribute_values, 10, 2 );

    #2440600
    Joss47
    Modérateur
    Maître WordPress
    5008 contributions
    #2442752
    Debrailly
    Participant
    Padawan WordPress
    58 contributions

    Merci pour le lien,

    Le code que j’ai mis plus haut fonctionne. Mais j’aimerais afficher seulement la valeur et non le nom de l’attribut.

    Une idée ?

    #2442976
    Joss47
    Modérateur
    Maître WordPress
    5008 contributions

    Test et ajoute cela dans ton functions.php à la place de tous les autres codes et dis moi si c’est ok :

    add_filter( 'woocommerce_cart_item_name', 'wc_cart_item_attributes', 10, 3 );
    function wc_cart_item_attributes( $product_name, $cart_item, $cart_item_key ) {
    $product = $cart_item['data'];
    $attributes = $product->get_attributes();
    $attribute_output = '';
    foreach ( $attributes as $attribute ) {
    $attribute_values = $attribute->get_options();
    foreach ( $attribute_values as $attribute_value ) {
    $attribute_output .= $attribute_value . '<br>';
    }
    }
    return $product_name . '<br>' . $attribute_output;
    }

6 sujets de 1 à 6 (sur un total de 6)
  • Vous devez être connecté pour répondre à ce sujet.