-1

I've been trying to echo an image in the following code, but even the "echo" gets printed.

This is the code:

 public function add_to_cart_text() {   $text = $this->is_purchasable()
 && $this->is_in_stock() ? __( 'IMAGE HERE', 'woocommerce' ) : __( 'Read
 more', 'woocommerce' );

    return apply_filters( 'woocommerce_product_add_to_cart_text', $text,
 $this ); }

I need to show an image where the "IMAGE HERE" text is, echo looks like it is not working, at least the way im trying to use it.

Is it possible to be done?

Jhecht
  • 4,407
  • 1
  • 26
  • 44

1 Answers1

0

You will want to echo an HTML image tag

echo("<img src='" . $image_soruce . "'/>");

So in the case above it looks like you just want the string:

"<img src='" . $image_soruce . "'/>"

Where $image_source is the URL of an image.

Update

If you want to use a static string that won't change, then you can just use the full string like this below:

"<img src='/link_to_you_image.jpg' />"

or

"<img src='https://somewebsite.com/link_to_you_image.jpg' />"
Asleepace
  • 3,466
  • 2
  • 23
  • 36
  • Thanks so much for your answer. I've added the code and looks like this: public function add_to_cart_text() { $text = $this->is_purchasable() && $this->is_in_stock() ? __( '""', 'woocommerce' ) : __( 'Read more', 'woocommerce' ); return apply_filters( 'woocommerce_product_add_to_cart_text', $text, $this ); } But this is whole code is being echoed: "" Did I mess up? – Patrick Ben Jul 25 '18 at 19:45
  • @GuillermoFernándezRuiz take a look at the updates I just posted, you won't be able to post placehold.it/20x20 it either needs to be a string or in a variable as a string. Also what does the __("") function do? – Asleepace Jul 25 '18 at 19:50
  • Thanks again. I changed to the following: && $this->is_in_stock() ? __( '""', 'woocommerce' ) : __( 'Read more', 'woocommerce' ); but stills echo the whole img src tag. I think its all about the __( since it should be working in a common echo. Its a woocommerce function that checks for the "Add to cart" text. I want to add an icon instead and thats why im trying to put the image in. Thanks you so much again – Patrick Ben Jul 25 '18 at 19:56
  • Sorry this is PHP lol try this instead `__("Add to cart") . ""` – Asleepace Jul 25 '18 at 20:08
  • I've been checking out. I think everything that goes into that spot is just automatically echoed no matter what. For example, this code is echoing in the website the "TEXT" string, even withouth echo: && $this->is_in_stock() ? "TEST" : __( 'Read more', 'woocommerce' ); . The problem is that image is not being printed, just plain text. I dont think it can be easily fixed so i'll just look around for a different way to go. Thanks you so much @Asleepace for your help – Patrick Ben Jul 25 '18 at 20:08
  • @GuillermoFernándezRuiz ya no worries good luck! – Asleepace Jul 25 '18 at 20:09