Woocommerce Custom Function - Error

Hi, I'm trying to create a very basic function that will display an image on a single product page based on the tag that is selected. The reason for this is that each item may have a different quality added to it.

I've created the code below and set the proper tag but it does not echo the image. Can anyone see if there is something wrong with the function?

Here is a link to the product page for reference: http://shop.iwiathletics.com/product/mens-lifestyle-t/

Here is the code I have generated:

add_filter( 'woocommerce_single_product_summary', 'woo_driproduct', 45 );

function woo_driproduct() {
    if ( is_product_tag( 'drirelease' )) {
    echo "<div class='driproduct'><a href='http://shop.iwiathletics.com/drirelease/' target='_blank'><img src='http://shop.iwiathletics.com/wp-content/uploads/2013/08/drilogo.png...";
    }
    else {
        echo 'hey!';
    }
   }
  • Jack Kitterhing
    • Code Norris

    Hi there @creativ3group

    Have you tried something like

    '<div class="driproduct"><a href="http://shop.iwiathletics.com/drirelease/" target="_blank"><img src="http://shop.iwiathletics.com/wp-content/uploads/2013/08/drilogo.png

    Rather than the " and then the ' and why does the logo have 3 ... at the end?

    Thanks!

    Kind Regards
    Jack

  • Tyler Orr
    • Site Builder, Child of Zeus

    Changing the " ' actually breaks the function to display abnormally. If I enter the echoed image path to the else echo property it displays correctly. So it appears that it has to do with the is_product_tag not being read correctly or at all for some reason.

    Not sure what you mean about the 3?

  • Jack Kitterhing
    • Code Norris

    Hi there @creativ3group

    The you have 3 dots at the end of your img, your also missing closing tags, you aren't closing the img tag, the a href or the div, you need to close all these or the echo basically won't work if that makes sense? :slight_smile:

    Close these and it should work :slight_smile:

    Thank you!

    Kind Regards
    Jack.

  • Tyler Orr
    • Site Builder, Child of Zeus

    It cut off the code pasted above - now I see what you are talking about. I actually do have all that in place.

    Here is the actual code:

    add_filter( 'woocommerce_single_product_summary', 'woo_driproduct', 45 );
    
    function woo_driproduct() {
    	if ( is_product_tag( 'drirelease' )) {
    	echo '<div class="driproduct"><a href="http://shop.iwiathletics.com/drirelease/" target="_blank"><img src="http://shop.iwiathletics.com/wp-content/uploads/2013/08/drilogo.png"></a></div>';
    	}
    	else {
    		echo 'Test';
    	}
    
    }
  • Jack Kitterhing
    • Code Norris

    Hi there @creativ3group

    Sorry I wasn't over clear there, I see you've got a else which should echo test.

    I'm not seeing that echoed either are you? I'm going offline now for the night but I'll be back tomorrow :slight_smile:

    Also going to include a colleague @Alexander Rohmann here :slight_smile:

    Kind Regards
    Jack.

  • Jack Kitterhing
    • Code Norris

    Hi there @creativ3group

    I hope you are well today, I've been looking into this and it appears the

    woocommerce_single_product_summary

    Is a action hook and not a filter, so it should be add_action and not add_filter.

    Here's the relevant parts of Woo Commerce documentation.http://docs.woothemes.com/document/hooks/

    http://docs.woothemes.com/document/introduction-to-hooks-actions-and-filters/

    And the codex of WordPress http://codex.wordpress.org/Plugin_API#Actions :slight_smile:

    Thank you!

    Kind Regards
    Jack.

  • Tyler Orr
    • Site Builder, Child of Zeus

    Trying that still had no luck. I've gotten it to receive an error with an updated function per their support, but, it's an error.

    Here's what they said about the reference: "is_product_tag() is checking if you are looking at a tag archive page, it is not checking if a product has a tag"

    Following their lead, I updated the function and received these 2 errors:

    Notice: Undefined variable: post in /home/iwiathle/public_html/shop/wp-content/themes/iwi-shop/functions.php on line 55

    Notice: Trying to get property of non-object in /home/iwiathle/public_html/shop/wp-content/themes/iwi-shop/functions.php on line 55

    Here is the updated code:

    function woo_driproduct() {
    	global $woocommerce;
    	if (is_object_in_term( $post->ID, 'product_tag', 'drirelease' )) {
    	echo '<div class="driproduct"><a href="http://shop.iwiathletics.com/drirelease/"><img src="http://shop.iwiathletics.com/wp-content/uploads/2013/08/drilogo.png"></a></div>';
    	}
    	else {
    		echo 'Test';
    	}
    
    }

    I'm assuming I'm moving in the right direction but am still fiddling with it.

  • Alexander
    • DEV MAN’s Mascot

    Hi @creativ3group,

    Taking a look at the most recent code sample, you're getting close. I actually think they were wrong to tell you that you need to globalize those variables.

    In all fairness, that would fix the "not defined" error, but it's not what you want. First I'll explain the error, to hopefully give some insight. When a function is called, it's isolated from the rest of the code. It only knows about variables that exist within that function. This is a programming principle called scope. $post does not exist in the local scope of that function.

    Take a look at this article for more reference: http://php.net/manual/en/language.variables.scope.php

    For example:

    $name="Alex";
    function display_name(){
        echo $name;
    }
    display_name();

    If you run that code, you will get an error telling you that $name is undefined. That's because the function doesn't know anything about it. We can fix this by referencing $name which was declared globally:

    $name="Alex";
    function display_name(){
        global $name;
        echo $name;
    }
    display_name();

    This code will work. So you can globalize the other variables by doing this in your function:

    global $woocommerce, $post, $product;

    All that to say, we're actually dealing with yet another issue here.

    Just looking at these first two lines, we can tell that something is off:

    add_filter( 'woocommerce_single_product_summary', 'woo_driproduct', 45 );
    
    function woo_driproduct() {

    You're using a filter, but you're not passing any data to he function to be filtered. A filter shouldn't echo anything either, but rather return a modified version.

    function woo_driproduct($content) {
        $mychanges = "test";
        return $content . $mychanges;
    }

    Hopefully this helps give some insight into what's going on here!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.