Skip Navigation

[Resolved] Create Relationship between Custom Post Type and WooCommerce Category

This support ticket is created 6 years, 3 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 6 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1139204

I have a custom post type called Athletes.
In each athlete I would like to be able to select a WooCommerce product category for that Athlete.

Using that data I can then post that athletes latest products on their single template page and have a button to link to their full shop category listing.

So the athlete post wont 'belong' to the shop product category but it will link to it.

Is that possible? Many thanks.

#1139816

Hi, what if you store the corresponding product category slug as a single line text field on the Athlete post? It will be easier than trying to maintain a select field populated by all the dynamic product categories in WooCommerce. For example, create a single-line text custom field on the Athletes post type called "pcat-slug". Whenever you create an Athlete post, save the slug of their corresponding product category. Then build your link to the Product Category archive using the slug something like this:

<a href="<em><u>hidden link</u></em> name='wpcf-pcat-slug']">See all [wpv-post-title]'s Products</a>

That will allow you to use the Athlete's pcat-slug field in a View shortcode attribute. This way you can create a View of Products filtered by Product Category term slug, something like this in your Athlete template:

[wpv-post-title]'s Latest Products:<br />
[wpv-view name="Your Latest Product View" wpvproductcat="[wpv-post-field name='wpcf-pcat-slug']"]

This technique is called passing arguments into Views. More info about this:
https://toolset.com/documentation/user-guides/passing-arguments-to-views

#1143643

Thanks! That would work really well.

I guess the only downside is exactly as you say - it wont give me a nice dropdown to choose from on the admin site, would have to look up the slug and type it manually.

I was able to find some code posted by another user on this forum....

add_action( 'init', 'add_product_cat_to_custom_post_type',11 );

function add_product_cat_to_custom_post_type() {
register_taxonomy_for_object_type( 'product_cat', 'athlete' );
}

This let me assign a product_category to an athlete post. Then I used hidden link plugin to ensure only one category could be selected.

This seems to have done the trick but I don't know if WooCommerce will get upset by the custom post type belonging to a category that is designated for products. So far all seems well but I will report back if it breaks anything.

Thanks again

#1144473

Sounds like you have a custom code approach. I'll stand by for your update.