Created
August 20, 2015 14:17
-
-
Save geesen/683f578878905a2043cd to your computer and use it in GitHub Desktop.
woocommerce create product categories via RST Api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//.... | |
public function register_routes( $routes ) { | |
//.... | |
# GET/POST /products/categories | |
$routes[ $this->base . '/categories' ] = array( | |
array( array( $this, 'get_product_categories' ), WC_API_Server::READABLE ), | |
array( array( $this, 'create_product_categories' ), WC_API_SERVER::CREATABLE | WC_API_Server::ACCEPT_DATA ), | |
); | |
# GET/PUT/DELETE /products/categories/<id> | |
$routes[ $this->base . '/categories/(?P<id>\d+)' ] = array( | |
array( array( $this, 'get_product_category' ), WC_API_Server::READABLE ), | |
array( array( $this, 'delete_product_category' ), WC_API_Server::DELETABLE ), | |
); | |
//.... | |
} | |
//... | |
public function create_product_categories( $data ) { | |
global $wpdb; | |
try { | |
if ( ! isset( $data['product_category'] ) ) { | |
throw new WC_API_Exception( 'woocommerce_api_missing_product_attribute_data', sprintf( __( 'No %1$s data specified to create %1$s', 'woocommerce' ), 'product_category' ), 400 ); | |
} | |
$data = $data['product_category']; | |
// Check permissions | |
if ( ! current_user_can( 'manage_product_terms' ) ) { | |
throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_product_attribute', __( 'You do not have permission to create product categories', 'woocommerce' ), 401 ); | |
} | |
$data = apply_filters( 'woocommerce_api_create_product_category_data', $data, $this ); | |
if ( ! isset( $data['name'] ) ) { | |
$data['name'] = ''; | |
} | |
// Set the category slug | |
if ( ! isset( $data['slug'] ) ) { | |
$data['slug'] = wc_sanitize_taxonomy_name( stripslashes( $data['name'] ) ); | |
} else { | |
$data['slug'] = preg_replace( '/^pa\_/', '', wc_sanitize_taxonomy_name( stripslashes( $data['slug'] ) ) ); | |
} | |
// Set taxonomy type | |
if ( ! isset( $data['type'] ) ) { | |
$data['type'] = 'product_cat'; | |
} | |
// Set values to defaults when not set | |
if ( ! isset( $data['parent'] ) ) { | |
$data['parent'] = 0; | |
} | |
if ( ! isset( $data['display'] ) ) { | |
$data['display'] = 'default'; | |
} | |
if ( ! isset( $data['image'] ) ) { | |
$data['image'] = ""; | |
} | |
if ( ! isset( $data['description'] ) ) { | |
$data['description'] = ""; | |
} | |
// Validate the category data | |
//$this->validate_attribute_data( $data['name'], $data['slug'], $data['type'], $data['order_by'], true ); | |
$ids = wp_insert_term($data['name'], 'product_cat', array( | |
'description'=> $data['description'], | |
'slug' => $data['slug'], | |
'parent'=> $data['parent'] | |
)); | |
if ( is_wp_error( $ids ) ) { | |
throw new WC_API_Exception( 'woocommerce_api_cannot_create_product_category', $ids->get_error_message(), 400 ); | |
} | |
$term_id = $ids['term_id']; | |
$term_taxonomy_id = $ids['term_taxonomy_id']; | |
// add thumbnail | |
if(isset($data['image']) && strlen($data['image'])>0){ | |
//TODO: use image files instead of post ids | |
$meta_value = intval($data['image']); | |
$insert = $wpdb->insert( | |
$wpdb->prefix . 'woocommerce_termmeta', | |
array( | |
'woocommerce_term_id' => $term_id, | |
'meta_key' => 'thumbnail_id', | |
'meta_value' => $meta_value | |
), | |
array( '%d', '%s', '%d') | |
); | |
// Checks for an error | |
if ( is_wp_error( $insert ) ) { | |
throw new WC_API_Exception( 'woocommerce_api_cannot_create_product_category', $insert->get_error_message(), 400 ); | |
} | |
$id = $wpdb->insert_id; | |
} | |
// add display | |
if(isset($data['display'])){ | |
//TODO: use image files | |
$meta_value = $data['display']; | |
$insert = $wpdb->insert( | |
$wpdb->prefix . 'woocommerce_termmeta', | |
array( | |
'woocommerce_term_id' => $term_id, | |
'meta_key' => 'display_type', | |
'meta_value' => $meta_value | |
), | |
array( '%d', '%s', '%d') | |
); | |
// Checks for an error | |
if ( is_wp_error( $insert ) ) { | |
throw new WC_API_Exception( 'woocommerce_api_cannot_create_product_category', $insert->get_error_message(), 400 ); | |
} | |
$id = $wpdb->insert_id; | |
} | |
do_action( 'woocommerce_api_create_product_category', $term_id, $data ); | |
// Clear transients | |
//delete_transient( 'wc_attribute_taxonomies' ); | |
$this->server->send_status( 201 ); | |
return $this->get_product_category( $term_id ); | |
} catch ( WC_API_Exception $e ) { | |
echo $e->getMessage(); | |
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); | |
} | |
} | |
public function delete_product_category( $id ) { | |
global $wpdb; | |
try { | |
// Check permissions | |
if ( ! current_user_can( 'manage_product_terms' ) ) { | |
throw new WC_API_Exception( 'woocommerce_api_user_cannot_delete_product_category', __( 'You do not have permission to delete product category', 'woocommerce' ), 401 ); | |
} | |
$id = absint( $id ); | |
$term = term_exists($id, 'product_cat'); | |
if ($term == 0 || $term == null) { | |
throw new WC_API_Exception( 'woocommerce_api_invalid_product_category_id', __( 'A product category with the provided ID could not be found', 'woocommerce' ), 404 ); | |
} | |
$term_id = $term['term_id']; | |
$deleted = $wpdb->delete( | |
$wpdb->prefix . 'woocommerce_termmeta', | |
array( 'woocommerce_term_id' => $term_id ), | |
array( '%d' ) | |
); | |
if ( false === $deleted ) { | |
throw new WC_API_Exception( 'woocommerce_api_cannot_delete_product_category', __( 'Could not delete the category', 'woocommerce' ), 401 ); | |
} | |
$result = wp_delete_term( $term_id, 'product_cat'); | |
if ( is_wp_error( $result ) || is_null( $result ) || !$result ) { | |
throw new WC_API_Exception( 'woocommerce_api_cannot_delete_product_category', __( 'Could not delete the category', 'woocommerce' ), 401 ); | |
} | |
do_action( 'woocommerce_product_category_deleted', $id); | |
do_action( 'woocommerce_api_delete_product_category', $id, $this ); | |
// Clear transients | |
delete_transient( 'wc_product_category' ); | |
return array( 'message' => sprintf( __( 'Deleted %s', 'woocommerce' ), 'product_category' ) ); | |
} catch ( WC_API_Exception $e ) { | |
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); | |
} | |
} |
Hi @alizainprasla,
I forked woocommerce and changed the file.
I don't think that it's possible to extend the rest endpoint by using actions.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @geesen,
woocommerce/woocommerce#8879
I followed you from this post.
Can you please tell me how you extend woocommerce rest api?
What i have tried to make it happen. i create a plugin in which i add action like :
add_action('woocommerce_api_create_customer', 'create_custom_customer');