phpwordpresswoocommerceconcatenationshortcode

How to insert a shortcode into a WooCommerce PHP file?


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 :

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?


Solution

  • From your provided code, try inserting do_shortcode() without echo, using . concatenation operator, like:

        $adc = '<div class="card_bot"> 
            <div class="vamtam-add-to-cart-wrap shrt">'
             . do_shortcode("[wlfmc_add_to_wishlist]") . $content .    
            '</div></div>';
        return apply_filters( 'vamtam_woocommerce_loop_add_to_cart_link', $adc );
    

    It should work.