Search code examples
phpwordpressstringwoocommerceshortcode

Syntax Error when using Wordpress do_shortcode in between an HTML string


I'm having a syntax error of "unexpected T_ECHO on line 7" and I checked PHP parse/syntax errors; and how to solve them thread trying to understand and solve that issue.

But I can't seem to find a way to solve this with my limited PHP knowledge. Below is the code snippet I want to have on my Woocommerce/Wordpress website. I am using an altered version of Display a custom div block for woocommerce out of stock product variations answer code which works flawlessly.

add_filter( 'woocommerce_available_variation', 'custom_outofstock_variation_addition', 10, 3 );
function custom_outofstock_variation_addition( $data, $product, $variation ) {
    if( $variation->is_on_backorder() ){
        $data['availability_html'] .= '<table class="variations mto-cont" cellspacing="0">
            <tbody>
                <tr>
                    <td class="made-to-order"><a> 'echo do_shortcode('[nm_lightbox title="Made to Order 🛈" content_image_id="5110"]');'</a></td>
                </tr>
            </tbody>
        </table>';
    }
    return $data;
}

So what I wish to reach is to have Wordpress to do a shortcode in between <td class="made-to-order"><a> and </a></td>.

How should this code be rewritten in order to make this work?


Solution

  • You need to concatenate do_shortcode() function in your string of HTML instead of using echo inside it like (as you are returning that string at the end):

    add_filter( 'woocommerce_available_variation', 'custom_outofstock_variation_addition', 10, 3 );
    function custom_outofstock_variation_addition( $data, $product, $variation ) {
        if( $variation->is_on_backorder() ){
            $data['availability_html'] .= '<table class="variations mto-cont" cellspacing="0">
                <tbody>
                    <tr>
                        <td class="made-to-order"><a> ' . do_shortcode('[nm_lightbox title="Made to Order …" content_image_id="5110"]') . '</a></td>
                    </tr>
                </tbody>
            </table>';
        }
        return $data;
    }