WC_API_Products::edit_product_attribute()
Edit a product attribute.
Method of the class: WC_API_Products{}
Hooks from the method
Return
Array|WP_Error
.
Usage
$WC_API_Products = new WC_API_Products(); $WC_API_Products->edit_product_attribute( $id, $data );
- $id(int) (required)
- the attribute ID.
- $data(array) (required)
- -
Changelog
Since 2.5.0 | Introduced. |
WC_API_Products::edit_product_attribute() WC API Products::edit product attribute code WC 8.7.0
public function edit_product_attribute( $id, $data ) { global $wpdb; try { if ( ! isset( $data['product_attribute'] ) ) { throw new WC_API_Exception( 'woocommerce_api_missing_product_attribute_data', sprintf( __( 'No %1$s data specified to edit %1$s', 'woocommerce' ), 'product_attribute' ), 400 ); } $id = absint( $id ); $data = $data['product_attribute']; // Check permissions. if ( ! current_user_can( 'manage_product_terms' ) ) { throw new WC_API_Exception( 'woocommerce_api_user_cannot_edit_product_attribute', __( 'You do not have permission to edit product attributes', 'woocommerce' ), 401 ); } $data = apply_filters( 'woocommerce_api_edit_product_attribute_data', $data, $this ); $attribute = $this->get_product_attribute( $id ); if ( is_wp_error( $attribute ) ) { return $attribute; } $attribute_name = isset( $data['name'] ) ? $data['name'] : $attribute['product_attribute']['name']; $attribute_type = isset( $data['type'] ) ? $data['type'] : $attribute['product_attribute']['type']; $attribute_order_by = isset( $data['order_by'] ) ? $data['order_by'] : $attribute['product_attribute']['order_by']; if ( isset( $data['slug'] ) ) { $attribute_slug = wc_sanitize_taxonomy_name( stripslashes( $data['slug'] ) ); } else { $attribute_slug = $attribute['product_attribute']['slug']; } $attribute_slug = preg_replace( '/^pa\_/', '', $attribute_slug ); if ( isset( $data['has_archives'] ) ) { $attribute_public = true === $data['has_archives'] ? 1 : 0; } else { $attribute_public = $attribute['product_attribute']['has_archives']; } // Validate the attribute data. $this->validate_attribute_data( $attribute_name, $attribute_slug, $attribute_type, $attribute_order_by, false ); $update = $wpdb->update( $wpdb->prefix . 'woocommerce_attribute_taxonomies', array( 'attribute_label' => $attribute_name, 'attribute_name' => $attribute_slug, 'attribute_type' => $attribute_type, 'attribute_orderby' => $attribute_order_by, 'attribute_public' => $attribute_public, ), array( 'attribute_id' => $id ), array( '%s', '%s', '%s', '%s', '%d' ), array( '%d' ) ); // Checks for an error in the product creation. if ( false === $update ) { throw new WC_API_Exception( 'woocommerce_api_cannot_edit_product_attribute', __( 'Could not edit the attribute', 'woocommerce' ), 400 ); } do_action( 'woocommerce_api_edit_product_attribute', $id, $data ); // Clear transients. wp_schedule_single_event( time(), 'woocommerce_flush_rewrite_rules' ); delete_transient( 'wc_attribute_taxonomies' ); WC_Cache_Helper::invalidate_cache_group( 'woocommerce-attributes' ); return $this->get_product_attribute( $id ); } catch ( WC_API_Exception $e ) { return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); } }