-1

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

Alex
  • 47
  • 6

2 Answers2

2

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\">";
prieber
  • 554
  • 3
  • 16
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\">";
Jimmy Loyola
  • 189
  • 10