2

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?

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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]');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.