- This topic has 10 replies, 2 voices, and was last updated 7 years, 6 months ago by
Tom.
-
AuthorPosts
-
November 25, 2018 at 3:40 am #738249
wonzilla
How can I add the Short product description with Add to cart button and star rating to the Page Hero content? I want to create something similar:
Thank you!
November 25, 2018 at 9:45 am #738680Tom
Lead DeveloperLead DeveloperYou would need to use WooCommerce shortcodes: https://docs.woocommerce.com/document/woocommerce-shortcodes/
For example, for an Add to Cart button: https://docs.woocommerce.com/document/woocommerce-shortcodes/#section-15
For the product description, you’d likely need to create your own shortcode:
add_shortcode( 'wc_product_desc', function( $atts ) { $a = shortcode_atts( array( 'id' => get_the_ID(), ), $atts ); return get_the_excerpt( $a['id'] ); } );Then you’d do this:
[wc_product_desc id="10]10being the ID of the product.As for stars, I’m not sure how you’d wrap that into a shortcode. It may be worth asking WooCommerce support if they have a solution for that.
November 25, 2018 at 10:26 am #738729wonzilla
Hi Tom,
Thank you so much for your detailed answer! But how can I add shortcodes to the Header element? If I add them in the content, they are not recognized/displayed. Fortunately, I was able to add the short product description and price using custom fields but I’m still struggling with finding a solution for the add to cart button and stars rating. Any help will be much appreciated!
November 25, 2018 at 11:14 am #738801wonzilla
I guess that I need to add the php code with the Code Snippets plugin and then add the shortcode for a particular product in the Header content. However, isn’t any method to show the add to cart button/description for each product automatically without the product ID? This doesn’t seem to be the best way to do it when there are lots of products :/
November 25, 2018 at 6:46 pm #739163Tom
Lead DeveloperLead DeveloperThat’s correct.
I just adjusted the shortcode function above with something that might detect the current ID if you leave the ID attribute empty.
I just checked their function for the add to cart shortcode, and it looks like it requires the ID or SKU. Would a custom button with the add to cart URL work, or do you need the full WC add to cart functionality?
November 26, 2018 at 5:57 am #739734wonzilla
Hi Tom,
If the custom build button works just like the WooCommerce button does, I would like to implement it. Btw, the short description appears now, thank you. However, I really need to add the star rating in the page hero content. It’s the second day I’m searching all over the internet for a solution. Isn’t there any hook or code that I need to add? 🙁
November 26, 2018 at 9:38 am #740133Tom
Lead DeveloperLead DeveloperTo get the add to cart button, you could do something like this:
add_shortcode( 'custom_add_to_cart', function() { return sprintf( '<a class="button add-to-cart-button" href="https://yourwebsite.com/?add-to-cart=%s">Add to Cart</a>', get_the_ID() ); } );Just be sure to change yourwebsite.com with your URL.
Then you can do this:
[custom_add_to_cart]I’m not sure about the stars, unfortunately. You may need to ask WooCommerce support if it’s possible to output them using a shortcode.
November 26, 2018 at 2:39 pm #740577wonzilla
Hi Tom,
Thank you for the code, it works! However, if the button is clicked one more time, it redirects to the homepage instead of showing the “You cannot add another “Item” to your cart.” message. Also, when I click the actual woocommrce button, I get two error messages – see the screenshot:
https://ibb.co/Xt9LRyjIs there any fix to this?
November 26, 2018 at 7:35 pm #740858Tom
Lead DeveloperLead DeveloperYea, that’s the limitation of the simple button – it won’t have the complexity of the regular add to cart buttons.
You’ll likely need to ask WooCommerce support if a function exists for that and the product ratings so we can add them to shortcodes. I’m not able to find anything by searching.
November 26, 2018 at 9:40 pm #740981wonzilla
Got it, thank you for your answers!
November 27, 2018 at 8:52 am #741684Tom
Lead DeveloperLead DeveloperNo problem!
-
AuthorPosts
- You must be logged in to reply to this topic.