WC_Product_Data_Store_CPT::create()publicWC 1.0

Method to create a new product in the database.

Method of the class: WC_Product_Data_Store_CPT{}

Return

null. Nothing (null).

Usage

$WC_Product_Data_Store_CPT = new WC_Product_Data_Store_CPT();
$WC_Product_Data_Store_CPT->create( $product );
$product(WC_Product) (required) (passed by reference — &)
Product object.

WC_Product_Data_Store_CPT::create() code WC 8.7.0

public function create( &$product ) {
	if ( ! $product->get_date_created( 'edit' ) ) {
		$product->set_date_created( time() );
	}

	$id = wp_insert_post(
		apply_filters(
			'woocommerce_new_product_data',
			array(
				'post_type'      => 'product',
				'post_status'    => $product->get_status() ? $product->get_status() : 'publish',
				'post_author'    => get_current_user_id(),
				'post_title'     => $product->get_name() ? $product->get_name() : __( 'Product', 'woocommerce' ),
				'post_content'   => $product->get_description(),
				'post_excerpt'   => $product->get_short_description(),
				'post_parent'    => $product->get_parent_id(),
				'comment_status' => $product->get_reviews_allowed() ? 'open' : 'closed',
				'ping_status'    => 'closed',
				'menu_order'     => $product->get_menu_order(),
				'post_password'  => $product->get_post_password( 'edit' ),
				'post_date'      => gmdate( 'Y-m-d H:i:s', $product->get_date_created( 'edit' )->getOffsetTimestamp() ),
				'post_date_gmt'  => gmdate( 'Y-m-d H:i:s', $product->get_date_created( 'edit' )->getTimestamp() ),
				'post_name'      => $product->get_slug( 'edit' ),
			)
		),
		true
	);

	if ( $id && ! is_wp_error( $id ) ) {
		$product->set_id( $id );

		$this->update_post_meta( $product, true );
		$this->update_terms( $product, true );
		$this->update_visibility( $product, true );
		$this->update_attributes( $product, true );
		$this->update_version_and_type( $product );
		$this->handle_updated_props( $product );
		$this->clear_caches( $product );

		$product->save_meta_data();
		$product->apply_changes();

		do_action( 'woocommerce_new_product', $id, $product );
	}
}