Skip Navigation

[Resolved] Filter view with variation post id

This support ticket is created 5 years, 4 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: Asia/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 17 replies, has 2 voices.

Last updated by jelle-jacobd 5 years, 4 months ago.

Assisted by: Luo Yang.

Author
Posts
#1167159

Tell us what you are trying to do?
I'm trying to display data from product attributes of a specific product variation on a separate page. I need this so customers can print the data for this specific variation. Thing is, it looks like the view filter ids only shows results when there a match on the parent id not on a variation id, which is also retrieved by wpv_product-id correct? How can I solve this so my page shows attribute info on the page in the link from a specific variation?

Is there a similar example that we can see?
hidden link

#1167422

Hello,

Woocommerce plugin stores the variation product as a post of post type "product_variation", but the post type "product_variation" is not supported by Views plugin, in your case, you can try to use filter hook "wpv_filter_query" to change the post_type parameter, for example:
1) create a post view query product posts, for example view's ID is 123

2) add a Post parent filter:
Select posts whose parent is the page where the View is shown.
https://toolset.com/documentation/user-guides/displaying-brother-pages/

3) Add below codes into your theme file functions.php:

add_filter('wpv_filter_query', function($query_args, $view_settings, $view_id){
	if($view_id == 123){
		$query_args['post_type'] = 'product_variation';
	}
	return $query_args;
	
}, 10, 3);

Please replace 123 with your view's ID of step 1)

More help:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
When displaying a View listing posts, this filter is applied to the arguments being generated by the View settings before they are passed to the WP_Query class.

#1167565

Hi Luo,

Thanks. Let see if I understand this correctly. My setup looks like this at the moment.

On the product page I have list of variations in a table with ID, SKU and some attributes displayed in separate columns. The ID column values contain a link to a separate page called datasheet and is also passing an extra variable called product_id within the link, which is the product_id of the variable product, so far so good. This is the correct ID.

1) The page called datasheet contains a view (called Datasheet as well) that returns product posts, which contains in my case a loop and template which contains [wpv-post-title] and [wpv-woo-list_attributes].

Before I posted this support ticket this page with this view was only displaying values when the parent product_id was passed. If I understand you correctly this is normal behaviour because Views doesn't support the post type product_variation and for that it doesn't filter these variations.

2) The create a workaround for showing a specific variation I should change the filter for my view from Post ID filter (Posts with IDs set by this URL parameter: product_id) to Post parent filter (Parent is the page where this View is shown)

3) I have added the code to my functions.php and changed the View ID to 58 in my case.

The page datasheet now returns: no items. Please see: hidden link
57025 is a variation product_id not a parent.

Should I include the view on the product page instead to get things right or should it work on a separate page like it is now?

#1168345
parent-filter.JPG

1) You are right, Views doesn't support the post type product_variation

2) The workaround works in a single product post

So in your case, it needs a nested view, for example:
1) Parent post view query product posts (same as your original view's setting)
- In the view's loop, display below child view

2) Child view(ID 123) query product posts(We use filter hook to change it to post type "product_variation")
- Select posts whose parent is the current post in the loop.
See screenshot parent-filter.JPG
- In the view's loop, display variation product information

3) Add below codes into your theme file functions.php:

add_filter('wpv_filter_query', function($query_args, $view_settings, $view_id){
    if($view_id == 123){
        $query_args['post_type'] = 'product_variation';
    }
    return $query_args;
     
}, 10, 3);

Hope it is clear

#1168431

Thanks. I will try. So again, for my understanding. I can’t use this setup on a separate page called datasheet. To which I will link to with a link containing the variation ID? I’ll need to nest the loop in my single product page template.

#1168439

No, you can use it in a separate page called datasheet, in that datasheet page, you can display a nested view:
- parent view list product posts
- child view list variation product posts

See details in my above answer:
https://toolset.com/forums/topic/filter-view-with-variation-post-id/#post-1168345
So in your case, it needs a nested view, for example:

#1168934
Schermafdruk 2018-12-18 20.36.51.png
Schermafdruk 2018-12-18 20.34.50.png
Schermafdruk 2018-12-18 20.33.59.png

