skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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.

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

    this should work

    Login or Signup to reply.
  3. In order to achieve what you want I think you should use hook.

    There are two types of hooks :

    • action: mainly used to add custom traitment(s) during a certain event
    • filter: mainly used to modify elements

    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:

    echo do_shortcode('[wlfmc_add_to_wishlist]');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search