Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API - Attribute exists check and return ID #12

Closed
danielbarenkamp opened this issue Jul 19, 2019 · 1 comment
Closed

API - Attribute exists check and return ID #12

danielbarenkamp opened this issue Jul 19, 2019 · 1 comment
Labels
enhancement Improvements that slightly enhance existing functionality and are fast to implement.

Comments

@danielbarenkamp
Copy link

Is your feature request related to a problem? Please describe.
The API is not returning the Attribute ID if it existing already. So when you are importing a product via the API and add attributes, you need to know the exact attribute ID.

Describe the solution you'd like
The API should check and return if an attribute is existing or has been created while importing products before.

What we did is first modify the /includes/wc-attribute-function.php and added 2 lines to return the existing attribute_id:

$attribute_id = $wpdb->get_var("select attribute_id from {$wpdb->prefix}woocommerce_attribute_taxonomies where attribute_name='" . $slug . "'");
return new WP_Error( 'invalid_product_attribute_slug_already_exists', sprintf( __( 'Slug "%s" is already in use. Change it, please.', 'woocommerce' ), $slug ), array( 'status' => 400, 'resource_id' => $attribute_id ) );

Bildschirmfoto 2019-07-19 um 08 55 52

Then, because the API function does not return the "resource_id" we added the following lines in 251:

// Checks for errors.
if ( is_wp_error( $id ) ) {
	if(isset($id->error_data['invalid_product_attribute_slug_already_exists'])) {
		return new WP_Error( 'woocommerce_rest_cannot_create', $id->get_error_message(), array( 'status' => 400, 'resource_id' => $id->error_data['invalid_product_attribute_slug_already_exists']['resource_id']) );	
	} else {
		return new WP_Error( 'woocommerce_rest_cannot_create', $id->get_error_message(), array( 'status' => 400 ) );	
	}
}

Bildschirmfoto 2019-07-19 um 08 58 40

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

@claudiosanches claudiosanches transferred this issue from woocommerce/woocommerce Jul 31, 2019
@mikejolley mikejolley added the enhancement Improvements that slightly enhance existing functionality and are fast to implement. label Aug 1, 2019
@vedanshujain
Copy link
Contributor

The requirement of knowing the exact product attribute is intended because you can have a product level attribute as well as a global level attribute which may be different. So if you want to link a global attribute, then it does make sense to have attribute ID as a requirement. I am closing this issue, but feel free to write back if you have questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvements that slightly enhance existing functionality and are fast to implement.
Projects
None yet
Development

No branches or pull requests

3 participants