0

I have created a custom post type for "Solutions" which will eventually replace all products that have been added in Woocommerce as I no longer have the need for a shopping cart.

So I need to copy the data from each product to a new 'solution' CPT.

I have the below code to set up the new post type:

function create_solution_post_type()
{
    register_post_type('solution',
                       [
                           'labels'      => [
                               'name'          => __('Solutions'),
                               'singular_name' => __('Solution'),
                           ],
                           'public'      => true,
                           'has_archive' => true,
                            'rewrite' => [
                                            'slug' => 'product',
                                        ],                         
                       ]
    );

    register_taxonomy(
        'solution-area',
        'solution',
        [
            'labels' => [
                'name' => __( 'Solution Areas' ),
                'singular_name' => __( 'Solution Area' ),
            ],
            'hierarchical' => true,
            'show_admin_column' => true,
        ]
    );
}
add_action('init', 'create_solution_post_type');

After resetting the permalinks, it works fine for the custom post type but whenever I go to view a woocommerce product page I get, "Oops! That page can’t be found."

I assume its because the slug for the product and solution is the same? Although I don't really understand why this would be a problem if the overall url is different eg. example.com/product/product-name in woocommommerce will become example.com/product/product-name-2 for the custom post type until I delete the products and remove woocommerce.

I need the CTP 'solution' and woocommerce products working in conjunction with the same 'products' slug. How can I achieve this?

Many thanks in advance for your help.

1
  • Please can someone get back to me regarding my responses? Thanks :)
    – LeeTee
    Commented Dec 8, 2016 at 14:45

5 Answers 5

1

I haven't tried this myself yet – and I would probably always prefer to go with the default 'products' for compatibility reasons… However it should be very well possible to add your own CPT to WooCommerce and then "move" all products to that new CPT.

First you'll have to add a CPT to your WordPress installation that is compatible with WooCommerce so you can add things to the cart etc. Reigel Gallarde has written a step-by-step guide on how to do this: How to add custom post type to WooCommerce.

Then you could export all Products and import them as Solutions using the pTypeConverter.

Don't forget to backup everything before you start!

1
  • I want to remove Woocommcerce eventually, hence the reason for setting up "Solutions" so I can gradually move each product over from Woocommerce to the CPT. My query is regarding the permalinks as I need to be able to use the 'products' slug for the CPTs and the Products
    – LeeTee
    Commented Dec 7, 2016 at 14:13
0

You are replaced only slug but not a post type. To move all post type product to solution you should go to phpmyadmin table wp_posts and replace all post_type='product' on 'solution'

To fix 404 page, go to Settings->Permalinks and click Save Changes to refresh it.

1
  • Yours sounds like a good suggestion although I dont want to swtich all the products to solutions in one go. Im not sure this would work well due to needing Advanced Custom Fields for the new Post Type. I therefore need to create a new Solution and then copy over the content from a Product page into the solution. I would then remove that product but all other products would still need to work.
    – LeeTee
    Commented Dec 7, 2016 at 14:09
0

Myself I have not experienced this problem myself before but I would try to backtrack my steps and undo them. In that case I would think that you would have to unregister the new custom type Solutions and set the slug for the WC custom post type back to product again. For setting the slug for the WC products I would first try this via the normal Permalink options and set it to default and if this doesn’t do the trick I would also try setting it to this via the custom base permalink option for WC.

Step 1 unregister custom type

// Function to unregister type
if ( ! function_exists( 'unregister_post_type' ) ) :
function unregister_post_type( $post_type ) {
    global $wp_post_types;
    if ( isset( $wp_post_types[ $post_type ] ) ) {
        unset( $wp_post_types[ $post_type ] );
        return true;
    }
    return false;
}
endif;


//  Unregister your new type solution
unregister_post_type(‘solution’);

See Deregister custom post types

Step 2 set permalink base products Set the permalink back to product for the WC base option.

PS. Myself I don’t know the method to create a custom post type and move all existing WooCommerce products to populate your new custom post type Solutions. I’m stretching it here but I guess you are wanting to create a WooCommerce product type of your own and build for your specs? In that case I would approach it differently. I would use product attributes for creating extended features for most probably simple products type. These I would use to add custom meta info for each post like type, price class etc etc. Then you can use these to start all sorts of logic branching based on this information instead of creating a new type for what is actually an extensions of the product type anyway.

I have created quite sophisticated features by only extending the product type using only product attributes ie for bookable products that are linked to Google Calendars and this for rental companies and boat trip companies etc. Both use the same basic product attributes from which I use the values in custom plug-in code. See this one ie which is almost finished. It uses product attributes for almost everything you can select http://kcd.meta-inc.com/, or this one where the product attributes are used to link products (cars) to calendars and make search based on date and the realtime availability of the car in a Google calendar possible. http://huurauto-curacao.com/

Or am I mistaking about what you are wanting to do? It needs to be pretty darn different or complex to need to is my opinion so that's why I’m really asking?

Good luck and hope this helps.

3
  • Thank you for your response, there has been lot of effort put into it, although. I don't understand why I would unregister the CPT? I need a way to have both CPT and Products working in conjunction and both using the same permalink. I don't see why this should be such a complex thing to do.
    – LeeTee
    Commented Dec 7, 2016 at 14:04
  • LeeTee what is your experience witch complex applications? I could be mistaken but are you not out of your reach here? Your comments show you do not fully grasp the concept of class extension, the design principle for WP and WC. In this class logic you would never copy a class and or move it's elements with the purpose of keeping them as they are to only add some new properties to the copied class. For this you would extend the WC product class and not copy it. Slugs need to be unique for some purposes and if it conflicts it could end up killing a important wordpress function or messing up. Commented Dec 8, 2016 at 22:48
  • I dont want woocommerce, therefore I would not extend it! am replacing woocommcerce. I am not trying to copy a class. i think you are totally misunderstanding what I am trying to do but thanks anyway :)
    – LeeTee
    Commented Dec 9, 2016 at 11:04
0

Don't forget to reset your permalinks after adding/removing custom post types or you will get a 404 error when visiting the single pages. (Re-save them in your settings).

1
  • Yes I reset the permalinks after adding the custom post type and got 404 for all woocommcerce products but if I remove the custom post type and reset permalinks again, woocommerce products work again. I need a way to have both CPT and Products working in conjunction.
    – LeeTee
    Commented Dec 7, 2016 at 14:00
0

Looks like slugs have to be unique for all posts according to wordpress documentation.

Post slugs must be unique across all posts

Therefore it is not possible to have the same slug for woocommcerce products and the CPT as I assume Woocommcerce Products are just posts. I will have to have a different slug and use redirects.

Thanks for your help guys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.