Hi Luo,

Thanks again. I did what you suggested, but the page is showing nothing (blank page). I guess I'm doing something wrong..
Is it possible for you to check my setup with the screenshots?

Looks like there in reverse order:
Screenshot 1 = view shortcode in page. Think nothing can go wrong here.
Screenshot 2 = parent view
Screenshot 3 = child view

I have placed the filter in functions.php

hidden link

60032 = existing product ID.

#1169060

Please provide your website credentials in below private message box, I can setup a demo for you. thanks

#1169415

Thanks for the details, it does work in your website, I have tried to test it in your website with below steps:
1) Edit the child view "Datasheet Child view", in section "Loop Editor", add a post link shortcode:
[wpv-post-link]
Test it in front-end:
it does output some variations product posts links, see it here:
hidden link
- LIVAL SECUR 816 (link URL parameter: attribute_pa_color-housing=black-ral-9005)
- LIVAL SECUR 816 (link URL parameter: attribute_pa_color-housing=white-ral-9016)
- LIVAL SECUR 816 (link URL parameter: attribute_pa_color-housing=grey-ral-9006)

2) The shortcode [wpv-woo-list_attributes] won't work in single "variations product" post, it works only in single "product" post, see our document:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-woo-list_attributes
context: single product page and product listing

So you will need to put it into the parent view's loop:
hidden link

Hope it is clear

#1169437

Thanks Luo,

Good to hear i did it correctly for at least a part;)

1) It does output some variations that's correct. But the output is related to more then one variation. There's only one color available per variation and it now shows three instead, so that's where I'm banging my head around..

2) Oke clear, so I should replace this shortcode with separate shortcodes for the needed attributes I guess?

I will check the document you suggested.

#1169446
color-housing.JPG

Q1) But the output is related to more then one variation.
No, you have setup three attribute value in post 60032, Please edit the product post ID (60032), you should be able to see those settings, see screenshot color-housing.JPG

Q2) For the new question:
so I should replace this shortcode with separate shortcodes for the needed attributes I guess?

Yes, you are right, you will need to setup separate shortcodes the for the needed attributes, and there isn't such a built-in feature to display the attribute value in "product_variation" post, you might consider custom codes.

#1169463

Thanks we're getting there. But I need to display the values of a single variation of a single product on the datasheet page. not three or more. Should I add an extra filter to do so?

The screenshot shows the available values for the variations. The variations itself has just one per variation. These are the values (of a single variation) that I need to show on that page. This means I need to display the data of one single variation for a specific product. I'm linking to this page by a link in the variations table, like shown here in the table column Wpcf Datasheet:

hidden link

The variation ID: 57025 in the Wpcf Datasheet column links to the page datasheet and should show the attributes for this specific variation. This where I need the views and filters for. De data displayed there is alike the data in the other columns next to Wpcf Datasheet but with some extra attributes displayed verticaly instead of in table so people can easily print it or save as PDF.

#1169519

I have tested it in my localhost, in the single "product_variation" post, there is a custom field "attribute_pa_color-housing", value is attribute's term slug, so you can use it to display it in the child view, for example:
[wpv-post-field name="attribute_pa_color-housing"]

See the result here:
hidden link

And since you can get the term's slug, you will be able to setup custom shortcodes to get the term's title:
https://codex.wordpress.org/Function_Reference/add_shortcode
https://developer.wordpress.org/reference/functions/get_terms/

#1169922

Thanks Luo. Do I need an extra filter to show just one perticular variation instead of all the available variations for 1 (parent) product?

#1170069

No, you don't need other extra filter, there is only one filter needed, see my first answer:
https://toolset.com/forums/topic/filter-view-with-variation-post-id/#post-1167422

If you just need one variation post, you can set view, limit to one in the result.

Since the "attribute_pa_color-housing" is a custom field, you can also add field filter to your view to get the specific variation post. See our document:
https://toolset.com/documentation/user-guides/filtering-views-by-custom-fields/

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.