0

My php is very limited. I edit existing things to try and make it work and can usually understand more or less what code is doing but I can't write code myself.

I'll try to explain my situation.

I have a Woocommerce plugin that shows products as a list (in a table) instead of separate boxes. This table has a cell for the "name" of the product. I want to have the content of a taxonomy as a second line under each name on each row.

So let's say it is now (3 columns/row):

Row 1 : Name 1 | Rating | Add To Cart Button Row 2 : Name 2 | Rating | Add To Cart Button

I want it to be:

Row1: Name 1 | Rating | Add To Cart Button Taxonomy Country | |

Row2 : Name 2 | Rating | Add To Cart Button Taxonomy Country | |

I did get this working years ago with a custom field using this line (in another plugin, Province being the custom field): . get_post_meta( get_the_ID(), 'Province', true ) .

Now, with the new plugin that does the same thing but is a lot better I want to do the same but this time with a Taxonomy instead of a Custom Field. (This allows me to set up filters based on taxonomy which I cannot do with the custom field)

I found the php file and place where it searches for and displays the content in the "name cell". Tried it out with just a plain "test" string and it showed "test" as I want it to as second line of each name cell. I then changed the code of the new plugin and added my old code (the . get_post_meta( get_the_ID(), 'Province', true ) . part) instead of the test string and again, works perfect.. shows the content of the custom field for each row as second line in the name cell...

Now I used a plugin to add a custom taxonomy, called it "country" and edited all my products to have the correct data in that new taxonomy.

All that remained was to change the line so that it reads the taxonomy instead of the custom field. And here is where I am stuck...

This is the full code for the custom field (works fine).

private function get_product_name( $product ) {
    $name = wcpt_get_name( $product ) . '</br><i>' . get_post_meta( get_the_ID(), 'Province', true ) . '</i></td>';
    if ( array_intersect( array( 'all', 'name' ), $this->args->links ) ) {
        $name = WCPT_Util::format_product_link( $product, $name );
    }
    return apply_filters( 'wc_product_table_data_name', $name, $product );
}

How do I get the value from the taxonomy in there instead of the value from the custom field?

Thank you!

0

The short answer is to get your taxonomy terms using:

wp_get_post_terms( $post->ID, 'yourTaxonomyName' );

This will return an array of WP Term Objects. Here's how you would get the first term name:

$terms = wp_get_post_terms( $post->ID, 'yourTaxonomyName' );
$first_term_name = $terms[0]->name;

You can learn more about this method in the WordPress Codex: https://codex.wordpress.org/Function_Reference/wp_get_post_terms

Hope this helps :)

Updated:

private function get_product_name( $product ) {
    // Get all the country terms associated with $product.
    $terms = wp_get_post_terms($product->ID, 'country');

    // Set the empty $country string.
    $country = '';

    // Check if there are country terms available.
    if($terms){

        // If there are terms then use the first one to populate the $country variable. 
        $country = '</br><i>' . $terms[0]->name . '</i>';
    }
    // Add $country to the $name variable.
    $name = wcpt_get_name( $product ) . $country . '</td>';

    // This part could rewrite $name which would remove $country.
    if ( array_intersect( [ 'all', 'name' ], $this->args->links ) ) {
        $name = WCPT_Util::format_product_link( $product, $name );
    }

    return apply_filters( 'wc_product_table_data_name', $name, $product );
}
  • How do I add this to that code? because it has to be between those two "periods" to be displayed in the right place.. I mean this part: $name = wcpt_get_name( $product ) . '</br><i>' . get_post_meta( get_the_ID(), 'Province', true ) . '</i></td>'; I don't see how i can add two lines to that " . get post meta .... true) . " part. – Planetdune Apr 4 '18 at 11:04
  • I think you can easily insert the meta data by adding to your string like so: – dawoodman71 Apr 4 '18 at 22:46
  • try this: $terms = wp_get_post_terms( get_the_ID(), 'country' ); $name = wcpt_get_name( $product ) . '</br><i>' .$terms[0]. '</i></td>'; – dawoodman71 Apr 4 '18 at 22:53
  • Hello, here is the code I tried and the error I am getting: private function get_product_name( $product ) { $terms = wp_get_post_terms( get_the_ID(), 'country' ); $name = wcpt_get_name( $product ) . '</br><i>' .$terms[0]. '</i></td>'; if ( array_intersect( array( 'all', 'name' ), $this->args->links ) ) { $name = WCPT_Util::format_product_link( $product, $name ); } return apply_filters( 'wc_product_table_data_name', $name, $product ); } ERROR: Parse error: syntax error, unexpected '$terms' (T_VARIABLE), expecting function (T_FUNCTION) – Planetdune Apr 5 '18 at 15:51
  • First, the wp_get_post_terms returns an array of Term Objects. So if you want to print the name you use $terms[0]->name. Secondly, if there are no terms you will get an empty array so you probably should make sure that $terms is not empty before attempting to print $terms[0]->name. – dawoodman71 Apr 5 '18 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.