phpwordpresswoocommerceproduct

Define External as default product type instead of Simple for WooCommerce created products


What I'm trying to do is every time a product is created, whether in the backend or frontend, or through any other method, the created product is automatically set to type "external" instead of the default Simple product.

I saw this answer code that allow to change the product type from an existing product. I'm new to everything and trying to see how I could implement it to add to my functions.php file. I thought I could replace "variable" type with "external" to make that work for me.

How could I implement that code into a PHP function that changes any created product to an external product type?

Maybe combining that answer code with the code below:

function woo_set_type_function(){
   $product_id = 18; //your product ID
   wp_remove_object_terms( $product_id, 'simple', 'product_type' );
   wp_set_object_terms( $product_id, 'external', 'product_type', true );
}
add_action('init', 'woo_set_type_function');

I'm not experienced, and I didn't want to break anything in the functions file by trying to combine this myself.


Solution

  • The class name of an external product is WC_Product_External, so we don't need to use WC_Product_Factory to get the product class name for an external product.

    You can use the following hooked function to change any product that is not external, to an external product (from a defined product ID):

    add_action('init', 'change_product_to_external_type');
    function change_product_to_external_type(){
        $product_id = 18; // Define your product ID
    
        $product = wc_get_product($product_id); // Get the product object
    
        // Check that the current product is not external
        if ( ! $product->is_type('external') ) {
            // Get an external product instance of the product
            $product = new WC_Product_External( $product_id );
            $product->save(); // Save product to database and sync caches
        }
    }
    

    For multiple product IDs, use the following:

    add_action('init', 'change_product_to_external_type');
    function change_product_to_external_type(){
        $product_ids = array( 18, 19, 25); // Define your product IDs in the array
    
        // Loop through the array of product IDs
        foreach ( $product_ids as $product_id ) {
            $product = wc_get_product($product_id); // Get the product object
    
            // Check that the current product is not external
            if ( ! $product->is_type('external') ) {
                // Get an external product instance of the product
                $product = new WC_Product_External( $product_id );
                $product->save(); // Save product to database and sync caches
            }
        }
    
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). It should work.


    Addition

    You can also set the external product type first using:

    add_filter('product_type_selector', 'external_product_type_first');
    function external_product_type_first( $product_types ) {
        // Get external product in a separate array
        $external_type = array( 'external' => $product_types['external'] ); 
        // Remove external product type from the original array
        unset($product_types['external']);
        // Add external first in the product types array
        return array_merge( $external_type, $product_types );
    }
    

    Note: This filter hook is located in wc_get_product_types() function.

    You will get the following (from the product type selector):

    enter image description here

    enter image description here


    Only External products

    The following will restrict the product type selector in the admin to external product type only, so only external products can be created:

    add_filter('product_type_selector', 'only_external_products');
    function only_external_products( $product_types ) {
        // Return only external product type
        return array( 'external' => $product_types['external'] );
    }
    

    Code goes in functions.php file of your child theme (or in a plugin).

    In admin single product pages, you will get:

    enter image description here

    So all created / Edited products will be "external".