If you use the free Elementor or another WordPress page builder, you’ve probably noticed that there’s no easy way to add an extra “Add to Cart” button for the product you’re currently viewing. That’s where a small piece of code comes in handy.
With just a few lines of code, you can create a customizable shortcode that will place the button anywhere in Elementor or any other editor. You can then display the button anywhere by using the shortcode:
[add_to_cart_here]
Add this code to the functions.php file of your child theme:
// Custom Add to Cart button
function add_to_cart_current_product() {
global $product;
if ( $product instanceof WC_Product ) {
return do_shortcode('[add_to_cart id="' . $product->get_id() . '"]');
}
return '';
}
add_shortcode('add_to_cart_here', 'add_to_cart_current_product');
Leave a Reply