skip to Main Content

I want to put variable with product id in quotes of html quotes.

if ( ! function_exists( 'woocommerce_template_loop_product_link_open' ) ) {
    /**
     * Insert the opening anchor tag for products in the loop.
     */
    function woocommerce_template_loop_product_link_open() {
        global $product;
        $id = $product->get_id();
        $link = apply_filters( 'woocommerce_loop_product_link', get_the_permalink(), $product );

        echo '<a href="#" class="yith-wcqv-button" data-product_id="$id">';
    }
}

$id should be in data-product_id="$id" but I don’t know how to do this

2

Answers


  1. String interpolation requires double quotes. You’re close.

    // OLD
    echo '<a href="#" class="yith-wcqv-button" data-product_id="$id">';
    
    // NEW
    echo "<a href="#" class="yith-wcqv-button" data-product_id="$id">";
    
    Login or Signup to reply.
  2. We have different ways to do that.

    <?php
    $id = 321;
    
    // Concatenation operator
    echo '<a href="#" class="yith-wcqv-button" data-product_id="' . $id . '">';
    
    // sprintf return a string formatted. It's very common on C code
    echo sprintf('<a href="#" class="yith-wcqv-button" data-product_id="%d">', $id);
    
    // heredoc notation
    echo  <<<STR
    <a href="#" class="yith-wcqv-button" data-product_id="$id">
    STR;
    
    // escaping double quotes.
    echo "<a href="#" class="yith-wcqv-button" data-product_id="$id">";
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search