Archived topic
Output Woo function within shortcode
11 replies · Started by David on September 18, 2017
Hi Tom,
following on from the other thread. I would really like to convert this script to calling the woo function, in this instance for the Woo add to cart button.
function diggs_cart_button () {
global $post , $product;
$product_id = $product->get_id();
$product = wc_get_product( $product_id );
ob_start();
echo "<a href='".$product->add_to_cart_url()."'>add to cart</a>";
return ob_get_clean();
}
add_shortcode ('diggs-cart-button', 'diggs_cart_button' );
I'm not that clued up on this and unsure about output buffering issues etc so any advice on whether it's ok to use the above or if you can provide a better solution such as calling the action/filter inside the shortcode.
thanks
David
The actual code looks ok.
I checked and couldn't find a button related function from WC, but you can give the <a> element a class of button in order to turn it into a button.
Unless of course you're missing specific functionality from the default WC buttons?
Hi Tom i appreciate you taking the time.
I would like the button to include the qty field.
I have codes for the title, short desc, long desc and price using the same method but they are all stripped of any style and particularly a problem all line breaks.
Should I be concerned having this many echoes on one page? They seem to work ok
All of them are being used in GP headers. As an alternative is their a way of hooking the woo actions inside this?
Hard finding any official documentation on this from Woo.
I did find this - perhaps it will help?: https://nicola.blog/2015/05/04/add-the-quantity-field-to-the-add_to_cart-shortcode/
Hi Tom, thanks again for investigating.
EDIT: Yep it's possible just add the action to a GP Hook. Now is there a way of hooking this inside the header?
The other issue i have is the shortcodes that pull in the description:
function diggs_full_desc () {
global $post , $product;
$product_id = $product->get_id();
$product = wc_get_product( $product_id );
ob_start();
echo '<div class="diggs_descrip">' . $product->get_description() . '</div>';
return ob_get_clean();
}
add_shortcode ('diggs-full_desc', 'diggs_full_desc' );
This returns the correct output but without any of the line breaks. Any ideas?
thanks again
David
Hi Tom, I discovered the inside merged header hook, so nice and simple to hook the add to cart inside the header area.
add_action( 'wp', 'tu_move_single_wc_addtocart_button' );
function tu_move_single_wc_addtocart_button() {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'generate_inside_merged_page_header', 'woocommerce_template_single_add_to_cart', 30 );
}
Apart from the above text styling issues shop page headers are now complete. Can't wait to share.
Awesome! Sorry for not being more helpful here - on a small vacation. Which text styling issue are you having?
Hi Tom, no problem - a well deserved break hey!
The text when pulled in via the above shortcode is stripped of all line breaks whereas in the actual woo description it is fine. The only fix i have so far would be to edit way too many product descriptions with <br> tags which wouldn't be fun.
Any ideas?
Can you show me an example of some stripped text?
In your code, try replacing:
$product->get_description()
With:
wpautop( $product->get_description() )
Tom, thank you again. This worked perfectly. David
You're welcome :)