WC_REST_Product_Attributes_V1_Controller::create_item() public WC 1.0
Create a single attribute.
{} It's a method of the class: WC_REST_Product_Attributes_V1_Controller{}
Hooks from the method
Return
WP_REST_Request|WP_Error.
Usage
$WC_REST_Product_Attributes_V1_Controller = new WC_REST_Product_Attributes_V1_Controller(); $WC_REST_Product_Attributes_V1_Controller->create_item( $request );
- $request(WP_REST_Request) (required)
- Full details about the request.
Code of WC_REST_Product_Attributes_V1_Controller::create_item() WC REST Product Attributes V1 Controller::create item WC 5.0.0
public function create_item( $request ) {
global $wpdb;
$id = wc_create_attribute( array(
'name' => $request['name'],
'slug' => wc_sanitize_taxonomy_name( stripslashes( $request['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;
}