- This topic has 28 replies, 3 voices, and was last updated 7 years, 3 months ago by
David.
-
AuthorPosts
-
November 26, 2018 at 2:23 pm #740553
wonzilla
Hi there!
I have an eCommerce site that sells digital products and I need to display a demo button for each product on the shop and archive/category pages (beside the add to cart button or somewhere in that area). I have no idea how to do it since some of the links are to external sites (such as Etsy). Any solutions? Thank you!
November 26, 2018 at 7:33 pm #740857Tom
Lead DeveloperLead DeveloperHi there,
You should be able to use a hook for that:
woocommerce_after_add_to_cart_buttonWhere would the URL come from for the button?
November 26, 2018 at 9:33 pm #740980wonzilla
The URL will be inside the single product page – more exactly in the Header element where I will include the short description with the Add to cart button. If you could provide a code snippet to display the live demo link for each product on the shop/product archive page, it would be super awesome. Thank you so much for all of your help!
November 27, 2018 at 9:14 am #741722Tom
Lead DeveloperLead DeveloperSo that URL would likely need to come from a custom field.
Then you could try this:
add_action( 'woocommerce_after_add_to_cart_button', function() { $url = get_post_meta( get_the_ID(), 'your_custom_field_key', true ); if ( $url ) : ?> <a class="button" href="<?php echo $url; ?>">Your button text</a> <?php endif; } );November 27, 2018 at 10:20 am #741793wonzilla
Hi Tom,
Unfortunately, this didn’t work. I created a custom field with the name product_demo_link, then added the URL in the Value field and updated the Header element with the {{custom_field.product_demo_link}} tag. Also, I replaced your_custom_field_key with product_demo_link in the code above but it shows nothing. Have I done something wrong?
Update: now I triple checked and it seems that your code adds the button on the single product page rather than on shop or archive product pages. How can I show it on the shop pages where all the products are listed?
November 27, 2018 at 11:47 am #741917wonzilla
I added it using woocommerce_after_shop_loop_item_title hook. Yay! Thank you so much, you are awesome!
November 27, 2018 at 12:25 pm #741964wonzilla
One more question: is there a way to echo the short product summary with the stars rating and add to cart button in the hero content? At the moment I’m hiding the product summary (and use custom fields instead) and I’m afraid that it will affect the SEO of my site.
November 27, 2018 at 4:19 pm #742147Tom
Lead DeveloperLead DeveloperI helped with some of that here: https://generatepress.com/forums/topic/i-want-to-add-product-details-to-the-header-element/
I’m not sure about the star ratings/add to cart button currently, but hopefully WooCommerce support can help with some functions we can use.
November 27, 2018 at 5:12 pm #742181wonzilla
Ah, too bad that the short description cannot be replicated, this could have saved a lot of time. Thank you anyways 🙂
November 28, 2018 at 8:56 am #742928Tom
Lead DeveloperLead DeveloperNo problem 🙂
November 29, 2018 at 9:02 am #743947wonzilla
Hello,
I’ve managed to style the product summary the way I wanted and removed the Header element but now the demo button doesn’t appear anymore (since there are no custom fields). How can I show the demo button inside the short product description on the shop/category pages? Sorry for bugging you again 🙁
Edit: Nevermind, I added the value for the custom field again and now it works.
February 27, 2019 at 12:41 am #822951wonzilla
Hi there.
I added this code because I need to display the buttons inside the inside-wc-product-image container:
add_action( 'woocommerce_before_shop_loop_item_title', function() { $urlr = get_post_meta( get_the_ID(), 'product_demo_url', true ); if ($urlr) : ?> <span><a class="button demo-btn" href="<?php echo $urlr; ?>" target="_blank" rel="nofollow">View Demo</a></span> <?php endif; } );And I noticed that the html code is not properly formatted – see the arrows: https://ibb.co/TLc3Yrw
The product titles also don’t display as links… just plain text. Can somebody help me solve this issue? Thank you.
February 27, 2019 at 9:06 am #823582Tom
Lead DeveloperLead DeveloperHi there,
That HTML seems fine. Does the formatting issue go away if you disable that function?
February 27, 2019 at 10:55 am #823674wonzilla
Yes, Tom. If I disable the function, the hyperlink wraps the product title as it should. If I activate it back, it creates three HTML codes with links containing no text or code that makes sense or has any purpose. It doesn’t look right to me at all.
February 27, 2019 at 5:26 pm #823922Tom
Lead DeveloperLead DeveloperAnd if you remove the
<span>element?I’m not seeing any reason for that to happen.
-
AuthorPosts
- You must be logged in to reply to this topic.