WC_REST_Product_Attributes_Controller::create_item()
Create a single attribute.
Method of the class: WC_REST_Product_Attributes_Controller{}
Hooks from the method
Return
WP_REST_Request|WP_Error
.
Usage
$WC_REST_Product_Attributes_Controller = new WC_REST_Product_Attributes_Controller(); $WC_REST_Product_Attributes_Controller->create_item( $request );
- $request(WP_REST_Request) (required)
- Full details about the request.
WC_REST_Product_Attributes_Controller::create_item() WC REST Product Attributes Controller::create item code WC 9.4.2
public function create_item( $request ) { global $wpdb; $generate_slug = stripslashes( $request['generate_slug'] ); $slug = wc_sanitize_taxonomy_name( stripslashes( $request['slug'] ) ); if ( ! empty( $generate_slug ) && 'true' === $generate_slug ) { $slug = $this->generate_unique_slug( $request['name'] ); } $id = wc_create_attribute( array( 'name' => $request['name'], 'slug' => $slug, 'type' => ! empty( $request['type'] ) ? $request['type'] : 'select', 'order_by' => ! empty( $request['order_by'] ) ? $request['order_by'] : 'menu_order', 'has_archives' => true === $request['has_archives'], ) ); // Checks for errors. if ( is_wp_error( $id ) ) { return new WP_Error( 'woocommerce_rest_cannot_create', $id->get_error_message(), array( 'status' => 400 ) ); } $attribute = $this->get_attribute( $id ); if ( is_wp_error( $attribute ) ) { return $attribute; } $this->update_additional_fields_for_object( $attribute, $request ); /** * Fires after a single product attribute is created or updated via the REST API. * * @param stdObject $attribute Inserted attribute object. * @param WP_REST_Request $request Request object. * @param boolean $creating True when creating attribute, false when updating. */ do_action( 'woocommerce_rest_insert_product_attribute', $attribute, $request, true ); $request->set_param( 'context', 'edit' ); $response = $this->prepare_item_for_response( $attribute, $request ); $response = rest_ensure_response( $response ); $response->set_status( 201 ); $response->header( 'Location', rest_url( '/' . $this->namespace . '/' . $this->rest_base . '/' . $attribute->attribute_id ) ); return $response; }