Skip Navigation

[Resolved] Bug: Woocommerce Variable Product and toolset_get_related_posts()

This support ticket is created 4 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

Tagged: 

This topic contains 4 replies, has 3 voices.

Last updated by andresR 4 years, 11 months ago.

Assisted by: Jamal.

Author
Posts
#1800069

I have WooCommerce installed.

I have one simple and one variable product.

I have a many-to-many relationship between Product and Posts, named "product-post".

Then, I associate one simple Product to some Posts, and one Variable Product to the same Posts.

Then I try to get, using function toolset_get_related_posts(), the Posts associated to these two separate Products.

I do it like this:

    $relatedPosts = toolset_get_related_posts( 
      $product->id, 
      'product-post', 
      [
          'query_by_role' => 'parent', 
          'limit' => 16, 
          'offset' => 0, 
          'role_to_return' => 'child',
          'return' => 'post_id'
      ]
  );

With the simple product all works fine, it finds the related posts associated to this simple product.

But with the variable product it doesn't find anything, no associated posts are found. Why?

Please, help!

#1801369

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

There's no reason in principle this shouldn't work, and I just did some light testing, and I can see that connecting both variable and simple products to posts stores the correct associations in the relationship tables in the database.

You might want to check that yourself if you are comfortable using phpMyAdmin to inspect the database.

You should see the IDs of the posts and products that are being joined stored in wp_toolset_connected_elements, and then the associations between these elements are stored in wp_toolset_associations.

When I connect variable as well as simple products I can see these are correctly stored.

I don't know what context you are using the above code, but I suspect the issue is that $product->id is not the id of the parent product variation post but may be the id of one of the variations.

You should familiarise yourself with how WooCommerce stores product posts, specfically product variations. There is one master post stored in wp_posts (which is the post you are connecting with Toolset relationships), and each variation is stored as a separate entry in wp_posts (post type product_variation) linked to the master post as parent.

In your code $product->id is likely not the id of the master variation post (you should dump it to your debug log so that you can verify what id is being generated). WooCommerce presumably has some means of retrieving the parent ID if that's the case.

#1801549

Thanks, Nigel!

It works now

I was doing a previous query to retrieve the upsells for that product, and the $product->id was the one for the last product queried

So, all I had to do is to call wp_reset_postdata()

Regards

#1801941

Hello,

I wonder if you still need assistance with your first request, or we can mark this ticket as resolved.

Let me know if you have any other questions.

#1802023

My issue is resolved now. Thank you!