0

A WooCommerce product is getting created easily but the product is getting added to the product_type:simple. I want to add the product to the "custom product_type:test"

static function createTicket($postId, $productDetails) {
$id = wp_insert_post(array(
    'post_type' => 'product',
    'post_title' => $productDetails['title'],
    'post_content' => $productDetails['description'],
    'post_status' => get_post_status($postId),
        'max_value' => 1,
));
update_post_meta($id, '_price', $productDetails['price']);
update_post_meta($id, '_regular_price', $productDetails['price']);
update_post_meta($id, '_wpws_testID', $postId);
update_post_meta($id, '_sold_individually', "yes");
wp_set_object_terms( $id, 'test', 'product_type' );
return $id;
}

I have added

wp_set_object_terms( $id, 'test', 'product_type' );

But nothing works

enter image description here

1 Answer 1

1

Used product_type_selector hook

// add a product type
add_filter( 'product_type_selector', 'add_custom_product_type' );
function add_custom_product_type( $types ){
    $types[ 'test_custom_product_type' ] = __( 'test Product' );
    return $types;
}

Edit

Please check below links it help you more on this

Edit

Above code for older version of WooCommerce

Used this code for save product with custom product type. it working with WooCommerce 3.* Check this for more information https://gist.github.com/stirtingale/753ac6ddb076bd551c3261007c9c63a5

function register_simple_rental_product_type() {
    class WC_Product_Simple_Rental extends WC_Product {

        public function __construct( $product ) {
            $this->product_type = 'simple_rental';
            parent::__construct( $product );
        }
    }
}
add_action( 'init', 'register_simple_rental_product_type' );
function add_simple_rental_product( $types ){
    // Key should be exactly the same as in the class
    $types[ 'simple_rental' ] = __( 'Simple Rental' );
    return $types;
}
add_filter( 'product_type_selector', 'add_simple_rental_product' );
Sign up to request clarification or add additional context in comments.

3 Comments

I am already done with those steps. The custom product type is displayed under product_type dropdown. Now I want to assign a new product to the custom product type in backend
How can I achieve this?
I have added these too. Let me be more clear: I am trying to create a woocommerce product through my custom plugin. However product is getting added but to "Simple Product" and not "Test Product" I hope this makes some sense

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.