Here below, is an existing function which is called in woocommerece-integration.php file to display the add to cart button in product cards on any WordPress page. I have a wishlist plugin which gives a shortcode and I need to display that icon before the $content
:
// Add add_to_cart btn wrapper (products loop)
$adc = '<div class="card_bot">
<div class="vamtam-add-to-cart-wrap shrt">'
. $content .
'</div></div>';
return apply_filters( 'vamtam_woocommerce_loop_add_to_cart_link', $adc );
}
}
The main goal is to get the wishlist icon on left and add to cart button on right in same row later which I can handle with CSS but both the buttons should be within same wrapper class card_bot which is my main goal.
This is my shortcode: [wlfmc_add_to_wishlist]
I have tried :
echo do_shortcode("[wlfmc_add_to_wishlist]");
- php
echo do_shortcode
tag as well
Both seem to give errors and are breaking my PHP file or displaying the shortcode as a string. What is the correct way to do it?
3
Answers
From your provided code, try inserting
do_shortcode()
withoutecho
, using.
concatenation operator, like:It should work.
To display the WooCommerce wishlist icon using the [wlfmc_add_to_wishlist] shortcode before the add to cart button in the same row within the card_bot wrapper, you can use do_shortcode() inside your PHP code properly.
this should work
In order to achieve what you want I think you should use hook.
There are two types of hooks :
So in your case, you should use a filter because what you basically want is to modify how the add to cart button is displayed (aka add a wrapper and your shortcode).
I suggest you search in the woo hook reference to find the right hook for your case (maybe: woocommerce_loop_add_to_cart_link) :
https://woocommerce.github.io/code-reference/hooks/hooks.html
And to add the plugin’s shortcode in your callback method, the correct way is: