Skip Navigation

[Gelöst] Can multiple CPTs share a woocommerce product taxonomy?

This support ticket is created vor 7 Jahre. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 5 Antworten, has 2 Stimmen.

Last updated by Minesh vor 7 Jahre.

Assisted by: Minesh.

Author
Artikel
#519214

We are building a site for a machine broker and we are using Woocommerce as the catalog for showing machines. We are creating a CPT 'Contact' who needs to be categorized with the same taxonomy as the machines. Contacts can own machines or just express an interest in a machine category. We want to use the Woocommerce product categories for the CPT contacts. Can we do that?

#519270

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

To use third party plugin taxonomy with custom post type created using Types yu need to add Types hook wpcf_type to your functions.php file.

Please try to add following code to your current theme's functions.php file:

add_filter( 'wpcf_type', 'toolset_support_add_taxonomy_to_cpt', 10, 2 );
function toolset_support_add_taxonomy_to_cpt( $params, $post_type ) {
    // adjust to your needs
    $cpt_slug   = array('contact');    /// add your post type slug here
    $taxonomies_to_add = array( 'product_cat' );
    // stop editing
     
    if( !in_array($post_type,$cpt_slug) ) {
        return $params;
    }
 
    $params['taxonomies'] = isset( $params['taxonomies'] ) ? $params['taxonomies'] : array();
 
    if( is_array( $taxonomies_to_add ) ) {
        $params['taxonomies'] = array_merge( $params['taxonomies'], $taxonomies_to_add );
    } else {
        $params['taxonomies'][] = $taxonomies_to_add;
    }
 
    return $params;
}

#519496

Hi. Using the code you provided (changing the slug to pm-contact), it worked great! The machine taxonomy is available on the contact CPT.

I changed the code to use another CPT (the slug is pm-company) and I got a php error that says I can't redeclare the taxonomy. Is it possible to use the woo product taxonomy on more than one CPT created in Toolset?

Thanks.

#519723

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

I checked with two different custom post types created using types and assign the same taxonomy product_cat to both custom post types and its working fine.

add_filter( 'wpcf_type', 'toolset_support_add_taxonomy_to_cpt', 10, 2 );
function toolset_support_add_taxonomy_to_cpt( $params, $post_type ) {
    // adjust to your needs
    $cpt_slug   = array('student','city');    /// add your post type slug here
    $taxonomies_to_add = array( 'product_cat' );
    // stop editing
      
    if( !in_array($post_type,$cpt_slug) ) {
        return $params;
    }
  
    $params['taxonomies'] = isset( $params['taxonomies'] ) ? $params['taxonomies'] : array();
  
    if( is_array( $taxonomies_to_add ) ) {
        $params['taxonomies'] = array_merge( $params['taxonomies'], $taxonomies_to_add );
    } else {
        $params['taxonomies'][] = $taxonomies_to_add;
    }
  
    return $params;
}

As you can see I've two post types student and city and to both product_cat taxonomy is assigned.

#520789

Hi. That worked great! Thank you. So now I have one more question, please. I've created two CPTs with WP-Types and they use the woo product taxonomy. One CPT (Company) is the parent and another CPT (Contact) is the child in terms of their relationship.

I need to tie the woo product (a CPT) to the Company (another CPT). How can i make the Woo product a parent and the company a child? Each machine is owned by a company; the contacts are people in the company. By making the product the parent and the company the child, I can illustrate machine ownership.

Thanks.

m

#520790

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

Great - glad to know that you get it working and the solution I shared help you to resolve your issue.

As your original issue is resolved - as per our support policy we entertain only one question per ticket, this will help other users searching on the forum. I kindly request you to open a new ticket with your each new question.

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