WC_API_Products::create_product()publicWC 2.2

Create a new product.

Метод класса: WC_API_Products{}

Возвращает

Массив|WP_Error.

Использование

$WC_API_Products = new WC_API_Products();
$WC_API_Products->create_product( $data );
$data(массив) (обязательный)
posted data

Список изменений

С версии 2.2 Введена.

Код WC_API_Products::create_product() WC 8.7.0

public function create_product( $data ) {
	$id = 0;

	try {
		if ( ! isset( $data['product'] ) ) {
			throw new WC_API_Exception( 'woocommerce_api_missing_product_data', sprintf( __( 'No %1$s data specified to create %1$s', 'woocommerce' ), 'product' ), 400 );
		}

		$data = $data['product'];

		// Check permissions.
		if ( ! current_user_can( 'publish_products' ) ) {
			throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_product', __( 'You do not have permission to create products', 'woocommerce' ), 401 );
		}

		$data = apply_filters( 'woocommerce_api_create_product_data', $data, $this );

		// Check if product title is specified.
		if ( ! isset( $data['title'] ) ) {
			throw new WC_API_Exception( 'woocommerce_api_missing_product_title', sprintf( __( 'Missing parameter %s', 'woocommerce' ), 'title' ), 400 );
		}

		// Check product type.
		if ( ! isset( $data['type'] ) ) {
			$data['type'] = 'simple';
		}

		// Set visible visibility when not sent.
		if ( ! isset( $data['catalog_visibility'] ) ) {
			$data['catalog_visibility'] = 'visible';
		}

		// Validate the product type.
		if ( ! in_array( wc_clean( $data['type'] ), array_keys( wc_get_product_types() ) ) ) {
			throw new WC_API_Exception( 'woocommerce_api_invalid_product_type', sprintf( __( 'Invalid product type - the product type must be any of these: %s', 'woocommerce' ), implode( ', ', array_keys( wc_get_product_types() ) ) ), 400 );
		}

		// Enable description html tags.
		$post_content = isset( $data['description'] ) ? wc_clean( $data['description'] ) : '';
		if ( $post_content && isset( $data['enable_html_description'] ) && true === $data['enable_html_description'] ) {

			$post_content = wp_filter_post_kses( $data['description'] );
		}

		// Enable short description html tags.
		$post_excerpt = isset( $data['short_description'] ) ? wc_clean( $data['short_description'] ) : '';
		if ( $post_excerpt && isset( $data['enable_html_short_description'] ) && true === $data['enable_html_short_description'] ) {
			$post_excerpt = wp_filter_post_kses( $data['short_description'] );
		}

		$classname = WC_Product_Factory::get_classname_from_product_type( $data['type'] );
		if ( ! class_exists( $classname ) ) {
			$classname = 'WC_Product_Simple';
		}
		$product = new $classname();

		$product->set_name( wc_clean( $data['title'] ) );
		$product->set_status( isset( $data['status'] ) ? wc_clean( $data['status'] ) : 'publish' );
		$product->set_short_description( isset( $data['short_description'] ) ? $post_excerpt : '' );
		$product->set_description( isset( $data['description'] ) ? $post_content : '' );
		$product->set_menu_order( isset( $data['menu_order'] ) ? intval( $data['menu_order'] ) : 0 );

		if ( ! empty( $data['name'] ) ) {
			$product->set_slug( sanitize_title( $data['name'] ) );
		}

		// Attempts to create the new product.
		$product->save();
		$id = $product->get_id();

		// Checks for an error in the product creation.
		if ( 0 >= $id ) {
			throw new WC_API_Exception( 'woocommerce_api_cannot_create_product', $id->get_error_message(), 400 );
		}

		// Check for featured/gallery images, upload it and set it.
		if ( isset( $data['images'] ) ) {
			$product = $this->save_product_images( $product, $data['images'] );
		}

		// Save product meta fields.
		$product = $this->save_product_meta( $product, $data );
		$product->save();

		// Save variations.
		if ( isset( $data['type'] ) && 'variable' == $data['type'] && isset( $data['variations'] ) && is_array( $data['variations'] ) ) {
			$this->save_variations( $product, $data );
		}

		do_action( 'woocommerce_api_create_product', $id, $data );

		// Clear cache/transients.
		wc_delete_product_transients( $id );

		$this->server->send_status( 201 );

		return $this->get_product( $id );
	} catch ( WC_Data_Exception $e ) {
		$this->clear_product( $id );
		return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
	} catch ( WC_API_Exception $e ) {
		$this->clear_product( $id );
		return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
	}
}