0
votes

I'm having trouble getting a taxonomy term to display as the title for my product-post snippets. This is on a Wordpress site, using the Avada Theme, with Woocommerce and the Advanced Custom Fields plug-in. The taxonomy is generated by the Custom Taxonomies plug-in, and everything works great on the rest of the site.

So far I can get everything to show up in the right place, but the best I can do is "echo" a message. I was getting the ID number of the taxonomy terms before reading further into the topic, and swapped "object ID" with "term-name" in the ACF front-end, but now I can't get the ID or the title!

Please help, PHP is not my strong suit, so I'm sure this is a newbie oversight!

add_action( 'woocommerce_shop_loop_item_title', 'artist_link' );
function artist_link() {
    $value = get_field("product_artist");
    if($value)
    {
    echo "can't return value here, but I'd like the artist product term! ";
    }
    else
    {
    echo 'No Artist Entry';
    }
}

This code snippet is from my functions.php file, as the Avada theme requires you to use the loop over a "single-XXX.php" file.

I should mention too this is a single choice from a multi select, and "product_artist" is from ACF, the Custom Taxonomy term ID is "artists".

Thanks!

*EDIT: for those who may have this problem in the future, this is what worked for me

add_action('woocommerce_shop_loop_item_title', 'artist_link');
function artist_link()
{
    // Get the WooCommerce global variable for the current product
    global $product;
    if ($product instanceof \WC_Product) {
        $value = get_field("product_artist", $product->get_id());
        if ($value) {
            echo $value->name;
        } else {
            echo 'Value falsey, do something with it here';
        }

        return;
    }

    echo 'No global product here';

}

Looks like I was trying to call a string and not an object (which is what $value is).

1

1 Answers

0
votes

Looking at the code that calls that action, there's a global variable that is brought into scope and should be set prior to the action being called that you should be able to use. If I'm reading it correctly, that variable should be an instance of WC_Product which through its parent class has a get_id() method, and I think that's what you need to pass to ACF's get_field() function as a second parameter:

add_action('woocommerce_shop_loop_item_title', 'artist_link');
function artist_link()
{
    // Get the WooCommerce global variable for the current product
    global $product;
    if ($product instanceof \WC_Product) {
        $value = get_field("product_artist", $product->get_id());
        if ($value) {
            echo $value;
        } else {
            echo 'Value falsey, do something with it here';
        }

        return;
    }

    echo 'No global product here';

}