• Resolved urbexlies

    (@urbexlies)


    Hello

    I got a Table list plugin for WooCommerce products. I want the content of a taxonomy (called “country”) to be displayed on the second row of every “product name cell”

    I have a VERY hard time with this… this is my code so far.

    private function get_product_name( $product ) {
        <strong>// 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.</strong>
        $name = wcpt_get_name( $product ) <strong>. $country . '</td>'</strong>;
    
        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 );
    }

    Now this code is from the Table view plugin PHP file where it populates the tables. I am trying to add the content of the taxonomy to this. I get no errors but it is not populating the “country” variable.

    This is the original non edited function straight from the plugin:

    private function get_product_name( $product ) {
          $name = wcpt_get_name( $product );
          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 );
       }
    

    What am I doing wrong ?

    • This topic was modified 6 years, 11 months ago by urbexlies.
    • This topic was modified 6 years, 11 months ago by urbexlies.
Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Hard for me to say, and this is technically outside the scope of what our plugin covers, but I’ll offer some guidance anyway.

    First up you shouldn’t edit plugin code unless you know what you’re doing or have absolutely no other choice. The filter at the end of the original function is the intended way to edit the returned value, and would be best to look into in the future.

    Regarding the actual attempted changes at hand. I’d start from the top and make sure you’re getting data at each step. Does $terms receive data and is it what you expect it to be? What is in $country set to after that first if statement where you attempt to assign data to it? What is the info in the $name variable after you assign it?

    Not sure if those <strong> tags are on purpose or accidental, but if intentional, they should be throwing PHP errors as is.

    Thread Starter urbexlies

    (@urbexlies)

    Those strong tags are code from here, i was trying to bold parts but it didn’t work. The $name parts works perfect as it should as nothing changed from that plugin.. the country taxonomy gives me this:

    https://ibb.co/eQ2VSS

    As you can see it prints “Array” everywhere instead of the content of the taxonomy.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    If it’s echoing out the word Array, then you need to go “deeper” into the variable than you currently are because you’re trying to echo an array instead of a string still. Basically there’s more to the item being used.

    Thread Starter urbexlies

    (@urbexlies)

    How would I do this? I mean it is one thing to setup a taxonomy and enter values on all my products but if I cannot then use that taxonomy it, well has no use.

    If I understand you correctly, this is not “deep” enough (“country” is the taxonomy name):
    $terms = wp_get_post_terms($product->ID, ‘country’);

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    The “deeper” comment was in relation to what that wp_get_post_terms() function returns, and not how it was used.

    Where it’s likely turning up this “Array” text is at this line:

    $country = '</br><i>' . $terms[0]->name . '</i>';
    

    At this point, for the sake of helping get this through, I’m curious what a var_dump() on the $terms variable would produce, after typing out and refreshing the page.

    var_dump($terms);

    Based on the codex page, this is what it should return as a whole:

    An array of taxonomy terms, or empty array if no terms are found
    
    Thread Starter urbexlies

    (@urbexlies)

    Where in the code do you want me to add this?
    var_dump($terms);

    But I didn’t do anything outside of the scope of your plugin. I used it to add a taxonomy, I called it “country” and then edited all my products (1000+) to have the correct country in that taxonomy. That’s it, I didn’t do anything else.

    • This reply was modified 6 years, 11 months ago by urbexlies.
    • This reply was modified 6 years, 11 months ago by urbexlies.
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Right after you set it, before this:

    // Set the empty $country string.
    $country = '';
    
    Thread Starter urbexlies

    (@urbexlies)

    It gives me at the top of the page:

    array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { }

    Twelve times, one for each product that is in the table for that page. (so I guess it does that function in a loop for every product and this goes over that dump line twelve times).

    EDIT: if it helps, this is how I added the content of taxonomies to products:
    https://ibb.co/f34dxS

    In this case “ITALY”. I did this for every product and now I want ITALY or well whatever country is in there to be displayed.

    • This reply was modified 6 years, 11 months ago by urbexlies.
    • This reply was modified 6 years, 11 months ago by urbexlies.
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    That output is telling me that $terms is empty, but it’s making it through that if check. I wonder if it’s still returning true for being what’s known as “truthy”

    Try changing:

    if($terms){
    

    to

    if( ! empty( $terms ) ){
    

    and see if it gets through then. You also should confirm that some of the products in this section of products do indeed have terms associated from this taxonomy.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Any news on this front @urbexlies ?

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Insert content of taxonomy in php’ is closed to new replies.