__( 'Create product', 'woocommerce' ), 'description' => __( 'Create a product using supported catalog fields.', 'woocommerce' ), 'category' => 'woocommerce', 'input_schema' => self::build_input_schema(), 'output_schema' => self::get_entity_output_schema( 'product', self::get_product_output_schema() ), 'permission_callback' => array( __CLASS__, 'can_create_product' ), 'meta' => array( 'show_in_rest' => true, 'mcp' => array( 'public' => true, 'type' => 'tool', ), 'annotations' => array( 'readonly' => false, 'idempotent' => false, 'destructive' => false, ), ), ); } /** * Resource that the ability changes. * * @return string */ public function get_resource(): string { return 'product'; } /** * Create the product object of the input type, without saving it. * * @param array $input Ability input. * @return \WC_Product|\WP_Error */ public function load( array $input ) { $product_config = self::get_product_config_for_alias( $input['product_type_alias'] ?? 'physical' ); if ( is_wp_error( $product_config ) ) { return $product_config; } $product = wc_get_product_object( $product_config['wc_type'] ); if ( ! $product ) { return new \WP_Error( 'woocommerce_invalid_product_type', __( 'Invalid product type.', 'woocommerce' ), array( 'status' => 400 ) ); } return $product; } /** * Set the product properties in memory. * * @param \WC_Product $subject Product. * @param array $input Ability input. * @return null|\WP_Error */ public function change( $subject, array $input ) { $product_config = self::get_product_config_for_alias( $input['product_type_alias'] ?? 'physical' ); if ( is_wp_error( $product_config ) ) { return $product_config; } try { self::apply_product_type_config( $subject, $product_config ); return self::set_product_props_from_input( $subject, $input, $product_config ); } catch ( \WC_Data_Exception $exception ) { return self::get_product_data_exception_error( $exception ); } } /** * Save the product. * * @param \WC_Product $subject Changed product. * @return null|\WP_Error */ public function save( $subject ) { return self::save_product( $subject, 'woocommerce_product_create_failed' ); } /** * The ability output for the saved product. * * @param \WC_Product $subject Saved product. * @return array */ public function prepare_response( $subject ): array { return array( 'product' => self::format_product_for_response( $subject ), ); } /** * Check product creation access. * * @param mixed $input Ability input. * @return bool * * @since 10.9.0 */ public static function can_create_product( $input = array() ): bool { // Cap is not object-scoped for create. unset( $input ); return wc_rest_check_post_permissions( 'product', 'create' ); } /** * Get the ability input schema. * * @return array */ private static function build_input_schema(): array { return self::get_product_create_input_schema(); } }