WC_Product_Data_Store_CPT::create()
Method to create a new product in the database.
Method of the class: WC_Product_Data_Store_CPT{}
Hooks from the method
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() WC Product Data Store CPT::create code WC 9.3.3
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 ); $sku = $product->get_sku(); /** * If SKU is already under processing aka Duplicate SKU * because of concurrent requests, then we should not proceed * Delete the product and throw an exception only if the request is * initiated via REST API */ if ( ! empty( $sku ) && WC()->is_rest_api_request() && ! $this->obtain_lock_on_sku_for_concurrent_requests( $product ) ) { $product->delete( true ); // translators: 1: SKU. throw new Exception( esc_html( sprintf( __( 'The SKU (%1$s) you are trying to insert is already under processing', 'woocommerce' ), $sku ) ) ); } // get the post object so that we can set the status // to the correct value; it is possible that the status was // changed by the woocommerce_new_product_data filter above. $post_object = get_post( $product->get_id() ); $product->set_status( $post_object->post_status ); $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 ); } }