WC_AJAX::create_product_with_attributes
Create product with attributes from POST data.
Method of the class: WC_AJAX{}
No Hooks.
Returns
Mixed. Product class.
Usage
$result = WC_AJAX::create_product_with_attributes( $data );
- $data(array) (required)
- Attribute data.
WC_AJAX::create_product_with_attributes() WC AJAX::create product with attributes code WC 10.8.1
private static function create_product_with_attributes( $data ) {
// phpcs:disable WordPress.Security.NonceVerification.Missing
if ( ! isset( $_POST['post_id'] ) ) {
wp_die( -1 );
}
$attributes = WC_Meta_Box_Product_Data::prepare_attributes( $data );
$product_id = absint( wp_unslash( $_POST['post_id'] ) );
$product_type = ! empty( $_POST['product_type'] ) ? wc_clean( wp_unslash( $_POST['product_type'] ) ) : ProductType::SIMPLE;
$classname = WC_Product_Factory::get_product_classname( $product_id, $product_type );
$product = new $classname( $product_id );
$product->set_attributes( $attributes );
$product->save();
return $product;
}