Agustin
Agustin

Reputation: 11

Adding an id to the output of some php

i have a small code,

i need to add a class to the output

$output_html .= ' ' . groovy_menu_woocommerce_mini_cart_counter( $qty ) . ' ';

which output a

<span class="gm-cart-counter">2</span>

how to add an id to the output of

' . groovy_menu_woocommerce_mini_cart_counter( $qty ) . '

so it look like this

<span id="newid"class="gm-cart-counter">2</span>

new code tested

$span = groovy_menu_woocommerce_mini_cart_counter($qty);
$spanWithId = "<span id='the-id'" ;

that work and show a span with the-id id

but this doesnt

$span = groovy_menu_woocommerce_mini_cart_counter($qty);
$spanWithId = "<span id='the-id'"  . $span;

Upvotes: 1

Views: 80

Answers (2)

I wrestled a bear once.
I wrestled a bear once.

Reputation: 23379

Here's what I would do.

// create a wrapper function that injects the id
function menu_with_id($qty, $id){
  $span = groovy_menu_woocommerce_mini_cart_counter($qty);
  return str_replace('<span', "<span id='$id'", $span);
}

// then replace it
$output_html .= ' ' . menu_with_id($qty, $id) . ' ';

Upvotes: 1

dazed-and-confused
dazed-and-confused

Reputation: 1333

This is ugly but it should do the trick:

$span = groovy_menu_woocommerce_mini_cart_counter($qty);
$spanWithId = "<span id='the-id'" . substr($span, 5, strlen($span));
$output_html .= $spanWithId;

echo "$spanWithId";

Output:

<span id='the-id' class="gm-cart-counter">2</span>

Try this

    global $woocommerce;

    $qty = 0;
    if ($tks == true) {
        $qty = $woocommerce->cart->get_cart_contents_count();
    }

    $cartIcon = 'fa fa-shopping-cart';

    $span = groovy_menu_woocommerce_mini_cart_counter($qty);
    $spanWithId = "<span id='the-id'" . substr($span, 5, strlen($span));

    $output_html .= '
                <div class="gm-minicart minicartmarie">
                    <a href="' . get_permalink( wc_get_page_id( 'cart' ) ) . '" class="gm-minicart-link minicartmarie">
                        <div class="gm-badge">
                            <i class="gm-icon ' . esc_attr( $cartIcon ) . '"></i>
                            ' . $spanWithId . '
                        </div>
                    </a>
                </div>
                ';

You correctly replaced the call to groovy_menu_woocommerce_mini_cart_counter($qty) but you included a ; in the statement which breaks it.

Also, instead of echoing $spanWithId you should echo $output_html

Upvotes: 1

Related Questions