• Resolved diegogomez

    (@diegogomez)


    Hello, I hope someone can help me.

    I made it possible to show the list of attributes in the short description through a snipet:

    add_action( 'woocommerce_single_product_summary', 'add_atts_to_summary' );
    
    function add_atts_to_summary() {
    global $product;
    wc_display_product_attributes( $product );
    }

    But now what I would like is to be able to show only a specific attribute, not all, that I can indicate through the code.

    how could i get it?

Viewing 13 replies - 1 through 13 (of 13 total)
  • Hi @diegogomez

    Thanks for reaching out!

    I understand that you want to show a specific attribute in the product short description, correct?

    This is a bit of a complicated topic that would need some customization to address. Unfortunately, custom coding is not something we can assist with directly. However, I’ll keep this thread open for a bit to see if anyone from the community can lend a hand.

    If you have any other questions related to development or custom coding, don’t hesitate to reach out to some of the great resources we have available for support. The WooCommerce community is filled with talented open-source developers, and many of them are active on the channels listed below:

    Hope this helps!

    Thread Starter diegogomez

    (@diegogomez)

    Hello, thanks for answering.

    I already did it, actually it wasn’t that complicated, I just had to change a line of code in the snippet that I found researching in other portals.

    add_action( 'woocommerce_single_product_summary', 'add_atts_to_summary' );
    
    function add_atts_to_summary() {
    	global $product;
    	echo implode (', ', wc_get_product_terms ( $product->id, 'pa_precio-venta', array( 'fields' => 'names' ) )); 
    }

    I’m not a code expert, so, after several tests I found this solution, now it works as I need.

    Thank you anyway.

    Saif

    (@babylon1999)

    Hello @diegogomez,

    Glad to hear you’ve figured it out! ??

    I will mark the thread as solved. If you have any other questions, please feel free to open a new thread.

    Cheers!

    @diegogomez — Hi there , I’m in the same predictament…

    Just need to check — if I want to show the Attribute (Warranty).. then the coding in that instance should change to pa_warranty ? Thanks.

    add_action( 'woocommerce_single_product_summary', 'add_atts_to_summary' );
    
    function add_atts_to_summary() {
    	global $product;
    	echo implode (', ', wc_get_product_terms ( $product->id, 'pa_warranty', array( 'fields' => 'names' ) )); 
    }
    
    Thread Starter diegogomez

    (@diegogomez)

    Hi @allstarsft.

    It could work, but you must consider that the name of the variable is equivalent to the “slug” that you have for that attribute that you want to invoke, in that case you must change:

    pa_”slug-atribute”

    If the slug of your variation is called “warranty” then it is correct: pa_warranty

    Remember that the slugs of your variations can be seen in the menu list Products > Attributes.

    I hope this works for you

    Thanks. I will have to try that again as a code snippet then if it works then use Oceanwp’s Ocean Hooks to do it.

    Will report back if it works.(earlier I just tried on Ocean Hooks and it did not work)

    Ok, here goes..

    I tried using Code Snippet way…. (as I’m using Oceanwp – -and decided to use Before Excerpt, had to use Ocean’s hook instead of woocommerce_single_product_summary)

    But end up : — Critical error for the site… and whole page gone bonkers.

    And is there anyway to Show “Warranty” Attribute term before the “3 Years with Local Distributor — ”

    • This reply was modified 1 year, 7 months ago by allstarsft.

    Hi Guys and to @diegogomez :

    I managed to find another coding that was easier for me to understand.. (as I need to add “Warranty : ” — Attribute name — not sure how to do that with the earlier “Implode” code. haha)

    add_action( 'ocean_before_single_product_excerpt', 'fdev_display_attribute_warranty', 10 );
    function fdev_display_attribute_warranty() {
        global $product;
           
      $taxonomy = 'pa_warranty';
      echo '<div class="wc-attribute-warranty" style="margin-top: 20px;"><span style="font-size:16px; font-weight:600;">' . __( 'Warranty : ', 'woocommerce' ) ;
      echo '<p class="attribute-warranty">' . $product->get_attribute($taxonomy) . '</p> 
    	</span>
            </div>';
           		
    }

    And just as a reminder to myself, if using Ocean Hook… these lines have to be removed (as Ocean Hook function has to select the Hook in the OceanWP — My Library Panel — Template creation) :

    add_action( ‘ocean_before_single_product_excerpt’, ‘fdev_display_attribute_warranty’, 10 );
    function fdev_display_attribute_warranty() {

    So the code ends up :

    global $product;
    
          
            $taxonomy = 'pa_warranty';
            echo '<div class="wc-attribute-warranty" style="margin-top: 20px;"><span style="font-size:16px; font-weight:600;">' . __( 'Warranty : ', 'woocommerce' ) ;
            echo '<p class="attribute-warranty">' . $product->get_attribute($taxonomy) . '</p>
            </span>
            </div>';

    Tested and it works for me!!!! Yeeee haaaaaa!!

    All is good till I go to Products that have not converted the Warranty information to Attributes yet.

    Anyone has any idea how to avoid this ? I do not want the Warranty label to be listed before I convert the Warranty information over to Attributes.

    Thread Starter diegogomez

    (@diegogomez)

    Hello @allstarsft , I see that you managed to find a code that works for you, I had tried that before and it did not work for what I needed, but I am glad that it is good for you.

    I don’t know if I understood what you need in your last message; Don’t want the word “Warranty:” to appear as a title?

    • This reply was modified 1 year, 7 months ago by diegogomez.

    @diegogomez

    As i managed to add in the Attribute name (label) which is the word Warranty.

    But since I’m progressively doing the conversion from Excerpt to Attribute for all warranty data…. (So far I have only done for 1 brand – Tp-link)..

    The other brands’ products should not have anything shown for the attribute part.

    It should be totally blank for other brands.

    For example in image above.. AudioEngine brand product.

    Warranty data is still keyed in Excerpt not yet convert to Attribute..but the bold text (label) “Warranty :” showed….

    So pending my manual switching the Warranty data over from Excerpt to Attribute… Such coding should not give me any text at all….

    Just looks wierd with the bold text label there… and below Excerpt showed warranty data … Arrrgh…

    • This reply was modified 1 year, 7 months ago by allstarsft.
    • This reply was modified 1 year, 7 months ago by allstarsft.
    • This reply was modified 1 year, 7 months ago by allstarsft.
    Thread Starter diegogomez

    (@diegogomez)

    Oh Ok, now I remember that maybe that’s why the code you use didn’t work for me, and I opted to insert the title directly as html code as follows:

    add_action( 'woocommerce_single_product_summary', 'add_atts_to_summary' );
    
    function add_atts_to_summary() {
    	global $product;
        echo ('<p class="title-warranty">Warranty: $');
    	echo implode (', ', wc_get_product_terms ( $product->id, 'pa_warranty', array( 'fields' => 'names' ) )); 
    	echo ('</p>');
    }

    I don’t know if I did it right, but it worked for me and it doesn’t throw me an error, not even in the log.

    So I managed to make the title appear only in the products that already have that particular attribute.

    I hope it helps you.

    @diegogomez Thanks for your help.

    I tried your coding but still ends up : (So it didn’t hide or remove the Warranty label when no Attribute is defined yet)

    And I have trouble trying to add css or a different class to the 2nd line of coding with “implode”. LOL

    Your tried code : (I changed P class to Div class)

    add_action( 'ocean_before_single_product_excerpt', 'add_atts_to_summary' );
    
    function add_atts_to_summary() {
    global $product;
    echo ('<div class="wc-attribute-warranty" style="margin-top: 20px;"><span style="font-size:16px; font-weight:600;"> Warranty: ');
    echo implode (', ', wc_get_product_terms ( $product->id, 'pa_warranty', array( 'fields' => 'names' ) ));
    	echo ('</div>');

    To share this new coding i got from Woocommerce Community in FB. In fact just by adding this code before my 2 lines — it works!

    if ( ! $product->get_attribute($taxonomy) ) {return;}

    Full code below :

    add_action( 'ocean_before_single_product_excerpt', 'fdev_display_attribute_warranty', 10 );
    function fdev_display_attribute_warranty() {
    
    	global $product;
       
            $taxonomy = 'pa_warranty'; 
    		
    		if ( ! $product->get_attribute($taxonomy) ) { 
    			return;
    		}
    
            echo '<div class="wc-attribute-warranty" style="margin-top: 20px;"><span style="font-size:16px; font-weight:600;">' . __( 'Warranty : ', 'woocommerce' ) ;
    		echo '<p class="attribute-warranty">' . $product->get_attribute($taxonomy) . '</p> </span> </div>' ;  
    		
    }
    ?>
    • This reply was modified 1 year, 7 months ago by allstarsft.
    • This reply was modified 1 year, 7 months ago by allstarsft.
    • This reply was modified 1 year, 7 months ago by allstarsft.
    • This reply was modified 1 year, 7 months ago by allstarsft.
Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Show a single specific attribute in the product short description’ is closed to new replies.