WC_Product_Variation_Data_Store_CPT::create()publicWC 3.0.0

Create a new product.

Method of the class: WC_Product_Variation_Data_Store_CPT{}

Return

null. Nothing (null).

Usage

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

Changelog

Since 3.0.0 Introduced.

WC_Product_Variation_Data_Store_CPT::create() code WC 8.7.0

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

	$new_title = $this->generate_product_title( $product );

	if ( $product->get_name( 'edit' ) !== $new_title ) {
		$product->set_name( $new_title );
	}

	$attribute_summary = $this->generate_attribute_summary( $product );
	$product->set_attribute_summary( $attribute_summary );

	// The post parent is not a valid variable product so we should prevent this.
	if ( $product->get_parent_id( 'edit' ) && 'product' !== get_post_type( $product->get_parent_id( 'edit' ) ) ) {
		$product->set_parent_id( 0 );
	}

	$id = wp_insert_post(
		apply_filters(
			'woocommerce_new_product_variation_data',
			array(
				'post_type'      => 'product_variation',
				'post_status'    => $product->get_status() ? $product->get_status() : 'publish',
				'post_author'    => get_current_user_id(),
				'post_title'     => $product->get_name( 'edit' ),
				'post_excerpt'   => $product->get_attribute_summary( 'edit' ),
				'post_content'   => '',
				'post_parent'    => $product->get_parent_id(),
				'comment_status' => 'closed',
				'ping_status'    => 'closed',
				'menu_order'     => $product->get_menu_order(),
				'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->handle_updated_props( $product );

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

		$this->update_version_and_type( $product );
		$this->update_guid( $product );

		$this->clear_caches( $product );

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