WC_Product_Variation_Data_Store_CPT::create( WC_Product_Variation $product )
Create a new product.
Description Description
Parameters Parameters
- $product
-
(Required) Product object.
Source Source
File: includes/data-stores/class-wc-product-variation-data-store-cpt.php
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 );
}
}
Changelog Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |