2013-11-04 06:36:31 +00:00
< ? php
/**
* WooCommerce API Products Class
*
* Handles requests to the / products endpoint
*
* @ author WooThemes
* @ category API
* @ package WooCommerce / API
* @ since 2.1
*/
2014-06-26 19:55:24 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
exit ; // Exit if accessed directly
}
2013-11-04 06:36:31 +00:00
2013-11-09 21:20:23 +00:00
class WC_API_Products extends WC_API_Resource {
2013-11-04 06:36:31 +00:00
/** @var string $base the route base */
protected $base = '/products' ;
/**
* Register the routes for this class
*
2014-06-24 20:16:08 +00:00
* GET / POST / products
2013-11-04 06:36:31 +00:00
* GET / products / count
2014-06-24 20:28:36 +00:00
* GET / PUT / DELETE / products /< id >
2013-11-04 06:36:31 +00:00
* GET / products /< id >/ reviews
*
* @ since 2.1
* @ param array $routes
* @ return array
*/
2013-11-11 00:29:36 +00:00
public function register_routes ( $routes ) {
2013-11-04 06:36:31 +00:00
2014-06-24 17:42:10 +00:00
# GET/POST /products
2013-11-04 06:36:31 +00:00
$routes [ $this -> base ] = array (
2014-06-24 17:42:10 +00:00
array ( array ( $this , 'get_products' ), WC_API_Server :: READABLE ),
array ( array ( $this , 'create_product' ), WC_API_SERVER :: CREATABLE | WC_API_Server :: ACCEPT_DATA ),
2013-11-04 06:36:31 +00:00
);
# GET /products/count
$routes [ $this -> base . '/count' ] = array (
2013-11-11 00:29:36 +00:00
array ( array ( $this , 'get_products_count' ), WC_API_Server :: READABLE ),
2013-11-04 06:36:31 +00:00
);
2014-06-24 20:28:36 +00:00
# GET/PUT/DELETE /products/<id>
2013-11-04 06:36:31 +00:00
$routes [ $this -> base . '/(?P<id>\d+)' ] = array (
2014-06-24 20:16:08 +00:00
array ( array ( $this , 'get_product' ), WC_API_Server :: READABLE ),
2014-06-24 20:34:06 +00:00
array ( array ( $this , 'edit_product' ), WC_API_Server :: EDITABLE | WC_API_Server :: ACCEPT_DATA ),
2014-06-24 20:16:08 +00:00
array ( array ( $this , 'delete_product' ), WC_API_Server :: DELETABLE ),
2013-11-04 06:36:31 +00:00
);
# GET /products/<id>/reviews
$routes [ $this -> base . '/(?P<id>\d+)/reviews' ] = array (
2013-11-11 00:29:36 +00:00
array ( array ( $this , 'get_product_reviews' ), WC_API_Server :: READABLE ),
2013-11-04 06:36:31 +00:00
);
2015-03-26 20:53:20 +00:00
# GET /products/<id>/orders
$routes [ $this -> base . '/(?P<id>\d+)/orders' ] = array (
array ( array ( $this , 'get_product_orders' ), WC_API_Server :: READABLE ),
);
2015-09-10 06:33:05 +00:00
# GET/POST /products/categories
2014-07-28 23:01:50 +00:00
$routes [ $this -> base . '/categories' ] = array (
array ( array ( $this , 'get_product_categories' ), WC_API_Server :: READABLE ),
2015-09-10 06:33:05 +00:00
array ( array ( $this , 'create_product_category' ), WC_API_Server :: CREATABLE | WC_API_Server :: ACCEPT_DATA ),
2014-07-28 23:01:50 +00:00
);
2015-09-10 06:33:05 +00:00
# GET/PUT/DELETE /products/categories/<id>
2014-07-28 23:01:50 +00:00
$routes [ $this -> base . '/categories/(?P<id>\d+)' ] = array (
array ( array ( $this , 'get_product_category' ), WC_API_Server :: READABLE ),
2015-09-10 06:33:05 +00:00
array ( array ( $this , 'edit_product_category' ), WC_API_Server :: EDITABLE | WC_API_Server :: ACCEPT_DATA ),
array ( array ( $this , 'delete_product_category' ), WC_API_Server :: DELETABLE ),
2014-07-28 23:01:50 +00:00
);
2015-09-10 09:58:48 +00:00
# GET/POST /products/tags
$routes [ $this -> base . '/tags' ] = array (
array ( array ( $this , 'get_product_tags' ), WC_API_Server :: READABLE ),
array ( array ( $this , 'create_product_tag' ), WC_API_Server :: CREATABLE | WC_API_Server :: ACCEPT_DATA ),
);
# GET/PUT/DELETE /products/tags/<id>
$routes [ $this -> base . '/tags/(?P<id>\d+)' ] = array (
array ( array ( $this , 'get_product_tag' ), WC_API_Server :: READABLE ),
array ( array ( $this , 'edit_product_tag' ), WC_API_Server :: EDITABLE | WC_API_Server :: ACCEPT_DATA ),
array ( array ( $this , 'delete_product_tag' ), WC_API_Server :: DELETABLE ),
);
2015-09-09 13:38:11 +00:00
# GET/POST /products/shipping_classes
$routes [ $this -> base . '/shipping_classes' ] = array (
array ( array ( $this , 'get_product_shipping_classes' ), WC_API_Server :: READABLE ),
array ( array ( $this , 'create_product_shipping_class' ), WC_API_Server :: CREATABLE | WC_API_Server :: ACCEPT_DATA ),
);
# GET/PUT/DELETE /products/shipping_classes/<id>
$routes [ $this -> base . '/shipping_classes/(?P<id>\d+)' ] = array (
array ( array ( $this , 'get_product_shipping_class' ), WC_API_Server :: READABLE ),
array ( array ( $this , 'edit_product_shipping_class' ), WC_API_Server :: EDITABLE | WC_API_Server :: ACCEPT_DATA ),
array ( array ( $this , 'delete_product_shipping_class' ), WC_API_Server :: DELETABLE ),
2014-07-28 23:01:50 +00:00
);
2015-04-27 16:06:42 +00:00
# GET/POST /products/attributes
2015-04-27 14:47:17 +00:00
$routes [ $this -> base . '/attributes' ] = array (
array ( array ( $this , 'get_product_attributes' ), WC_API_Server :: READABLE ),
2015-04-27 16:06:42 +00:00
array ( array ( $this , 'create_product_attribute' ), WC_API_SERVER :: CREATABLE | WC_API_Server :: ACCEPT_DATA ),
2015-04-27 14:47:17 +00:00
);
2015-12-04 18:02:31 +00:00
# GET/PUT/DELETE /products/attributes/<id>
2015-04-27 14:58:58 +00:00
$routes [ $this -> base . '/attributes/(?P<id>\d+)' ] = array (
array ( array ( $this , 'get_product_attribute' ), WC_API_Server :: READABLE ),
2015-04-27 19:14:31 +00:00
array ( array ( $this , 'edit_product_attribute' ), WC_API_Server :: EDITABLE | WC_API_Server :: ACCEPT_DATA ),
2015-04-27 19:33:47 +00:00
array ( array ( $this , 'delete_product_attribute' ), WC_API_Server :: DELETABLE ),
2015-04-27 14:58:58 +00:00
);
2015-12-04 18:02:31 +00:00
# GET/POST /products/attributes/<attribute_id>/terms
$routes [ $this -> base . '/attributes/(?P<attribute_id>\d+)/terms' ] = array (
array ( array ( $this , 'get_product_attribute_terms' ), WC_API_Server :: READABLE ),
2015-12-04 18:38:17 +00:00
array ( array ( $this , 'create_product_attribute_term' ), WC_API_SERVER :: CREATABLE | WC_API_Server :: ACCEPT_DATA ),
2015-12-04 18:02:31 +00:00
);
2015-12-04 18:14:57 +00:00
# GET/PUT/DELETE /products/attributes/<attribute_id>/terms/<id>
$routes [ $this -> base . '/attributes/(?P<attribute_id>\d+)/terms/(?P<id>\d+)' ] = array (
array ( array ( $this , 'get_product_attribute_term' ), WC_API_Server :: READABLE ),
2015-12-04 18:53:17 +00:00
array ( array ( $this , 'edit_product_attribute_term' ), WC_API_Server :: EDITABLE | WC_API_Server :: ACCEPT_DATA ),
2015-12-04 19:01:49 +00:00
array ( array ( $this , 'delete_product_attribute_term' ), WC_API_Server :: DELETABLE ),
2015-12-04 18:14:57 +00:00
);
2015-04-24 21:43:33 +00:00
# POST|PUT /products/bulk
2015-04-09 20:48:51 +00:00
$routes [ $this -> base . '/bulk' ] = array (
2015-04-24 21:43:33 +00:00
array ( array ( $this , 'bulk' ), WC_API_Server :: EDITABLE | WC_API_Server :: ACCEPT_DATA ),
2015-04-09 20:48:51 +00:00
);
2014-11-27 12:34:55 +00:00
2013-11-04 06:36:31 +00:00
return $routes ;
}
2015-04-24 21:43:33 +00:00
2013-11-04 06:36:31 +00:00
/**
* Get all products
*
* @ since 2.1
2013-11-11 00:29:36 +00:00
* @ param string $fields
2013-11-04 06:36:31 +00:00
* @ param string $type
2013-11-11 00:29:36 +00:00
* @ param array $filter
2013-11-19 02:06:45 +00:00
* @ param int $page
2013-11-04 06:36:31 +00:00
* @ return array
*/
2013-11-19 02:06:45 +00:00
public function get_products ( $fields = null , $type = null , $filter = array (), $page = 1 ) {
2013-11-11 00:29:36 +00:00
2014-06-26 19:55:24 +00:00
if ( ! empty ( $type ) ) {
2013-11-11 00:29:36 +00:00
$filter [ 'type' ] = $type ;
2014-06-26 19:55:24 +00:00
}
2013-11-04 06:36:31 +00:00
2013-11-19 02:06:45 +00:00
$filter [ 'page' ] = $page ;
2013-11-11 00:29:36 +00:00
$query = $this -> query_products ( $filter );
2013-11-04 06:36:31 +00:00
$products = array ();
2014-06-26 19:55:24 +00:00
foreach ( $query -> posts as $product_id ) {
2013-11-04 06:36:31 +00:00
2014-06-26 19:55:24 +00:00
if ( ! $this -> is_readable ( $product_id ) ) {
2013-11-11 00:29:36 +00:00
continue ;
2014-06-26 19:55:24 +00:00
}
2013-11-11 00:29:36 +00:00
2013-11-22 08:41:32 +00:00
$products [] = current ( $this -> get_product ( $product_id , $fields ) );
2013-11-04 06:36:31 +00:00
}
2013-11-19 02:06:45 +00:00
$this -> server -> add_pagination_headers ( $query );
2013-11-11 00:29:36 +00:00
2013-11-04 06:36:31 +00:00
return array ( 'products' => $products );
}
/**
* Get the product for the given ID
*
* @ since 2.1
* @ param int $id the product ID
* @ param string $fields
* @ return array
*/
2013-11-11 00:29:36 +00:00
public function get_product ( $id , $fields = null ) {
2013-11-04 06:36:31 +00:00
2013-11-11 00:29:36 +00:00
$id = $this -> validate_request ( $id , 'product' , 'read' );
2013-11-04 06:36:31 +00:00
2014-06-26 19:55:24 +00:00
if ( is_wp_error ( $id ) ) {
2013-11-11 00:29:36 +00:00
return $id ;
2014-06-26 19:55:24 +00:00
}
2013-11-04 06:36:31 +00:00
2014-08-19 10:09:29 +00:00
$product = wc_get_product ( $id );
2013-11-04 06:36:31 +00:00
2013-11-14 17:40:35 +00:00
// add data that applies to every product type
$product_data = $this -> get_product_data ( $product );
// add variations to variable products
if ( $product -> is_type ( 'variable' ) && $product -> has_child () ) {
$product_data [ 'variations' ] = $this -> get_variation_data ( $product );
}
// add the parent product data to an individual variation
2015-07-30 21:32:24 +00:00
if ( $product -> is_type ( 'variation' ) && $product -> parent ) {
2013-11-14 17:40:35 +00:00
$product_data [ 'parent' ] = $this -> get_product_data ( $product -> parent );
}
2013-11-04 06:36:31 +00:00
2015-10-01 12:08:42 +00:00
// Add grouped products data
if ( $product -> is_type ( 'grouped' ) && $product -> has_child () ) {
$product_data [ 'grouped_products' ] = $this -> get_grouped_products_data ( $product );
}
if ( $product -> is_type ( 'simple' ) && ! empty ( $product -> post -> post_parent ) ) {
$_product = wc_get_product ( $product -> post -> post_parent );
$product_data [ 'parent' ] = $this -> get_product_data ( $_product );
}
2013-11-22 08:41:32 +00:00
return array ( 'product' => apply_filters ( 'woocommerce_api_product_response' , $product_data , $product , $fields , $this -> server ) );
2013-11-04 06:36:31 +00:00
}
/**
2014-07-28 03:23:59 +00:00
* Get the total number of products
2013-11-04 06:36:31 +00:00
*
* @ since 2.1
* @ param string $type
2013-11-11 00:29:36 +00:00
* @ param array $filter
2013-11-04 06:36:31 +00:00
* @ return array
*/
2013-11-11 00:29:36 +00:00
public function get_products_count ( $type = null , $filter = array () ) {
2015-01-05 19:26:24 +00:00
try {
if ( ! current_user_can ( 'read_private_products' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_read_products_count' , __ ( 'You do not have permission to read the products count' , 'woocommerce' ), 401 );
}
2013-11-04 06:36:31 +00:00
2015-01-05 19:26:24 +00:00
if ( ! empty ( $type ) ) {
$filter [ 'type' ] = $type ;
}
2013-11-04 06:36:31 +00:00
2015-01-05 19:26:24 +00:00
$query = $this -> query_products ( $filter );
2013-11-04 06:36:31 +00:00
2015-01-05 19:26:24 +00:00
return array ( 'count' => ( int ) $query -> found_posts );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
2013-11-04 06:36:31 +00:00
}
2014-06-24 17:42:10 +00:00
/**
2015-12-17 10:08:17 +00:00
* Create a new product .
2014-06-24 17:42:10 +00:00
*
* @ since 2.2
* @ param array $data posted data
* @ return array
*/
public function create_product ( $data ) {
2015-01-05 19:26:24 +00:00
$id = 0 ;
2014-06-24 17:42:10 +00:00
2015-01-05 19:26:24 +00:00
try {
2015-04-13 16:06:19 +00:00
if ( ! isset ( $data [ 'product' ] ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_product_data' , sprintf ( __ ( 'No %1$s data specified to create %1$s' , 'woocommerce' ), 'product' ), 400 );
}
$data = $data [ 'product' ];
2014-08-30 19:50:40 +00:00
2015-12-17 10:08:17 +00:00
// Check permissions.
2015-01-05 19:26:24 +00:00
if ( ! current_user_can ( 'publish_products' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_create_product' , __ ( 'You do not have permission to create products' , 'woocommerce' ), 401 );
}
2014-06-24 17:42:10 +00:00
2015-01-05 19:26:24 +00:00
$data = apply_filters ( 'woocommerce_api_create_product_data' , $data , $this );
2014-06-24 17:42:10 +00:00
2015-12-17 10:08:17 +00:00
// Check if product title is specified.
2015-01-05 19:26:24 +00:00
if ( ! isset ( $data [ 'title' ] ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_product_title' , sprintf ( __ ( 'Missing parameter %s' , 'woocommerce' ), 'title' ), 400 );
}
2014-06-24 17:42:10 +00:00
2015-12-17 10:08:17 +00:00
// Check product type.
2015-01-05 19:26:24 +00:00
if ( ! isset ( $data [ 'type' ] ) ) {
$data [ 'type' ] = 'simple' ;
}
2014-06-24 17:42:10 +00:00
2015-12-17 10:08:17 +00:00
// Set visible visibility when not sent.
2015-01-30 13:00:57 +00:00
if ( ! isset ( $data [ 'catalog_visibility' ] ) ) {
2015-01-30 13:08:17 +00:00
$data [ 'catalog_visibility' ] = 'visible' ;
2015-01-30 13:00:57 +00:00
}
2015-12-17 10:08:17 +00:00
// Validate the product type.
2015-01-05 19:26:24 +00:00
if ( ! in_array ( wc_clean ( $data [ 'type' ] ), array_keys ( wc_get_product_types () ) ) ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_product_type' , sprintf ( __ ( 'Invalid product type - the product type must be any of these: %s' , 'woocommerce' ), implode ( ', ' , array_keys ( wc_get_product_types () ) ) ), 400 );
}
2015-01-13 15:24:28 +00:00
2015-01-19 12:52:44 +00:00
// Enable description html tags.
$post_content = isset ( $data [ 'description' ] ) ? wc_clean ( $data [ 'description' ] ) : '' ;
2015-05-06 17:28:57 +00:00
if ( $post_content && isset ( $data [ 'enable_html_description' ] ) && true === $data [ 'enable_html_description' ] ) {
2015-01-19 12:52:44 +00:00
$post_content = $data [ 'description' ];
}
2014-06-24 17:42:10 +00:00
2015-01-19 12:52:44 +00:00
// Enable short description html tags.
$post_excerpt = isset ( $data [ 'short_description' ] ) ? wc_clean ( $data [ 'short_description' ] ) : '' ;
2015-05-06 17:28:57 +00:00
if ( $post_excerpt && isset ( $data [ 'enable_html_short_description' ] ) && true === $data [ 'enable_html_short_description' ] ) {
2015-01-19 12:52:44 +00:00
$post_excerpt = $data [ 'short_description' ];
}
2015-01-13 15:24:28 +00:00
2015-01-05 19:26:24 +00:00
$new_product = array (
'post_title' => wc_clean ( $data [ 'title' ] ),
2015-12-17 10:08:17 +00:00
'post_status' => isset ( $data [ 'status' ] ) ? wc_clean ( $data [ 'status' ] ) : 'publish' ,
2015-01-05 19:26:24 +00:00
'post_type' => 'product' ,
2015-12-17 10:08:17 +00:00
'post_excerpt' => isset ( $data [ 'short_description' ] ) ? $post_excerpt : '' ,
'post_content' => isset ( $data [ 'description' ] ) ? $post_content : '' ,
2015-01-05 19:26:24 +00:00
'post_author' => get_current_user_id (),
2015-12-17 10:08:17 +00:00
'menu_order' => isset ( $data [ 'menu_order' ] ) ? intval ( $data [ 'menu_order' ] ) : 0 ,
2015-01-05 19:26:24 +00:00
);
2014-06-24 18:19:34 +00:00
2015-12-17 10:08:17 +00:00
// Attempts to create the new product.
2015-01-05 19:26:24 +00:00
$id = wp_insert_post ( $new_product , true );
2014-11-25 14:10:12 +00:00
2015-12-17 10:08:17 +00:00
// Checks for an error in the product creation.
2015-01-05 19:26:24 +00:00
if ( is_wp_error ( $id ) ) {
throw new WC_API_Exception ( 'woocommerce_api_cannot_create_product' , $id -> get_error_message (), 400 );
2014-06-24 18:19:34 +00:00
}
2014-11-25 14:10:12 +00:00
2015-12-17 10:08:17 +00:00
// Check for featured/gallery images, upload it and set it.
2015-01-05 19:26:24 +00:00
if ( isset ( $data [ 'images' ] ) ) {
$this -> save_product_images ( $id , $data [ 'images' ] );
}
2014-06-24 18:30:11 +00:00
2015-12-17 10:08:17 +00:00
// Save product meta fields.
2015-01-05 19:26:24 +00:00
$this -> save_product_meta ( $id , $data );
2014-06-26 19:47:54 +00:00
2015-12-17 10:08:17 +00:00
// Save variations.
2015-01-05 19:26:24 +00:00
if ( isset ( $data [ 'type' ] ) && 'variable' == $data [ 'type' ] && isset ( $data [ 'variations' ] ) && is_array ( $data [ 'variations' ] ) ) {
$this -> save_variations ( $id , $data );
2014-06-26 19:47:54 +00:00
}
2015-01-05 19:26:24 +00:00
do_action ( 'woocommerce_api_create_product' , $id , $data );
2014-06-26 19:51:03 +00:00
2015-12-17 10:08:17 +00:00
// Clear cache/transients.
2015-01-05 19:26:24 +00:00
wc_delete_product_transients ( $id );
2014-06-26 19:47:54 +00:00
2015-01-05 19:26:24 +00:00
$this -> server -> send_status ( 201 );
2014-06-24 17:42:10 +00:00
2015-01-05 19:26:24 +00:00
return $this -> get_product ( $id );
} catch ( WC_API_Exception $e ) {
2015-12-17 10:08:17 +00:00
// Remove the product when fails.
2015-07-10 09:48:29 +00:00
$this -> clear_product ( $id );
2015-07-10 13:49:18 +00:00
2015-01-05 19:26:24 +00:00
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
2014-06-24 17:42:10 +00:00
}
2013-11-04 06:36:31 +00:00
/**
* Edit a product
*
2014-06-26 19:55:24 +00:00
* @ since 2.2
2013-11-04 06:36:31 +00:00
* @ param int $id the product ID
* @ param array $data
* @ return array
*/
2013-11-11 00:29:36 +00:00
public function edit_product ( $id , $data ) {
2015-01-05 19:26:24 +00:00
try {
2015-04-13 16:06:19 +00:00
if ( ! isset ( $data [ 'product' ] ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_product_data' , sprintf ( __ ( 'No %1$s data specified to edit %1$s' , 'woocommerce' ), 'product' ), 400 );
}
$data = $data [ 'product' ];
2013-11-11 00:29:36 +00:00
2015-01-05 19:26:24 +00:00
$id = $this -> validate_request ( $id , 'product' , 'edit' );
2014-08-30 19:50:40 +00:00
2015-01-05 19:26:24 +00:00
if ( is_wp_error ( $id ) ) {
return $id ;
}
2014-06-24 20:34:06 +00:00
2015-01-05 19:26:24 +00:00
$data = apply_filters ( 'woocommerce_api_edit_product_data' , $data , $this );
2014-10-23 10:58:39 +00:00
2015-01-05 19:26:24 +00:00
// Product title.
if ( isset ( $data [ 'title' ] ) ) {
wp_update_post ( array ( 'ID' => $id , 'post_title' => wc_clean ( $data [ 'title' ] ) ) );
}
2014-06-24 20:34:06 +00:00
2015-01-05 19:26:24 +00:00
// Product name (slug).
if ( isset ( $data [ 'name' ] ) ) {
wp_update_post ( array ( 'ID' => $id , 'post_name' => sanitize_title ( $data [ 'name' ] ) ) );
}
2014-06-24 20:34:06 +00:00
2015-01-05 19:26:24 +00:00
// Product status.
if ( isset ( $data [ 'status' ] ) ) {
wp_update_post ( array ( 'ID' => $id , 'post_status' => wc_clean ( $data [ 'status' ] ) ) );
}
2014-06-24 20:34:06 +00:00
2015-01-05 19:26:24 +00:00
// Product short description.
if ( isset ( $data [ 'short_description' ] ) ) {
2015-02-19 12:48:21 +00:00
// Enable short description html tags.
2015-05-06 17:28:57 +00:00
$post_excerpt = ( isset ( $data [ 'enable_html_short_description' ] ) && true === $data [ 'enable_html_short_description' ] ) ? $data [ 'short_description' ] : wc_clean ( $data [ 'short_description' ] );
2015-02-19 12:48:21 +00:00
2015-01-09 12:12:47 +00:00
wp_update_post ( array ( 'ID' => $id , 'post_excerpt' => $post_excerpt ) );
2015-01-05 19:26:24 +00:00
}
2014-06-25 16:27:48 +00:00
2015-01-05 19:26:24 +00:00
// Product description.
if ( isset ( $data [ 'description' ] ) ) {
2015-02-19 12:48:21 +00:00
// Enable description html tags.
2015-05-06 17:28:57 +00:00
$post_content = ( isset ( $data [ 'enable_html_description' ] ) && true === $data [ 'enable_html_description' ] ) ? $data [ 'description' ] : wc_clean ( $data [ 'description' ] );
2015-02-19 12:48:21 +00:00
2015-01-09 12:12:47 +00:00
wp_update_post ( array ( 'ID' => $id , 'post_content' => $post_content ) );
2015-01-05 19:26:24 +00:00
}
2014-06-24 20:28:36 +00:00
2015-12-17 10:08:17 +00:00
// Menu order.
if ( isset ( $data [ 'menu_order' ] ) ) {
wp_update_post ( array ( 'ID' => $id , 'menu_order' => intval ( $data [ 'menu_order' ] ) ) );
}
// Validate the product type.
2015-01-05 19:26:24 +00:00
if ( isset ( $data [ 'type' ] ) && ! in_array ( wc_clean ( $data [ 'type' ] ), array_keys ( wc_get_product_types () ) ) ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_product_type' , sprintf ( __ ( 'Invalid product type - the product type must be any of these: %s' , 'woocommerce' ), implode ( ', ' , array_keys ( wc_get_product_types () ) ) ), 400 );
2014-06-24 20:28:36 +00:00
}
2015-12-17 10:08:17 +00:00
// Check for featured/gallery images, upload it and set it.
2015-01-05 19:26:24 +00:00
if ( isset ( $data [ 'images' ] ) ) {
$this -> save_product_images ( $id , $data [ 'images' ] );
}
2013-11-04 06:36:31 +00:00
2015-12-17 10:08:17 +00:00
// Save product meta fields.
2015-01-05 19:26:24 +00:00
$this -> save_product_meta ( $id , $data );
2014-06-26 19:47:54 +00:00
2015-12-17 10:08:17 +00:00
// Save variations.
2015-10-22 19:19:16 +00:00
$product = get_product ( $id );
if ( $product -> is_type ( 'variable' ) ) {
if ( isset ( $data [ 'variations' ] ) && is_array ( $data [ 'variations' ] ) ) {
$this -> save_variations ( $id , $data );
} else {
// Just sync variations
WC_Product_Variable :: sync ( $id );
}
2014-06-26 19:47:54 +00:00
}
2015-01-05 19:26:24 +00:00
do_action ( 'woocommerce_api_edit_product' , $id , $data );
2014-06-26 19:51:03 +00:00
2015-12-17 10:08:17 +00:00
// Clear cache/transients.
2015-01-05 19:26:24 +00:00
wc_delete_product_transients ( $id );
2014-06-26 19:47:54 +00:00
2015-01-05 19:26:24 +00:00
return $this -> get_product ( $id );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
2013-11-04 06:36:31 +00:00
}
/**
2015-12-01 21:26:35 +00:00
* Delete a product .
2013-11-04 06:36:31 +00:00
*
2014-06-24 20:16:08 +00:00
* @ since 2.2
2015-12-01 21:26:35 +00:00
* @ param int $id the product ID .
* @ param bool $force true to permanently delete order , false to move to trash .
2013-11-04 06:36:31 +00:00
* @ return array
*/
2013-11-11 00:29:36 +00:00
public function delete_product ( $id , $force = false ) {
2013-11-04 06:36:31 +00:00
2013-11-11 00:29:36 +00:00
$id = $this -> validate_request ( $id , 'product' , 'delete' );
2014-06-24 20:16:08 +00:00
if ( is_wp_error ( $id ) ) {
2013-11-11 00:29:36 +00:00
return $id ;
2014-06-24 20:16:08 +00:00
}
2013-11-11 00:29:36 +00:00
2014-08-30 19:50:40 +00:00
do_action ( 'woocommerce_api_delete_product' , $id , $this );
2015-12-01 21:26:35 +00:00
$parent_id = wp_get_post_parent_id ( $id );
$result = ( $force ) ? wp_delete_post ( $id , true ) : wp_trash_post ( $id );
if ( ! $result ) {
return new WP_Error ( 'woocommerce_api_cannot_delete_product' , sprintf ( __ ( 'This %s cannot be deleted' , 'woocommerce' ), 'product' ), array ( 'status' => 500 ) );
}
// Delete parent product transients.
if ( $parent_id ) {
wc_delete_product_transients ( $parent_id );
}
if ( $force ) {
return array ( 'message' => sprintf ( __ ( 'Permanently deleted %s' , 'woocommerce' ), 'product' ) );
} else {
$this -> server -> send_status ( '202' );
return array ( 'message' => sprintf ( __ ( 'Deleted %s' , 'woocommerce' ), 'product' ) );
}
2013-11-04 06:36:31 +00:00
}
/**
* Get the reviews for a product
2013-11-14 17:40:35 +00:00
*
* @ since 2.1
* @ param int $id the product ID to get reviews for
* @ param string $fields fields to include in response
* @ return array
2013-11-04 06:36:31 +00:00
*/
2013-11-14 17:40:35 +00:00
public function get_product_reviews ( $id , $fields = null ) {
2013-11-11 00:29:36 +00:00
$id = $this -> validate_request ( $id , 'product' , 'read' );
2014-06-26 19:55:24 +00:00
if ( is_wp_error ( $id ) ) {
2013-11-11 00:29:36 +00:00
return $id ;
2014-06-26 19:55:24 +00:00
}
2013-11-11 00:29:36 +00:00
2015-03-24 17:38:08 +00:00
$comments = get_approved_comments ( $id );
$reviews = array ();
2013-11-04 06:36:31 +00:00
2013-11-14 17:40:35 +00:00
foreach ( $comments as $comment ) {
$reviews [] = array (
2015-01-23 00:04:03 +00:00
'id' => intval ( $comment -> comment_ID ),
2013-11-18 21:47:38 +00:00
'created_at' => $this -> server -> format_datetime ( $comment -> comment_date_gmt ),
2013-11-14 17:40:35 +00:00
'review' => $comment -> comment_content ,
'rating' => get_comment_meta ( $comment -> comment_ID , 'rating' , true ),
'reviewer_name' => $comment -> comment_author ,
'reviewer_email' => $comment -> comment_author_email ,
2015-10-09 16:23:30 +00:00
'verified' => wc_review_is_from_verified_owner ( $comment -> comment_ID ),
2013-11-14 17:40:35 +00:00
);
}
2013-11-19 02:59:13 +00:00
return array ( 'product_reviews' => apply_filters ( 'woocommerce_api_product_reviews_response' , $reviews , $id , $fields , $comments , $this -> server ) );
2013-11-04 06:36:31 +00:00
}
2015-03-26 20:53:20 +00:00
/**
* Get the orders for a product
*
2015-04-24 20:04:13 +00:00
* @ since 2.4 . 0
2015-03-26 20:53:20 +00:00
* @ param int $id the product ID to get orders for
2015-04-16 20:32:35 +00:00
* @ param string fields fields to retrieve
* @ param string $filter filters to include in response
2015-03-26 20:56:37 +00:00
* @ param string $status the order status to retrieve
* @ param $page $page page to retrieve
2015-03-26 20:53:20 +00:00
* @ return array
*/
2015-04-16 20:32:35 +00:00
public function get_product_orders ( $id , $fields = null , $filter = array (), $status = null , $page = 1 ) {
2015-04-24 20:04:13 +00:00
global $wpdb ;
2015-03-26 20:53:20 +00:00
$id = $this -> validate_request ( $id , 'product' , 'read' );
if ( is_wp_error ( $id ) ) {
return $id ;
}
2015-04-24 20:04:13 +00:00
$order_ids = $wpdb -> get_col ( $wpdb -> prepare ( "
SELECT order_id
FROM { $wpdb -> prefix } woocommerce_order_items
WHERE order_item_id IN ( SELECT order_item_id FROM { $wpdb -> prefix } woocommerce_order_itemmeta WHERE meta_key = '_product_id' AND meta_value = % d )
AND order_item_type = 'line_item'
" , $id ) );
2015-03-26 20:53:20 +00:00
2015-04-24 20:04:13 +00:00
if ( empty ( $order_ids ) ) {
return array ( 'orders' => array () );
}
2015-04-16 20:32:35 +00:00
2015-04-24 20:04:13 +00:00
$filter = array_merge ( $filter , array (
'in' => implode ( ',' , $order_ids )
) );
2015-03-26 20:53:20 +00:00
2015-04-24 20:04:13 +00:00
$orders = WC () -> api -> WC_API_Orders -> get_orders ( $fields , $filter , $status , $page );
2015-03-26 20:53:20 +00:00
2015-04-24 20:04:13 +00:00
return array ( 'orders' => apply_filters ( 'woocommerce_api_product_orders_response' , $orders [ 'orders' ], $id , $filter , $fields , $this -> server ) );
2015-03-26 20:53:20 +00:00
}
2014-07-28 23:01:50 +00:00
/**
* Get a listing of product categories
*
* @ since 2.2
2014-08-30 19:02:19 +00:00
* @ param string | null $fields fields to limit response to
2014-07-28 23:01:50 +00:00
* @ return array
*/
2014-08-30 19:02:19 +00:00
public function get_product_categories ( $fields = null ) {
2015-01-05 19:26:24 +00:00
try {
// Permissions check
if ( ! current_user_can ( 'manage_product_terms' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_read_product_categories' , __ ( 'You do not have permission to read product categories' , 'woocommerce' ), 401 );
}
2014-07-28 23:01:50 +00:00
2015-01-05 19:26:24 +00:00
$product_categories = array ();
2014-07-28 23:01:50 +00:00
2015-01-05 19:26:24 +00:00
$terms = get_terms ( 'product_cat' , array ( 'hide_empty' => false , 'fields' => 'ids' ) );
2014-07-28 23:01:50 +00:00
2015-01-05 19:26:24 +00:00
foreach ( $terms as $term_id ) {
$product_categories [] = current ( $this -> get_product_category ( $term_id , $fields ) );
}
2014-07-28 23:01:50 +00:00
2015-01-05 19:26:24 +00:00
return array ( 'product_categories' => apply_filters ( 'woocommerce_api_product_categories_response' , $product_categories , $terms , $fields , $this ) );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
2014-07-28 23:01:50 +00:00
}
}
/**
* Get the product category for the given ID
*
* @ since 2.2
* @ param string $id product category term ID
2014-08-30 19:02:19 +00:00
* @ param string | null $fields fields to limit response to
2014-07-28 23:01:50 +00:00
* @ return array
*/
2014-08-30 19:02:19 +00:00
public function get_product_category ( $id , $fields = null ) {
2015-01-05 19:26:24 +00:00
try {
$id = absint ( $id );
2014-07-28 23:01:50 +00:00
2015-01-05 19:26:24 +00:00
// Validate ID
if ( empty ( $id ) ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_product_category_id' , __ ( 'Invalid product category ID' , 'woocommerce' ), 400 );
}
2014-07-28 23:01:50 +00:00
2015-01-05 19:26:24 +00:00
// Permissions check
if ( ! current_user_can ( 'manage_product_terms' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_read_product_categories' , __ ( 'You do not have permission to read product categories' , 'woocommerce' ), 401 );
}
2014-07-28 23:01:50 +00:00
2015-01-05 19:26:24 +00:00
$term = get_term ( $id , 'product_cat' );
2014-07-28 23:01:50 +00:00
2015-01-05 19:26:24 +00:00
if ( is_wp_error ( $term ) || is_null ( $term ) ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_product_category_id' , __ ( 'A product category with the provided ID could not be found' , 'woocommerce' ), 404 );
}
2014-07-28 23:01:50 +00:00
2015-06-05 14:37:46 +00:00
$term_id = intval ( $term -> term_id );
// Get category display type
$display_type = get_woocommerce_term_meta ( $term_id , 'display_type' );
// Get category image
$image = '' ;
if ( $image_id = get_woocommerce_term_meta ( $term_id , 'thumbnail_id' ) ) {
$image = wp_get_attachment_url ( $image_id );
}
2015-01-05 19:26:24 +00:00
$product_category = array (
2015-06-05 14:37:46 +00:00
'id' => $term_id ,
2015-01-05 19:26:24 +00:00
'name' => $term -> name ,
'slug' => $term -> slug ,
'parent' => $term -> parent ,
'description' => $term -> description ,
2015-06-05 14:37:46 +00:00
'display' => $display_type ? $display_type : 'default' ,
'image' => $image ? esc_url ( $image ) : '' ,
'count' => intval ( $term -> count )
2015-01-05 19:26:24 +00:00
);
2014-07-28 23:01:50 +00:00
2015-01-05 19:26:24 +00:00
return array ( 'product_category' => apply_filters ( 'woocommerce_api_product_category_response' , $product_category , $id , $fields , $term , $this ) );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
2014-07-28 23:01:50 +00:00
}
2015-09-10 06:33:05 +00:00
/**
* Create a new product category .
*
* @ since 2.5 . 0
* @ param array $data Posted data
* @ return array | WP_Error Product category if succeed , otherwise WP_Error
* will be returned
*/
public function create_product_category ( $data ) {
global $wpdb ;
try {
if ( ! isset ( $data [ 'product_category' ] ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_product_category_data' , sprintf ( __ ( 'No %1$s data specified to create %1$s' , 'woocommerce' ), 'product_category' ), 400 );
}
// Check permissions
if ( ! current_user_can ( 'manage_product_terms' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_create_product_category' , __ ( 'You do not have permission to create product categories' , 'woocommerce' ), 401 );
}
$defaults = array (
'name' => '' ,
'slug' => '' ,
'description' => '' ,
'parent' => 0 ,
'display' => 'default' ,
'image' => '' ,
);
$data = wp_parse_args ( $data [ 'product_category' ], $defaults );
$data = apply_filters ( 'woocommerce_api_create_product_category_data' , $data , $this );
// Check parent.
$data [ 'parent' ] = absint ( $data [ 'parent' ] );
if ( $data [ 'parent' ] ) {
2016-02-10 16:27:00 +00:00
$parent = get_term_by ( 'id' , $data [ 'parent' ], 'product_cat' );
2015-09-10 06:33:05 +00:00
if ( ! $parent ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_product_category_parent' , __ ( 'Product category parent is invalid' , 'woocommerce' ), 400 );
}
}
// If value of image is numeric, assume value as image_id.
2015-12-16 18:35:46 +00:00
$image = $data [ 'image' ];
$image_id = 0 ;
2015-09-10 06:33:05 +00:00
if ( is_numeric ( $image ) ) {
$image_id = absint ( $image );
} else if ( ! empty ( $image ) ) {
2016-01-22 18:43:52 +00:00
$upload = $this -> upload_product_category_image ( esc_url_raw ( $image ) );
2015-09-10 06:33:05 +00:00
$image_id = $this -> set_product_category_image_as_attachment ( $upload );
}
2015-12-16 18:35:46 +00:00
$insert = wp_insert_term ( $data [ 'name' ], 'product_cat' , $data );
2015-09-10 06:33:05 +00:00
if ( is_wp_error ( $insert ) ) {
throw new WC_API_Exception ( 'woocommerce_api_cannot_create_product_category' , $insert -> get_error_message (), 400 );
}
2016-02-10 16:27:00 +00:00
$id = $insert [ 'term_id' ];
update_woocommerce_term_meta ( $id , 'display_type' , esc_attr ( $data [ 'display' ] ) );
2015-09-10 06:33:05 +00:00
// Check if image_id is a valid image attachment before updating the term meta.
2015-12-16 18:35:46 +00:00
if ( $image_id && wp_attachment_is_image ( $image_id ) ) {
2016-02-10 16:27:00 +00:00
update_woocommerce_term_meta ( $id , 'thumbnail_id' , $image_id );
2015-09-10 06:33:05 +00:00
}
2016-02-10 16:27:00 +00:00
do_action ( 'woocommerce_api_create_product_category' , $id , $data );
2015-09-10 06:33:05 +00:00
$this -> server -> send_status ( 201 );
2016-02-10 16:27:00 +00:00
return $this -> get_product_category ( $id );
2015-09-10 06:33:05 +00:00
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
/**
* Edit a product category .
*
* @ since 2.5 . 0
* @ param int $id Product category term ID
* @ param array $data Posted data
* @ return array | WP_Error Product category if succeed , otherwise WP_Error
* will be returned
*/
public function edit_product_category ( $id , $data ) {
global $wpdb ;
try {
if ( ! isset ( $data [ 'product_category' ] ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_product_category' , sprintf ( __ ( 'No %1$s data specified to edit %1$s' , 'woocommerce' ), 'product_category' ), 400 );
}
$id = absint ( $id );
$data = $data [ 'product_category' ];
// Check permissions.
if ( ! current_user_can ( 'manage_product_terms' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_edit_product_category' , __ ( 'You do not have permission to edit product categories' , 'woocommerce' ), 401 );
}
$data = apply_filters ( 'woocommerce_api_edit_product_category_data' , $data , $this );
$category = $this -> get_product_category ( $id );
if ( is_wp_error ( $category ) ) {
return $category ;
}
if ( isset ( $data [ 'image' ] ) ) {
2015-12-16 18:44:41 +00:00
$image_id = 0 ;
2015-09-10 06:33:05 +00:00
// If value of image is numeric, assume value as image_id.
$image = $data [ 'image' ];
if ( is_numeric ( $image ) ) {
$image_id = absint ( $image );
} else if ( ! empty ( $image ) ) {
2016-01-22 18:43:52 +00:00
$upload = $this -> upload_product_category_image ( esc_url_raw ( $image ) );
2015-09-10 06:33:05 +00:00
$image_id = $this -> set_product_category_image_as_attachment ( $upload );
}
2015-12-16 18:44:41 +00:00
// In case client supplies invalid image or wants to unset category image.
2015-09-10 06:33:05 +00:00
if ( ! wp_attachment_is_image ( $image_id ) ) {
$image_id = '' ;
}
}
$update = wp_update_term ( $id , 'product_cat' , $data );
if ( is_wp_error ( $update ) ) {
throw new WC_API_Exception ( 'woocommerce_api_cannot_edit_product_catgory' , __ ( 'Could not edit the category' , 'woocommerce' ), 400 );
}
2015-12-16 18:44:41 +00:00
if ( ! empty ( $data [ 'display' ] ) ) {
2016-02-10 16:27:00 +00:00
update_woocommerce_term_meta ( $id , 'display_type' , sanitize_text_field ( $data [ 'display' ] ) );
2015-09-10 06:33:05 +00:00
}
if ( isset ( $image_id ) ) {
2016-02-10 16:27:00 +00:00
update_woocommerce_term_meta ( $id , 'thumbnail_id' , $image_id );
2015-09-10 06:33:05 +00:00
}
do_action ( 'woocommerce_api_edit_product_category' , $id , $data );
return $this -> get_product_category ( $id );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
/**
* Delete a product category .
*
* @ since 2.5 . 0
* @ param int $id Product category term ID
* @ return array | WP_Error Success message if succeed , otherwise WP_Error
* will be returned
*/
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 );
$deleted = wp_delete_term ( $id , 'product_cat' );
if ( ! $deleted || is_wp_error ( $deleted ) ) {
throw new WC_API_Exception ( 'woocommerce_api_cannot_delete_product_category' , __ ( 'Could not delete the category' , 'woocommerce' ), 401 );
}
// When a term is deleted, delete its meta.
$wpdb -> delete ( $wpdb -> woocommerce_termmeta , array ( 'woocommerce_term_id' => $id ), array ( '%d' ) );
do_action ( 'woocommerce_api_delete_product_category' , $id , $this );
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 () ) );
}
}
2015-09-10 09:58:48 +00:00
/**
* Get a listing of product tags .
*
* @ since 2.5 . 0
* @ param string | null $fields Fields to limit response to
* @ return array Product tags
*/
public function get_product_tags ( $fields = null ) {
try {
// Permissions check
if ( ! current_user_can ( 'manage_product_terms' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_read_product_tags' , __ ( 'You do not have permission to read product tags' , 'woocommerce' ), 401 );
}
$product_tags = array ();
$terms = get_terms ( 'product_tag' , array ( 'hide_empty' => false , 'fields' => 'ids' ) );
foreach ( $terms as $term_id ) {
$product_tags [] = current ( $this -> get_product_tag ( $term_id , $fields ) );
}
return array ( 'product_tags' => apply_filters ( 'woocommerce_api_product_tags_response' , $product_tags , $terms , $fields , $this ) );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
/**
* Get the product tag for the given ID .
*
* @ since 2.5 . 0
* @ param string $id Product tag term ID
* @ param string | null $fields Fields to limit response to
* @ return array Product tag
*/
public function get_product_tag ( $id , $fields = null ) {
try {
$id = absint ( $id );
// Validate ID
if ( empty ( $id ) ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_product_tag_id' , __ ( 'Invalid product tag ID' , 'woocommerce' ), 400 );
}
// Permissions check
if ( ! current_user_can ( 'manage_product_terms' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_read_product_tags' , __ ( 'You do not have permission to read product tags' , 'woocommerce' ), 401 );
}
$term = get_term ( $id , 'product_tag' );
if ( is_wp_error ( $term ) || is_null ( $term ) ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_product_tag_id' , __ ( 'A product tag with the provided ID could not be found' , 'woocommerce' ), 404 );
}
$term_id = intval ( $term -> term_id );
$tag = array (
'id' => $term_id ,
'name' => $term -> name ,
'slug' => $term -> slug ,
'description' => $term -> description ,
'count' => intval ( $term -> count )
);
return array ( 'product_tag' => apply_filters ( 'woocommerce_api_product_tag_response' , $tag , $id , $fields , $term , $this ) );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
/**
* Create a new product tag .
*
* @ since 2.5 . 0
* @ param array $data Posted data
* @ return array | WP_Error Product tag if succeed , otherwise WP_Error
* will be returned
*/
public function create_product_tag ( $data ) {
try {
if ( ! isset ( $data [ 'product_tag' ] ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_product_tag_data' , sprintf ( __ ( 'No %1$s data specified to create %1$s' , 'woocommerce' ), 'product_tag' ), 400 );
}
// Check permissions
if ( ! current_user_can ( 'manage_product_terms' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_create_product_tag' , __ ( 'You do not have permission to create product tags' , 'woocommerce' ), 401 );
}
$defaults = array (
'name' => '' ,
'slug' => '' ,
'description' => '' ,
);
$data = wp_parse_args ( $data [ 'product_tag' ], $defaults );
$data = apply_filters ( 'woocommerce_api_create_product_tag_data' , $data , $this );
2015-12-16 18:46:31 +00:00
$insert = wp_insert_term ( $data [ 'name' ], 'product_tag' , $data );
2015-09-10 09:58:48 +00:00
if ( is_wp_error ( $insert ) ) {
throw new WC_API_Exception ( 'woocommerce_api_cannot_create_product_tag' , $insert -> get_error_message (), 400 );
}
2016-02-10 16:27:00 +00:00
$id = $insert [ 'term_id' ];
2015-09-10 09:58:48 +00:00
2016-02-10 16:27:00 +00:00
do_action ( 'woocommerce_api_create_product_tag' , $id , $data );
2015-09-10 09:58:48 +00:00
$this -> server -> send_status ( 201 );
2016-02-10 16:27:00 +00:00
return $this -> get_product_tag ( $id );
2015-09-10 09:58:48 +00:00
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
/**
* Edit a product tag .
*
* @ since 2.5 . 0
* @ param int $id Product tag term ID
* @ param array $data Posted data
* @ return array | WP_Error Product tag if succeed , otherwise WP_Error
* will be returned
*/
public function edit_product_tag ( $id , $data ) {
try {
if ( ! isset ( $data [ 'product_tag' ] ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_product_tag' , sprintf ( __ ( 'No %1$s data specified to edit %1$s' , 'woocommerce' ), 'product_tag' ), 400 );
}
$id = absint ( $id );
$data = $data [ 'product_tag' ];
// Check permissions.
if ( ! current_user_can ( 'manage_product_terms' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_edit_product_tag' , __ ( 'You do not have permission to edit product tags' , 'woocommerce' ), 401 );
}
$data = apply_filters ( 'woocommerce_api_edit_product_tag_data' , $data , $this );
$tag = $this -> get_product_tag ( $id );
if ( is_wp_error ( $tag ) ) {
return $tag ;
}
$update = wp_update_term ( $id , 'product_tag' , $data );
if ( is_wp_error ( $update ) ) {
throw new WC_API_Exception ( 'woocommerce_api_cannot_edit_product_tag' , __ ( 'Could not edit the tag' , 'woocommerce' ), 400 );
}
do_action ( 'woocommerce_api_edit_product_tag' , $id , $data );
return $this -> get_product_tag ( $id );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
/**
* Delete a product tag .
*
* @ since 2.5 . 0
* @ param int $id Product tag term ID
* @ return array | WP_Error Success message if succeed , otherwise WP_Error
* will be returned
*/
public function delete_product_tag ( $id ) {
try {
// Check permissions
if ( ! current_user_can ( 'manage_product_terms' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_delete_product_tag' , __ ( 'You do not have permission to delete product tag' , 'woocommerce' ), 401 );
}
$id = absint ( $id );
$deleted = wp_delete_term ( $id , 'product_tag' );
if ( ! $deleted || is_wp_error ( $deleted ) ) {
throw new WC_API_Exception ( 'woocommerce_api_cannot_delete_product_tag' , __ ( 'Could not delete the tag' , 'woocommerce' ), 401 );
}
do_action ( 'woocommerce_api_delete_product_tag' , $id , $this );
return array ( 'message' => sprintf ( __ ( 'Deleted %s' , 'woocommerce' ), 'product_tag' ) );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
2013-11-04 06:36:31 +00:00
/**
* Helper method to get product post objects
*
* @ since 2.1
* @ param array $args request arguments for filtering query
2013-11-11 00:29:36 +00:00
* @ return WP_Query
2013-11-04 06:36:31 +00:00
*/
2013-11-11 00:29:36 +00:00
private function query_products ( $args ) {
2013-11-04 06:36:31 +00:00
2015-01-05 19:26:24 +00:00
// Set base query arguments
2013-11-04 06:36:31 +00:00
$query_args = array (
'fields' => 'ids' ,
'post_type' => 'product' ,
'post_status' => 'publish' ,
2014-04-07 09:19:09 +00:00
'meta_query' => array (),
2013-11-04 06:36:31 +00:00
);
2015-09-15 12:18:28 +00:00
// Taxonomy query to filter products by type, category, tag, shipping class, and
// attribute.
$tax_query = array ();
2013-11-11 00:29:36 +00:00
2015-09-15 12:18:28 +00:00
// Map between taxonomy name and arg's key.
$taxonomies_arg_map = array (
'product_type' => 'type' ,
'product_cat' => 'category' ,
'product_tag' => 'tag' ,
'product_shipping_class' => 'shipping_class' ,
);
2013-11-18 21:47:38 +00:00
2015-09-15 12:18:28 +00:00
// Add attribute taxonomy names into the map.
foreach ( wc_get_attribute_taxonomy_names () as $attribute_name ) {
$taxonomies_arg_map [ $attribute_name ] = $attribute_name ;
}
2013-11-11 00:29:36 +00:00
2015-09-15 12:18:28 +00:00
// Set tax_query for each passed arg.
foreach ( $taxonomies_arg_map as $tax_name => $arg ) {
if ( ! empty ( $args [ $arg ] ) ) {
$terms = explode ( ',' , $args [ $arg ] );
2013-11-18 21:47:38 +00:00
2015-09-15 12:18:28 +00:00
$tax_query [] = array (
'taxonomy' => $tax_name ,
2013-11-11 00:29:36 +00:00
'field' => 'slug' ,
2015-09-15 12:18:28 +00:00
'terms' => $terms ,
);
2013-11-11 00:29:36 +00:00
2015-09-15 12:18:28 +00:00
unset ( $args [ $arg ] );
}
2013-11-11 00:29:36 +00:00
}
2015-09-15 12:18:28 +00:00
if ( ! empty ( $tax_query ) ) {
$query_args [ 'tax_query' ] = $tax_query ;
2014-10-17 14:53:14 +00:00
}
2015-03-23 14:41:37 +00:00
// Filter by specific sku
if ( ! empty ( $args [ 'sku' ] ) ) {
if ( ! is_array ( $query_args [ 'meta_query' ] ) ) {
$query_args [ 'meta_query' ] = array ();
}
$query_args [ 'meta_query' ][] = array (
'key' => '_sku' ,
'value' => $args [ 'sku' ],
'compare' => '='
);
2015-10-09 19:43:12 +00:00
$query_args [ 'post_type' ] = array ( 'product' , 'product_variation' );
2015-03-21 17:53:00 +00:00
}
2014-10-17 14:53:14 +00:00
2013-11-11 00:29:36 +00:00
$query_args = $this -> merge_query_args ( $query_args , $args );
2013-11-04 06:36:31 +00:00
return new WP_Query ( $query_args );
}
2013-11-14 17:40:35 +00:00
/**
* Get standard product data that applies to every product type
*
* @ since 2.1
* @ param WC_Product $product
2014-09-07 23:37:55 +00:00
* @ return WC_Product
2013-11-14 17:40:35 +00:00
*/
private function get_product_data ( $product ) {
return array (
'title' => $product -> get_title (),
'id' => ( int ) $product -> is_type ( 'variation' ) ? $product -> get_variation_id () : $product -> id ,
2013-11-18 21:47:38 +00:00
'created_at' => $this -> server -> format_datetime ( $product -> get_post_data () -> post_date_gmt ),
'updated_at' => $this -> server -> format_datetime ( $product -> get_post_data () -> post_modified_gmt ),
2013-11-14 17:40:35 +00:00
'type' => $product -> product_type ,
'status' => $product -> get_post_data () -> post_status ,
'downloadable' => $product -> is_downloadable (),
'virtual' => $product -> is_virtual (),
'permalink' => $product -> get_permalink (),
'sku' => $product -> get_sku (),
2015-10-06 14:12:23 +00:00
'price' => $product -> get_price (),
'regular_price' => $product -> get_regular_price (),
'sale_price' => $product -> get_sale_price () ? $product -> get_sale_price () : null ,
2013-11-14 17:40:35 +00:00
'price_html' => $product -> get_price_html (),
'taxable' => $product -> is_taxable (),
'tax_status' => $product -> get_tax_status (),
'tax_class' => $product -> get_tax_class (),
'managing_stock' => $product -> managing_stock (),
2015-06-19 04:07:10 +00:00
'stock_quantity' => $product -> get_stock_quantity (),
2013-11-14 17:40:35 +00:00
'in_stock' => $product -> is_in_stock (),
'backorders_allowed' => $product -> backorders_allowed (),
'backordered' => $product -> is_on_backorder (),
'sold_individually' => $product -> is_sold_individually (),
'purchaseable' => $product -> is_purchasable (),
'featured' => $product -> is_featured (),
'visible' => $product -> is_visible (),
'catalog_visibility' => $product -> visibility ,
'on_sale' => $product -> is_on_sale (),
2015-04-17 18:50:18 +00:00
'product_url' => $product -> is_type ( 'external' ) ? $product -> get_product_url () : '' ,
'button_text' => $product -> is_type ( 'external' ) ? $product -> get_button_text () : '' ,
2015-10-06 14:12:23 +00:00
'weight' => $product -> get_weight () ? $product -> get_weight () : null ,
2013-11-14 17:40:35 +00:00
'dimensions' => array (
'length' => $product -> length ,
'width' => $product -> width ,
'height' => $product -> height ,
'unit' => get_option ( 'woocommerce_dimension_unit' ),
),
'shipping_required' => $product -> needs_shipping (),
'shipping_taxable' => $product -> is_shipping_taxable (),
'shipping_class' => $product -> get_shipping_class (),
'shipping_class_id' => ( 0 !== $product -> get_shipping_class_id () ) ? $product -> get_shipping_class_id () : null ,
2014-02-13 04:49:30 +00:00
'description' => wpautop ( do_shortcode ( $product -> get_post_data () -> post_content ) ),
2013-11-14 17:40:35 +00:00
'short_description' => apply_filters ( 'woocommerce_short_description' , $product -> get_post_data () -> post_excerpt ),
'reviews_allowed' => ( 'open' === $product -> get_post_data () -> comment_status ),
2013-11-25 13:34:21 +00:00
'average_rating' => wc_format_decimal ( $product -> get_average_rating (), 2 ),
2013-11-18 21:47:38 +00:00
'rating_count' => ( int ) $product -> get_rating_count (),
'related_ids' => array_map ( 'absint' , array_values ( $product -> get_related () ) ),
'upsell_ids' => array_map ( 'absint' , $product -> get_upsells () ),
'cross_sell_ids' => array_map ( 'absint' , $product -> get_cross_sells () ),
2016-02-11 17:40:57 +00:00
'parent_id' => $product -> is_type ( 'variation' ) ? $product -> parent -> id : $product -> post -> post_parent ,
2013-11-14 17:40:35 +00:00
'categories' => wp_get_post_terms ( $product -> id , 'product_cat' , array ( 'fields' => 'names' ) ),
'tags' => wp_get_post_terms ( $product -> id , 'product_tag' , array ( 'fields' => 'names' ) ),
'images' => $this -> get_images ( $product ),
2015-09-25 14:13:52 +00:00
'featured_src' => ( string ) wp_get_attachment_url ( get_post_thumbnail_id ( $product -> is_type ( 'variation' ) ? $product -> variation_id : $product -> id ) ),
2013-11-14 17:40:35 +00:00
'attributes' => $this -> get_attributes ( $product ),
'downloads' => $this -> get_downloads ( $product ),
2013-11-18 21:47:38 +00:00
'download_limit' => ( int ) $product -> download_limit ,
'download_expiry' => ( int ) $product -> download_expiry ,
2013-11-14 17:40:35 +00:00
'download_type' => $product -> download_type ,
2014-10-22 11:12:25 +00:00
'purchase_note' => wpautop ( do_shortcode ( wp_kses_post ( $product -> purchase_note ) ) ),
2013-11-23 20:17:14 +00:00
'total_sales' => metadata_exists ( 'post' , $product -> id , 'total_sales' ) ? ( int ) get_post_meta ( $product -> id , 'total_sales' , true ) : 0 ,
2013-11-14 17:40:35 +00:00
'variations' => array (),
'parent' => array (),
2015-10-01 12:08:42 +00:00
'grouped_products' => array (),
2016-02-11 17:40:57 +00:00
'menu_order' => $this -> get_product_menu_order ( $product ),
2013-11-14 17:40:35 +00:00
);
}
2016-02-11 17:40:57 +00:00
/**
* Get product menu order .
*
* @ since 2.5 . 3
* @ param WC_Product $product
* @ return int
*/
private function get_product_menu_order ( $product ) {
$menu_order = $product -> post -> menu_order ;
if ( $product -> is_type ( 'variation' ) ) {
$_product = get_post ( $product -> get_variation_id () );
$menu_order = $_product -> menu_order ;
}
return apply_filters ( 'woocommerce_api_product_menu_order' , $menu_order , $product );
}
2013-11-14 17:40:35 +00:00
/**
* Get an individual variation ' s data
*
* @ since 2.1
* @ param WC_Product $product
* @ return array
*/
private function get_variation_data ( $product ) {
2015-10-06 14:12:23 +00:00
$variations = array ();
2013-11-14 17:40:35 +00:00
foreach ( $product -> get_children () as $child_id ) {
$variation = $product -> get_child ( $child_id );
2014-06-26 19:55:24 +00:00
if ( ! $variation -> exists () ) {
2013-11-14 17:40:35 +00:00
continue ;
2014-06-26 19:55:24 +00:00
}
2013-11-14 17:40:35 +00:00
$variations [] = array (
2014-11-12 14:21:26 +00:00
'id' => $variation -> get_variation_id (),
'created_at' => $this -> server -> format_datetime ( $variation -> get_post_data () -> post_date_gmt ),
'updated_at' => $this -> server -> format_datetime ( $variation -> get_post_data () -> post_modified_gmt ),
'downloadable' => $variation -> is_downloadable (),
'virtual' => $variation -> is_virtual (),
'permalink' => $variation -> get_permalink (),
'sku' => $variation -> get_sku (),
2015-10-06 14:12:23 +00:00
'price' => $variation -> get_price (),
'regular_price' => $variation -> get_regular_price (),
'sale_price' => $variation -> get_sale_price () ? $variation -> get_sale_price () : null ,
2014-11-12 14:21:26 +00:00
'taxable' => $variation -> is_taxable (),
'tax_status' => $variation -> get_tax_status (),
'tax_class' => $variation -> get_tax_class (),
'managing_stock' => $variation -> managing_stock (),
2015-06-19 04:07:10 +00:00
'stock_quantity' => $variation -> get_stock_quantity (),
2014-11-12 14:21:26 +00:00
'in_stock' => $variation -> is_in_stock (),
'backordered' => $variation -> is_on_backorder (),
'purchaseable' => $variation -> is_purchasable (),
'visible' => $variation -> variation_is_visible (),
'on_sale' => $variation -> is_on_sale (),
2015-10-06 14:12:23 +00:00
'weight' => $variation -> get_weight () ? $variation -> get_weight () : null ,
2014-11-12 14:21:26 +00:00
'dimensions' => array (
'length' => $variation -> length ,
'width' => $variation -> width ,
'height' => $variation -> height ,
'unit' => get_option ( 'woocommerce_dimension_unit' ),
),
'shipping_class' => $variation -> get_shipping_class (),
'shipping_class_id' => ( 0 !== $variation -> get_shipping_class_id () ) ? $variation -> get_shipping_class_id () : null ,
'image' => $this -> get_images ( $variation ),
'attributes' => $this -> get_attributes ( $variation ),
'downloads' => $this -> get_downloads ( $variation ),
'download_limit' => ( int ) $product -> download_limit ,
'download_expiry' => ( int ) $product -> download_expiry ,
2013-11-14 17:40:35 +00:00
);
}
return $variations ;
}
2015-10-01 12:08:42 +00:00
/**
* Get grouped products data
*
* @ since 2.5 . 0
* @ param WC_Product $product
*
* @ return array
*/
private function get_grouped_products_data ( $product ) {
$products = array ();
foreach ( $product -> get_children () as $child_id ) {
$_product = $product -> get_child ( $child_id );
if ( ! $_product -> exists () ) {
continue ;
}
$products [] = $this -> get_product_data ( $_product );
}
return $products ;
}
2014-06-24 18:30:11 +00:00
/**
2015-12-17 10:08:17 +00:00
* Save product meta .
2014-06-24 18:30:11 +00:00
*
2015-11-17 12:51:42 +00:00
* @ since 2.2
* @ param int $product_id
* @ param array $data
2015-01-13 15:44:30 +00:00
* @ return bool
2015-11-17 12:51:42 +00:00
* @ throws WC_API_Exception
2014-06-24 18:30:11 +00:00
*/
2015-04-23 15:45:47 +00:00
protected function save_product_meta ( $product_id , $data ) {
2015-01-21 23:09:08 +00:00
global $wpdb ;
2015-12-17 10:08:17 +00:00
// Product Type.
2014-06-25 16:27:48 +00:00
$product_type = null ;
2014-06-24 18:30:11 +00:00
if ( isset ( $data [ 'type' ] ) ) {
2014-06-25 16:27:48 +00:00
$product_type = wc_clean ( $data [ 'type' ] );
2015-04-23 15:45:47 +00:00
wp_set_object_terms ( $product_id , $product_type , 'product_type' );
2014-06-25 16:27:48 +00:00
} else {
2015-04-23 15:45:47 +00:00
$_product_type = get_the_terms ( $product_id , 'product_type' );
2014-06-25 16:27:48 +00:00
if ( is_array ( $_product_type ) ) {
$_product_type = current ( $_product_type );
2014-06-25 19:20:28 +00:00
$product_type = $_product_type -> slug ;
2014-06-24 18:30:11 +00:00
}
}
2015-12-17 10:08:17 +00:00
// Virtual.
2014-06-26 19:47:54 +00:00
if ( isset ( $data [ 'virtual' ] ) ) {
2015-04-23 15:45:47 +00:00
update_post_meta ( $product_id , '_virtual' , ( true === $data [ 'virtual' ] ) ? 'yes' : 'no' );
2014-06-26 19:47:54 +00:00
}
2015-12-17 10:08:17 +00:00
// Tax status.
2014-06-24 18:30:11 +00:00
if ( isset ( $data [ 'tax_status' ] ) ) {
2015-04-23 15:45:47 +00:00
update_post_meta ( $product_id , '_tax_status' , wc_clean ( $data [ 'tax_status' ] ) );
2014-06-24 18:30:11 +00:00
}
2015-12-17 10:08:17 +00:00
// Tax Class.
2014-06-24 18:30:11 +00:00
if ( isset ( $data [ 'tax_class' ] ) ) {
2015-04-23 15:45:47 +00:00
update_post_meta ( $product_id , '_tax_class' , wc_clean ( $data [ 'tax_class' ] ) );
2014-06-24 18:30:11 +00:00
}
2015-12-17 10:08:17 +00:00
// Catalog Visibility.
2014-06-24 18:30:11 +00:00
if ( isset ( $data [ 'catalog_visibility' ] ) ) {
2015-04-23 15:45:47 +00:00
update_post_meta ( $product_id , '_visibility' , wc_clean ( $data [ 'catalog_visibility' ] ) );
2014-06-24 18:30:11 +00:00
}
2015-12-17 10:08:17 +00:00
// Purchase Note.
2014-06-24 18:30:11 +00:00
if ( isset ( $data [ 'purchase_note' ] ) ) {
2015-04-23 15:45:47 +00:00
update_post_meta ( $product_id , '_purchase_note' , wc_clean ( $data [ 'purchase_note' ] ) );
2014-06-24 18:30:11 +00:00
}
2015-12-17 10:08:17 +00:00
// Featured Product.
2014-06-24 18:30:11 +00:00
if ( isset ( $data [ 'featured' ] ) ) {
2015-04-23 15:45:47 +00:00
update_post_meta ( $product_id , '_featured' , ( true === $data [ 'featured' ] ) ? 'yes' : 'no' );
2014-06-24 18:30:11 +00:00
}
2015-12-17 10:08:17 +00:00
// Shipping data.
2015-04-23 15:45:47 +00:00
$this -> save_product_shipping_data ( $product_id , $data );
2014-06-24 18:30:11 +00:00
2015-12-17 10:08:17 +00:00
// SKU.
2014-06-24 18:30:11 +00:00
if ( isset ( $data [ 'sku' ] ) ) {
2015-04-23 15:45:47 +00:00
$sku = get_post_meta ( $product_id , '_sku' , true );
2014-06-27 18:58:29 +00:00
$new_sku = wc_clean ( $data [ 'sku' ] );
if ( '' == $new_sku ) {
2015-04-23 15:45:47 +00:00
update_post_meta ( $product_id , '_sku' , '' );
2014-06-27 18:58:29 +00:00
} elseif ( $new_sku !== $sku ) {
if ( ! empty ( $new_sku ) ) {
2015-04-23 15:45:47 +00:00
$unique_sku = wc_product_has_unique_sku ( $product_id , $new_sku );
2014-06-27 18:58:29 +00:00
if ( ! $unique_sku ) {
2015-01-05 19:26:24 +00:00
throw new WC_API_Exception ( 'woocommerce_api_product_sku_already_exists' , __ ( 'The SKU already exists on another product' , 'woocommerce' ), 400 );
2014-06-27 18:58:29 +00:00
} else {
2015-04-23 15:45:47 +00:00
update_post_meta ( $product_id , '_sku' , $new_sku );
2014-06-27 18:58:29 +00:00
}
} else {
2015-04-23 15:45:47 +00:00
update_post_meta ( $product_id , '_sku' , '' );
2014-06-27 18:58:29 +00:00
}
2014-06-24 18:30:11 +00:00
}
}
2015-12-17 10:08:17 +00:00
// Attributes.
2014-06-24 18:30:11 +00:00
if ( isset ( $data [ 'attributes' ] ) ) {
$attributes = array ();
2014-06-24 21:32:37 +00:00
2014-06-24 18:30:11 +00:00
foreach ( $data [ 'attributes' ] as $attribute ) {
2015-10-28 18:31:05 +00:00
$is_taxonomy = 0 ;
$taxonomy = 0 ;
2014-06-24 21:32:37 +00:00
if ( ! isset ( $attribute [ 'name' ] ) ) {
continue ;
}
2015-01-13 15:24:28 +00:00
$attribute_slug = sanitize_title ( $attribute [ 'name' ] );
if ( isset ( $attribute [ 'slug' ] ) ) {
$taxonomy = $this -> get_attribute_taxonomy_by_slug ( $attribute [ 'slug' ] );
$attribute_slug = sanitize_title ( $attribute [ 'slug' ] );
}
2014-06-26 19:47:54 +00:00
if ( $taxonomy ) {
$is_taxonomy = 1 ;
2014-06-24 21:32:37 +00:00
}
if ( $is_taxonomy ) {
if ( isset ( $attribute [ 'options' ] ) ) {
2015-10-23 16:38:37 +00:00
$options = $attribute [ 'options' ];
2014-06-24 21:32:37 +00:00
2015-10-23 16:38:37 +00:00
if ( ! is_array ( $attribute [ 'options' ] ) ) {
2015-12-17 10:08:17 +00:00
// Text based attributes - Posted values are term names.
2015-10-23 16:38:37 +00:00
$options = explode ( WC_DELIMITER , $options );
2014-06-24 21:32:37 +00:00
}
2015-10-23 16:38:37 +00:00
$values = array_map ( 'wc_sanitize_term_text_based' , $options );
2014-06-24 21:32:37 +00:00
$values = array_filter ( $values , 'strlen' );
} else {
$values = array ();
}
2015-12-17 10:08:17 +00:00
// Update post terms.
2014-06-24 21:32:37 +00:00
if ( taxonomy_exists ( $taxonomy ) ) {
2015-04-23 15:45:47 +00:00
wp_set_object_terms ( $product_id , $values , $taxonomy );
2014-06-24 21:32:37 +00:00
}
if ( $values ) {
2015-12-17 10:08:17 +00:00
// Add attribute to array, but don't set values.
2014-06-24 21:32:37 +00:00
$attributes [ $taxonomy ] = array (
'name' => $taxonomy ,
'value' => '' ,
'position' => isset ( $attribute [ 'position' ] ) ? absint ( $attribute [ 'position' ] ) : 0 ,
2014-10-22 20:46:39 +00:00
'is_visible' => ( isset ( $attribute [ 'visible' ] ) && $attribute [ 'visible' ] ) ? 1 : 0 ,
2014-06-24 21:32:37 +00:00
'is_variation' => ( isset ( $attribute [ 'variation' ] ) && $attribute [ 'variation' ] ) ? 1 : 0 ,
'is_taxonomy' => $is_taxonomy
);
}
} elseif ( isset ( $attribute [ 'options' ] ) ) {
2015-12-17 10:08:17 +00:00
// Array based.
2014-06-24 21:32:37 +00:00
if ( is_array ( $attribute [ 'options' ] ) ) {
$values = implode ( ' ' . WC_DELIMITER . ' ' , array_map ( 'wc_clean' , $attribute [ 'options' ] ) );
2015-12-17 10:08:17 +00:00
// Text based, separate by pipe.
2014-06-24 21:32:37 +00:00
} else {
$values = implode ( ' ' . WC_DELIMITER . ' ' , array_map ( 'wc_clean' , explode ( WC_DELIMITER , $attribute [ 'options' ] ) ) );
}
2015-12-17 10:08:17 +00:00
// Custom attribute - Add attribute to array and set the values.
2015-01-13 15:24:28 +00:00
$attributes [ $attribute_slug ] = array (
2014-06-24 21:32:37 +00:00
'name' => wc_clean ( $attribute [ 'name' ] ),
'value' => $values ,
'position' => isset ( $attribute [ 'position' ] ) ? absint ( $attribute [ 'position' ] ) : 0 ,
2014-10-22 20:46:39 +00:00
'is_visible' => ( isset ( $attribute [ 'visible' ] ) && $attribute [ 'visible' ] ) ? 1 : 0 ,
2014-06-24 21:32:37 +00:00
'is_variation' => ( isset ( $attribute [ 'variation' ] ) && $attribute [ 'variation' ] ) ? 1 : 0 ,
'is_taxonomy' => $is_taxonomy
);
}
}
if ( ! function_exists ( 'attributes_cmp' ) ) {
function attributes_cmp ( $a , $b ) {
2014-06-25 19:20:28 +00:00
if ( $a [ 'position' ] == $b [ 'position' ] ) {
return 0 ;
}
2014-06-24 21:32:37 +00:00
return ( $a [ 'position' ] < $b [ 'position' ] ) ? - 1 : 1 ;
}
2014-06-24 18:30:11 +00:00
}
2014-06-24 21:32:37 +00:00
uasort ( $attributes , 'attributes_cmp' );
2015-04-23 15:45:47 +00:00
update_post_meta ( $product_id , '_product_attributes' , $attributes );
2014-06-24 18:30:11 +00:00
}
2015-12-17 10:08:17 +00:00
// Sales and prices.
2014-06-25 16:27:48 +00:00
if ( in_array ( $product_type , array ( 'variable' , 'grouped' ) ) ) {
2015-12-17 10:08:17 +00:00
// Variable and grouped products have no prices.
2015-04-23 15:45:47 +00:00
update_post_meta ( $product_id , '_regular_price' , '' );
update_post_meta ( $product_id , '_sale_price' , '' );
update_post_meta ( $product_id , '_sale_price_dates_from' , '' );
update_post_meta ( $product_id , '_sale_price_dates_to' , '' );
update_post_meta ( $product_id , '_price' , '' );
2014-06-25 16:27:48 +00:00
} else {
2016-01-07 22:52:01 +00:00
// Regular Price
2014-06-25 16:27:48 +00:00
if ( isset ( $data [ 'regular_price' ] ) ) {
2016-01-07 22:52:01 +00:00
$regular_price = ( '' === $data [ 'regular_price' ] ) ? '' : $data [ 'regular_price' ];
2014-06-25 16:27:48 +00:00
} else {
2015-04-23 15:45:47 +00:00
$regular_price = get_post_meta ( $product_id , '_regular_price' , true );
2014-06-25 16:27:48 +00:00
}
2016-01-07 22:52:01 +00:00
// Sale Price
2014-06-25 16:27:48 +00:00
if ( isset ( $data [ 'sale_price' ] ) ) {
2016-01-07 22:52:01 +00:00
$sale_price = ( '' === $data [ 'sale_price' ] ) ? '' : $data [ 'sale_price' ];
2014-06-25 16:27:48 +00:00
} else {
2015-04-23 15:45:47 +00:00
$sale_price = get_post_meta ( $product_id , '_sale_price' , true );
2014-06-25 16:27:48 +00:00
}
2016-01-08 12:34:19 +00:00
if ( isset ( $data [ 'sale_price_dates_from' ] ) ) {
$date_from = $data [ 'sale_price_dates_from' ];
2014-06-25 16:27:48 +00:00
} else {
2016-01-08 12:34:19 +00:00
$date_from = get_post_meta ( $product_id , '_sale_price_dates_from' , true );
2016-01-11 10:27:55 +00:00
$date_from = ( '' === $date_from ) ? '' : date ( 'Y-m-d' , $date_from );
2014-06-25 16:27:48 +00:00
}
2016-01-08 12:34:19 +00:00
if ( isset ( $data [ 'sale_price_dates_to' ] ) ) {
$date_to = $data [ 'sale_price_dates_to' ];
2014-06-25 16:27:48 +00:00
} else {
2016-01-11 10:27:55 +00:00
$date_to = get_post_meta ( $product_id , '_sale_price_dates_to' , true );
$date_to = ( '' === $date_to ) ? '' : date ( 'Y-m-d' , $date_to );
2014-06-25 16:27:48 +00:00
}
2016-01-07 22:52:01 +00:00
_wc_save_product_price ( $product_id , $regular_price , $sale_price , $date_from , $date_to );
2014-06-25 16:27:48 +00:00
2014-06-24 18:30:11 +00:00
}
2015-12-17 10:08:17 +00:00
// Product parent ID for groups.
2014-12-10 18:28:30 +00:00
if ( isset ( $data [ 'parent_id' ] ) ) {
2015-04-23 15:45:47 +00:00
wp_update_post ( array ( 'ID' => $product_id , 'post_parent' => absint ( $data [ 'parent_id' ] ) ) );
2014-12-10 18:28:30 +00:00
}
2015-12-17 10:08:17 +00:00
// Update parent if grouped so price sorting works and stays in sync with the cheapest child.
2015-04-23 15:45:47 +00:00
$_product = wc_get_product ( $product_id );
2014-06-25 19:20:28 +00:00
if ( $_product -> post -> post_parent > 0 || $product_type == 'grouped' ) {
$clear_parent_ids = array ();
if ( $_product -> post -> post_parent > 0 ) {
$clear_parent_ids [] = $_product -> post -> post_parent ;
}
if ( $product_type == 'grouped' ) {
2015-04-23 15:45:47 +00:00
$clear_parent_ids [] = $product_id ;
2014-06-25 19:20:28 +00:00
}
if ( $clear_parent_ids ) {
foreach ( $clear_parent_ids as $clear_id ) {
$children_by_price = get_posts ( array (
'post_parent' => $clear_id ,
'orderby' => 'meta_value_num' ,
'order' => 'asc' ,
'meta_key' => '_price' ,
'posts_per_page' => 1 ,
'post_type' => 'product' ,
'fields' => 'ids'
) );
if ( $children_by_price ) {
foreach ( $children_by_price as $child ) {
$child_price = get_post_meta ( $child , '_price' , true );
update_post_meta ( $clear_id , '_price' , $child_price );
}
}
}
}
}
2015-12-17 10:08:17 +00:00
// Sold Individually.
2014-06-24 18:30:11 +00:00
if ( isset ( $data [ 'sold_individually' ] ) ) {
2015-04-23 15:45:47 +00:00
update_post_meta ( $product_id , '_sold_individually' , ( true === $data [ 'sold_individually' ] ) ? 'yes' : '' );
2014-06-25 19:20:28 +00:00
}
2015-12-17 10:08:17 +00:00
// Stock status.
2014-06-25 19:20:28 +00:00
if ( isset ( $data [ 'in_stock' ] ) ) {
$stock_status = ( true === $data [ 'in_stock' ] ) ? 'instock' : 'outofstock' ;
} else {
2015-04-23 15:45:47 +00:00
$stock_status = get_post_meta ( $product_id , '_stock_status' , true );
2014-06-25 19:20:28 +00:00
if ( '' === $stock_status ) {
$stock_status = 'instock' ;
2014-06-24 18:30:11 +00:00
}
}
2015-12-17 10:08:17 +00:00
// Stock Data.
2014-06-25 19:20:28 +00:00
if ( 'yes' == get_option ( 'woocommerce_manage_stock' ) ) {
2015-12-17 10:08:17 +00:00
// Manage stock.
2014-06-25 19:20:28 +00:00
if ( isset ( $data [ 'managing_stock' ] ) ) {
$managing_stock = ( true === $data [ 'managing_stock' ] ) ? 'yes' : 'no' ;
2015-04-23 15:45:47 +00:00
update_post_meta ( $product_id , '_manage_stock' , $managing_stock );
2014-06-25 19:20:28 +00:00
} else {
2015-04-23 15:45:47 +00:00
$managing_stock = get_post_meta ( $product_id , '_manage_stock' , true );
2014-06-25 19:20:28 +00:00
}
2015-12-17 10:08:17 +00:00
// Backorders.
2014-06-25 19:20:28 +00:00
if ( isset ( $data [ 'backorders' ] ) ) {
2016-01-09 22:59:54 +00:00
if ( 'notify' === $data [ 'backorders' ] ) {
2014-06-25 19:20:28 +00:00
$backorders = 'notify' ;
} else {
$backorders = ( true === $data [ 'backorders' ] ) ? 'yes' : 'no' ;
}
2015-04-23 15:45:47 +00:00
update_post_meta ( $product_id , '_backorders' , $backorders );
2014-06-25 19:20:28 +00:00
} else {
2015-04-23 15:45:47 +00:00
$backorders = get_post_meta ( $product_id , '_backorders' , true );
2014-06-25 19:20:28 +00:00
}
if ( 'grouped' == $product_type ) {
2015-04-23 15:45:47 +00:00
update_post_meta ( $product_id , '_manage_stock' , 'no' );
update_post_meta ( $product_id , '_backorders' , 'no' );
update_post_meta ( $product_id , '_stock' , '' );
2014-06-25 19:20:28 +00:00
2015-04-23 15:45:47 +00:00
wc_update_product_stock_status ( $product_id , $stock_status );
2014-06-25 19:20:28 +00:00
} elseif ( 'external' == $product_type ) {
2015-04-23 15:45:47 +00:00
update_post_meta ( $product_id , '_manage_stock' , 'no' );
update_post_meta ( $product_id , '_backorders' , 'no' );
update_post_meta ( $product_id , '_stock' , '' );
2014-06-25 19:20:28 +00:00
2015-04-23 15:45:47 +00:00
wc_update_product_stock_status ( $product_id , 'instock' );
2014-06-25 19:20:28 +00:00
} elseif ( 'yes' == $managing_stock ) {
2015-04-23 15:45:47 +00:00
update_post_meta ( $product_id , '_backorders' , $backorders );
2014-06-25 19:20:28 +00:00
2015-04-23 15:45:47 +00:00
wc_update_product_stock_status ( $product_id , $stock_status );
2014-06-25 19:20:28 +00:00
2015-12-17 10:08:17 +00:00
// Stock quantity.
2014-06-25 19:20:28 +00:00
if ( isset ( $data [ 'stock_quantity' ] ) ) {
2015-10-01 13:05:02 +00:00
wc_update_product_stock ( $product_id , wc_stock_amount ( $data [ 'stock_quantity' ] ) );
} else if ( isset ( $data [ 'inventory_delta' ] ) ) {
$stock_quantity = wc_stock_amount ( get_post_meta ( $product_id , '_stock' , true ) );
$stock_quantity += wc_stock_amount ( $data [ 'inventory_delta' ] );
wc_update_product_stock ( $product_id , wc_stock_amount ( $stock_quantity ) );
2014-06-25 19:20:28 +00:00
}
2014-06-24 18:30:11 +00:00
} else {
2014-06-25 19:20:28 +00:00
2015-12-17 10:08:17 +00:00
// Don't manage stock.
2015-04-23 15:45:47 +00:00
update_post_meta ( $product_id , '_manage_stock' , 'no' );
update_post_meta ( $product_id , '_backorders' , $backorders );
update_post_meta ( $product_id , '_stock' , '' );
2014-06-25 19:20:28 +00:00
2015-04-23 15:45:47 +00:00
wc_update_product_stock_status ( $product_id , $stock_status );
2014-06-24 18:30:11 +00:00
}
2014-06-25 19:20:28 +00:00
} else {
2015-04-23 15:45:47 +00:00
wc_update_product_stock_status ( $product_id , $stock_status );
2014-06-24 18:30:11 +00:00
}
2015-12-17 10:08:17 +00:00
// Upsells.
2014-06-25 19:20:28 +00:00
if ( isset ( $data [ 'upsell_ids' ] ) ) {
$upsells = array ();
$ids = $data [ 'upsell_ids' ];
if ( ! empty ( $ids ) ) {
foreach ( $ids as $id ) {
if ( $id && $id > 0 ) {
$upsells [] = $id ;
}
}
2015-04-23 15:45:47 +00:00
update_post_meta ( $product_id , '_upsell_ids' , $upsells );
2014-06-25 19:20:28 +00:00
} else {
2015-04-23 15:45:47 +00:00
delete_post_meta ( $product_id , '_upsell_ids' );
2014-06-25 19:20:28 +00:00
}
}
2015-12-17 10:08:17 +00:00
// Cross sells.
2014-06-25 19:20:28 +00:00
if ( isset ( $data [ 'cross_sell_ids' ] ) ) {
$crosssells = array ();
$ids = $data [ 'cross_sell_ids' ];
if ( ! empty ( $ids ) ) {
foreach ( $ids as $id ) {
if ( $id && $id > 0 ) {
$crosssells [] = $id ;
}
}
2015-04-23 15:45:47 +00:00
update_post_meta ( $product_id , '_crosssell_ids' , $crosssells );
2014-06-25 19:20:28 +00:00
} else {
2015-04-23 15:45:47 +00:00
delete_post_meta ( $product_id , '_crosssell_ids' );
2014-06-25 19:20:28 +00:00
}
}
2015-12-17 10:08:17 +00:00
// Product categories.
2014-06-25 19:38:51 +00:00
if ( isset ( $data [ 'categories' ] ) && is_array ( $data [ 'categories' ] ) ) {
2015-05-29 13:04:02 +00:00
$term_ids = array_unique ( array_map ( 'intval' , $data [ 'categories' ] ) );
wp_set_object_terms ( $product_id , $term_ids , 'product_cat' );
2014-06-25 19:38:51 +00:00
}
2015-12-17 10:08:17 +00:00
// Product tags.
2014-06-25 19:38:51 +00:00
if ( isset ( $data [ 'tags' ] ) && is_array ( $data [ 'tags' ] ) ) {
2015-05-29 13:04:02 +00:00
$term_ids = array_unique ( array_map ( 'intval' , $data [ 'tags' ] ) );
2015-05-29 13:05:15 +00:00
wp_set_object_terms ( $product_id , $term_ids , 'product_tag' );
2014-06-25 19:38:51 +00:00
}
2015-12-17 10:08:17 +00:00
// Downloadable.
2014-06-25 19:20:28 +00:00
if ( isset ( $data [ 'downloadable' ] ) ) {
$is_downloadable = ( true === $data [ 'downloadable' ] ) ? 'yes' : 'no' ;
2015-04-23 15:45:47 +00:00
update_post_meta ( $product_id , '_downloadable' , $is_downloadable );
2014-06-25 19:20:28 +00:00
} else {
2015-04-23 15:45:47 +00:00
$is_downloadable = get_post_meta ( $product_id , '_downloadable' , true );
2014-06-25 19:20:28 +00:00
}
2015-12-17 10:08:17 +00:00
// Downloadable options.
2014-06-25 19:20:28 +00:00
if ( 'yes' == $is_downloadable ) {
2015-12-17 10:08:17 +00:00
// Downloadable files.
2014-06-25 19:27:24 +00:00
if ( isset ( $data [ 'downloads' ] ) && is_array ( $data [ 'downloads' ] ) ) {
2015-04-23 15:45:47 +00:00
$this -> save_downloadable_files ( $product_id , $data [ 'downloads' ] );
2014-06-25 19:20:28 +00:00
}
2015-12-17 10:08:17 +00:00
// Download limit.
2014-06-25 19:20:28 +00:00
if ( isset ( $data [ 'download_limit' ] ) ) {
2015-04-23 15:45:47 +00:00
update_post_meta ( $product_id , '_download_limit' , ( '' === $data [ 'download_limit' ] ) ? '' : absint ( $data [ 'download_limit' ] ) );
2014-06-25 19:20:28 +00:00
}
2015-12-17 10:08:17 +00:00
// Download expiry.
2014-06-25 19:20:28 +00:00
if ( isset ( $data [ 'download_expiry' ] ) ) {
2015-04-23 15:45:47 +00:00
update_post_meta ( $product_id , '_download_expiry' , ( '' === $data [ 'download_expiry' ] ) ? '' : absint ( $data [ 'download_expiry' ] ) );
2014-06-25 19:20:28 +00:00
}
2015-12-17 10:08:17 +00:00
// Download type.
2014-06-25 19:20:28 +00:00
if ( isset ( $data [ 'download_type' ] ) ) {
2015-04-23 15:45:47 +00:00
update_post_meta ( $product_id , '_download_type' , wc_clean ( $data [ 'download_type' ] ) );
2014-06-25 19:20:28 +00:00
}
}
2015-12-17 10:08:17 +00:00
// Product url.
2014-06-25 19:20:28 +00:00
if ( $product_type == 'external' ) {
if ( isset ( $data [ 'product_url' ] ) ) {
2015-04-23 15:45:47 +00:00
update_post_meta ( $product_id , '_product_url' , wc_clean ( $data [ 'product_url' ] ) );
2014-06-25 19:20:28 +00:00
}
if ( isset ( $data [ 'button_text' ] ) ) {
2015-04-23 15:45:47 +00:00
update_post_meta ( $product_id , '_button_text' , wc_clean ( $data [ 'button_text' ] ) );
2014-06-25 19:20:28 +00:00
}
}
2015-12-17 10:08:17 +00:00
// Reviews allowed.
2015-01-21 23:09:08 +00:00
if ( isset ( $data [ 'reviews_allowed' ] ) ) {
$reviews_allowed = ( true === $data [ 'reviews_allowed' ] ) ? 'open' : 'closed' ;
2015-04-23 15:45:47 +00:00
$wpdb -> update ( $wpdb -> posts , array ( 'comment_status' => $reviews_allowed ), array ( 'ID' => $product_id ) );
2015-01-21 23:09:08 +00:00
}
2014-06-26 19:51:03 +00:00
// Do action for product type
2015-04-23 15:45:47 +00:00
do_action ( 'woocommerce_api_process_product_meta_' . $product_type , $product_id , $data );
2014-06-26 19:51:03 +00:00
2014-06-26 19:47:54 +00:00
return true ;
}
/**
* Save variations
*
2015-11-17 12:51:42 +00:00
* @ since 2.2
* @ param int $id
* @ param array $data
2015-01-13 15:44:30 +00:00
* @ return bool
2015-11-17 12:51:42 +00:00
* @ throws WC_API_Exception
2014-06-26 19:47:54 +00:00
*/
protected function save_variations ( $id , $data ) {
global $wpdb ;
$variations = $data [ 'variations' ];
$attributes = ( array ) maybe_unserialize ( get_post_meta ( $id , '_product_attributes' , true ) );
foreach ( $variations as $menu_order => $variation ) {
$variation_id = isset ( $variation [ 'id' ] ) ? absint ( $variation [ 'id' ] ) : 0 ;
// Generate a useful post title
$variation_post_title = sprintf ( __ ( 'Variation #%s of %s' , 'woocommerce' ), $variation_id , esc_html ( get_the_title ( $id ) ) );
// Update or Add post
if ( ! $variation_id ) {
$post_status = ( isset ( $variation [ 'visible' ] ) && false === $variation [ 'visible' ] ) ? 'private' : 'publish' ;
2014-06-30 19:36:13 +00:00
$new_variation = array (
2014-06-26 19:47:54 +00:00
'post_title' => $variation_post_title ,
'post_content' => '' ,
'post_status' => $post_status ,
'post_author' => get_current_user_id (),
'post_parent' => $id ,
'post_type' => 'product_variation' ,
'menu_order' => $menu_order
);
2014-06-30 19:36:13 +00:00
$variation_id = wp_insert_post ( $new_variation );
2014-06-26 19:47:54 +00:00
do_action ( 'woocommerce_create_product_variation' , $variation_id );
} else {
$update_variation = array ( 'post_title' => $variation_post_title , 'menu_order' => $menu_order );
if ( isset ( $variation [ 'visible' ] ) ) {
$post_status = ( false === $variation [ 'visible' ] ) ? 'private' : 'publish' ;
$update_variation [ 'post_status' ] = $post_status ;
}
$wpdb -> update ( $wpdb -> posts , $update_variation , array ( 'ID' => $variation_id ) );
do_action ( 'woocommerce_update_product_variation' , $variation_id );
}
// Stop with we don't have a variation ID
if ( is_wp_error ( $variation_id ) ) {
2015-01-05 19:26:24 +00:00
throw new WC_API_Exception ( 'woocommerce_api_cannot_save_product_variation' , $variation_id -> get_error_message (), 400 );
2014-06-26 19:47:54 +00:00
}
// SKU
if ( isset ( $variation [ 'sku' ] ) ) {
2014-06-27 18:58:29 +00:00
$sku = get_post_meta ( $variation_id , '_sku' , true );
$new_sku = wc_clean ( $variation [ 'sku' ] );
if ( '' == $new_sku ) {
update_post_meta ( $variation_id , '_sku' , '' );
} elseif ( $new_sku !== $sku ) {
if ( ! empty ( $new_sku ) ) {
2014-06-27 19:03:25 +00:00
$unique_sku = wc_product_has_unique_sku ( $variation_id , $new_sku );
2014-06-27 18:58:29 +00:00
if ( ! $unique_sku ) {
2015-01-05 19:26:24 +00:00
throw new WC_API_Exception ( 'woocommerce_api_product_sku_already_exists' , __ ( 'The SKU already exists on another product' , 'woocommerce' ), 400 );
2014-06-27 18:58:29 +00:00
} else {
update_post_meta ( $variation_id , '_sku' , $new_sku );
}
} else {
update_post_meta ( $variation_id , '_sku' , '' );
}
2014-06-26 19:47:54 +00:00
}
}
2016-01-22 18:43:52 +00:00
// Thumbnail.
2014-06-26 19:47:54 +00:00
if ( isset ( $variation [ 'image' ] ) && is_array ( $variation [ 'image' ] ) ) {
$image = current ( $variation [ 'image' ] );
if ( $image && is_array ( $image ) ) {
if ( isset ( $image [ 'position' ] ) && isset ( $image [ 'src' ] ) && $image [ 'position' ] == 0 ) {
$upload = $this -> upload_product_image ( wc_clean ( $image [ 'src' ] ) );
2016-01-22 18:43:52 +00:00
2014-06-26 19:47:54 +00:00
if ( is_wp_error ( $upload ) ) {
2015-01-05 19:26:24 +00:00
throw new WC_API_Exception ( 'woocommerce_api_cannot_upload_product_image' , $upload -> get_error_message (), 400 );
2014-06-26 19:47:54 +00:00
}
2016-01-22 18:43:52 +00:00
2014-06-26 19:47:54 +00:00
$attachment_id = $this -> set_product_image_as_attachment ( $upload , $id );
2016-01-22 18:43:52 +00:00
// Set the image alt if present.
if ( ! empty ( $image [ 'alt' ] ) ) {
update_post_meta ( $attachment_id , '_wp_attachment_image_alt' , wc_clean ( $image [ 'alt' ] ) );
}
// Set the image title if present.
if ( ! empty ( $image [ 'title' ] ) ) {
wp_update_post ( array ( 'ID' => $attachment_id , 'post_title' => $image [ 'title' ] ) );
}
2014-06-26 19:47:54 +00:00
update_post_meta ( $variation_id , '_thumbnail_id' , $attachment_id );
}
} else {
delete_post_meta ( $variation_id , '_thumbnail_id' );
}
}
// Virtual variation
if ( isset ( $variation [ 'virtual' ] ) ) {
$is_virtual = ( true === $variation [ 'virtual' ] ) ? 'yes' : 'no' ;
update_post_meta ( $variation_id , '_virtual' , $is_virtual );
}
// Downloadable variation
if ( isset ( $variation [ 'downloadable' ] ) ) {
$is_downloadable = ( true === $variation [ 'downloadable' ] ) ? 'yes' : 'no' ;
update_post_meta ( $variation_id , '_downloadable' , $is_downloadable );
} else {
$is_downloadable = get_post_meta ( $variation_id , '_downloadable' , true );
}
// Shipping data
$this -> save_product_shipping_data ( $variation_id , $variation );
// Stock handling
2015-02-21 01:01:26 +00:00
if ( isset ( $variation [ 'managing_stock' ] ) ) {
$managing_stock = ( true === $variation [ 'managing_stock' ] ) ? 'yes' : 'no' ;
update_post_meta ( $variation_id , '_manage_stock' , $managing_stock );
2014-11-12 14:21:26 +00:00
} else {
2015-02-21 01:01:26 +00:00
$managing_stock = get_post_meta ( $variation_id , '_manage_stock' , true );
2014-06-26 19:47:54 +00:00
}
2014-11-12 14:21:26 +00:00
// Only update stock status to user setting if changed by the user, but do so before looking at stock levels at variation level
if ( isset ( $variation [ 'in_stock' ] ) ) {
$stock_status = ( true === $variation [ 'in_stock' ] ) ? 'instock' : 'outofstock' ;
wc_update_product_stock_status ( $variation_id , $stock_status );
}
2015-02-21 01:01:26 +00:00
if ( 'yes' === $managing_stock ) {
2015-12-01 21:14:41 +00:00
$backorders = get_post_meta ( $variation_id , '_backorders' , true );
2014-11-12 14:21:26 +00:00
if ( isset ( $variation [ 'backorders' ] ) ) {
2014-06-26 19:47:54 +00:00
if ( 'notify' == $variation [ 'backorders' ] ) {
$backorders = 'notify' ;
} else {
$backorders = ( true === $variation [ 'backorders' ] ) ? 'yes' : 'no' ;
}
}
2014-11-12 14:21:26 +00:00
2015-12-01 21:14:41 +00:00
update_post_meta ( $variation_id , '_backorders' , '' === $backorders ? 'no' : $backorders );
2015-03-16 18:13:07 +00:00
if ( isset ( $variation [ 'stock_quantity' ] ) ) {
wc_update_product_stock ( $variation_id , wc_stock_amount ( $variation [ 'stock_quantity' ] ) );
2015-10-01 13:05:02 +00:00
} else if ( isset ( $data [ 'inventory_delta' ] ) ) {
$stock_quantity = wc_stock_amount ( get_post_meta ( $variation_id , '_stock' , true ) );
$stock_quantity += wc_stock_amount ( $data [ 'inventory_delta' ] );
wc_update_product_stock ( $variation_id , wc_stock_amount ( $stock_quantity ) );
2015-03-16 18:13:07 +00:00
}
2014-11-12 14:21:26 +00:00
} else {
delete_post_meta ( $variation_id , '_backorders' );
delete_post_meta ( $variation_id , '_stock' );
2014-06-26 19:47:54 +00:00
}
// Regular Price
if ( isset ( $variation [ 'regular_price' ] ) ) {
2016-01-07 22:52:01 +00:00
$regular_price = ( '' === $variation [ 'regular_price' ] ) ? '' : $variation [ 'regular_price' ];
2014-06-26 19:47:54 +00:00
} else {
$regular_price = get_post_meta ( $variation_id , '_regular_price' , true );
}
// Sale Price
if ( isset ( $variation [ 'sale_price' ] ) ) {
2016-01-07 22:52:01 +00:00
$sale_price = ( '' === $variation [ 'sale_price' ] ) ? '' : $variation [ 'sale_price' ];
2014-06-26 19:47:54 +00:00
} else {
$sale_price = get_post_meta ( $variation_id , '_sale_price' , true );
}
2016-01-08 12:34:19 +00:00
if ( isset ( $variation [ 'sale_price_dates_from' ] ) ) {
$date_from = $variation [ 'sale_price_dates_from' ];
2014-06-26 19:47:54 +00:00
} else {
2016-01-08 12:34:19 +00:00
$date_from = get_post_meta ( $variation_id , '_sale_price_dates_from' , true );
2016-01-11 10:27:55 +00:00
$date_from = ( '' === $date_from ) ? '' : date ( 'Y-m-d' , $date_from );
2014-06-26 19:47:54 +00:00
}
2016-01-08 12:34:19 +00:00
if ( isset ( $variation [ 'sale_price_dates_to' ] ) ) {
$date_to = $variation [ 'sale_price_dates_to' ];
2014-06-26 19:47:54 +00:00
} else {
2016-01-08 12:34:19 +00:00
$date_to = get_post_meta ( $variation_id , '_sale_price_dates_to' , true );
2016-01-11 10:27:55 +00:00
$date_to = ( '' === $date_to ) ? '' : date ( 'Y-m-d' , $date_to );
2014-06-26 19:47:54 +00:00
}
2016-01-07 22:52:01 +00:00
_wc_save_product_price ( $variation_id , $regular_price , $sale_price , $date_from , $date_to );
2014-06-26 19:47:54 +00:00
// Tax class
if ( isset ( $variation [ 'tax_class' ] ) ) {
if ( $variation [ 'tax_class' ] !== 'parent' ) {
update_post_meta ( $variation_id , '_tax_class' , wc_clean ( $variation [ 'tax_class' ] ) );
} else {
delete_post_meta ( $variation_id , '_tax_class' );
}
}
// Downloads
if ( 'yes' == $is_downloadable ) {
// Downloadable files
if ( isset ( $variation [ 'downloads' ] ) && is_array ( $variation [ 'downloads' ] ) ) {
$this -> save_downloadable_files ( $id , $variation [ 'downloads' ], $variation_id );
}
// Download limit
if ( isset ( $variation [ 'download_limit' ] ) ) {
$download_limit = absint ( $variation [ 'download_limit' ] );
update_post_meta ( $variation_id , '_download_limit' , ( ! $download_limit ) ? '' : $download_limit );
}
// Download expiry
if ( isset ( $variation [ 'download_expiry' ] ) ) {
$download_expiry = absint ( $variation [ 'download_expiry' ] );
update_post_meta ( $variation_id , '_download_expiry' , ( ! $download_expiry ) ? '' : $download_expiry );
}
} else {
update_post_meta ( $variation_id , '_download_limit' , '' );
update_post_meta ( $variation_id , '_download_expiry' , '' );
update_post_meta ( $variation_id , '_downloadable_files' , '' );
}
2015-11-29 21:57:08 +00:00
// Description.
if ( isset ( $variation [ 'description' ] ) ) {
update_post_meta ( $variation_id , '_variation_description' , wp_kses_post ( $variation [ 'description' ] ) );
}
2014-06-26 19:47:54 +00:00
// Update taxonomies
2014-06-30 19:36:13 +00:00
if ( isset ( $variation [ 'attributes' ] ) ) {
2014-06-26 19:47:54 +00:00
$updated_attribute_keys = array ();
foreach ( $variation [ 'attributes' ] as $attribute_key => $attribute ) {
if ( ! isset ( $attribute [ 'name' ] ) ) {
continue ;
}
2015-10-28 18:31:05 +00:00
$taxonomy = 0 ;
2015-01-19 13:11:30 +00:00
$_attribute = array ();
2015-01-13 15:24:28 +00:00
if ( isset ( $attribute [ 'slug' ] ) ) {
$taxonomy = $this -> get_attribute_taxonomy_by_slug ( $attribute [ 'slug' ] );
}
2015-09-17 05:57:04 +00:00
if ( ! $taxonomy ) {
$taxonomy = sanitize_title ( $attribute [ 'name' ] );
}
2014-06-26 19:47:54 +00:00
if ( isset ( $attributes [ $taxonomy ] ) ) {
$_attribute = $attributes [ $taxonomy ];
2014-08-28 19:25:08 +00:00
}
2014-06-26 19:47:54 +00:00
2014-08-28 19:25:08 +00:00
if ( isset ( $_attribute [ 'is_variation' ] ) && $_attribute [ 'is_variation' ] ) {
2015-07-23 00:07:05 +00:00
$_attribute_key = 'attribute_' . sanitize_title ( $_attribute [ 'name' ] );
$updated_attribute_keys [] = $_attribute_key ;
2015-01-19 13:11:30 +00:00
2015-07-23 00:07:05 +00:00
if ( isset ( $_attribute [ 'is_taxonomy' ] ) && $_attribute [ 'is_taxonomy' ] ) {
// Don't use wc_clean as it destroys sanitized characters
$_attribute_value = isset ( $attribute [ 'option' ] ) ? sanitize_title ( stripslashes ( $attribute [ 'option' ] ) ) : '' ;
} else {
$_attribute_value = isset ( $attribute [ 'option' ] ) ? wc_clean ( stripslashes ( $attribute [ 'option' ] ) ) : '' ;
}
update_post_meta ( $variation_id , $_attribute_key , $_attribute_value );
2014-06-26 19:47:54 +00:00
}
}
// Remove old taxonomies attributes so data is kept up to date - first get attribute key names
$delete_attribute_keys = $wpdb -> get_col ( $wpdb -> prepare ( " SELECT meta_key FROM { $wpdb -> postmeta } WHERE meta_key LIKE 'attribute_%%' AND meta_key NOT IN ( ' " . implode ( " ',' " , $updated_attribute_keys ) . " ' ) AND post_id = %d; " , $variation_id ) );
foreach ( $delete_attribute_keys as $key ) {
delete_post_meta ( $variation_id , $key );
}
}
do_action ( 'woocommerce_api_save_product_variation' , $variation_id , $menu_order , $variation );
}
// Update parent if variable so price sorting works and stays in sync with the cheapest child
WC_Product_Variable :: sync ( $id );
2015-01-22 20:49:09 +00:00
// Update default attributes options setting
if ( isset ( $data [ 'default_attribute' ] ) ) {
$data [ 'default_attributes' ] = $data [ 'default_attribute' ];
}
if ( isset ( $data [ 'default_attributes' ] ) && is_array ( $data [ 'default_attributes' ] ) ) {
2014-06-26 19:47:54 +00:00
$default_attributes = array ();
2015-01-22 20:49:09 +00:00
foreach ( $data [ 'default_attributes' ] as $default_attr_key => $default_attr ) {
2014-06-26 19:47:54 +00:00
if ( ! isset ( $default_attr [ 'name' ] ) ) {
continue ;
}
2015-02-03 14:41:50 +00:00
$taxonomy = sanitize_title ( $default_attr [ 'name' ] );
2015-01-19 13:33:33 +00:00
2015-02-03 14:41:50 +00:00
if ( isset ( $default_attr [ 'slug' ] ) ) {
$taxonomy = $this -> get_attribute_taxonomy_by_slug ( $default_attr [ 'slug' ] );
2015-01-19 13:33:33 +00:00
}
2014-06-26 19:47:54 +00:00
if ( isset ( $attributes [ $taxonomy ] ) ) {
$_attribute = $attributes [ $taxonomy ];
if ( $_attribute [ 'is_variation' ] ) {
2015-07-23 00:40:06 +00:00
$value = '' ;
2014-06-26 19:47:54 +00:00
if ( isset ( $default_attr [ 'option' ] ) ) {
2015-07-23 00:40:06 +00:00
if ( $_attribute [ 'is_taxonomy' ] ) {
// Don't use wc_clean as it destroys sanitized characters
$value = sanitize_title ( trim ( stripslashes ( $default_attr [ 'option' ] ) ) );
} else {
$value = wc_clean ( trim ( stripslashes ( $default_attr [ 'option' ] ) ) );
}
2014-06-26 19:47:54 +00:00
}
if ( $value ) {
$default_attributes [ $taxonomy ] = $value ;
}
}
}
}
update_post_meta ( $id , '_default_attributes' , $default_attributes );
}
2014-06-25 19:20:28 +00:00
2014-06-24 18:30:11 +00:00
return true ;
}
2014-06-26 19:47:54 +00:00
/**
* Save product shipping data
*
* @ since 2.2
* @ param int $id
* @ param array $data
*/
private function save_product_shipping_data ( $id , $data ) {
if ( isset ( $data [ 'weight' ] ) ) {
update_post_meta ( $id , '_weight' , ( '' === $data [ 'weight' ] ) ? '' : wc_format_decimal ( $data [ 'weight' ] ) );
}
// Product dimensions
if ( isset ( $data [ 'dimensions' ] ) ) {
// Height
if ( isset ( $data [ 'dimensions' ][ 'height' ] ) ) {
update_post_meta ( $id , '_height' , ( '' === $data [ 'dimensions' ][ 'height' ] ) ? '' : wc_format_decimal ( $data [ 'dimensions' ][ 'height' ] ) );
}
// Width
if ( isset ( $data [ 'dimensions' ][ 'width' ] ) ) {
update_post_meta ( $id , '_width' , ( '' === $data [ 'dimensions' ][ 'width' ] ) ? '' : wc_format_decimal ( $data [ 'dimensions' ][ 'width' ] ) );
}
// Length
if ( isset ( $data [ 'dimensions' ][ 'length' ] ) ) {
update_post_meta ( $id , '_length' , ( '' === $data [ 'dimensions' ][ 'length' ] ) ? '' : wc_format_decimal ( $data [ 'dimensions' ][ 'length' ] ) );
}
}
// Virtual
if ( isset ( $data [ 'virtual' ] ) ) {
$virtual = ( true === $data [ 'virtual' ] ) ? 'yes' : 'no' ;
if ( 'yes' == $virtual ) {
update_post_meta ( $id , '_weight' , '' );
update_post_meta ( $id , '_length' , '' );
update_post_meta ( $id , '_width' , '' );
update_post_meta ( $id , '_height' , '' );
}
}
// Shipping class
if ( isset ( $data [ 'shipping_class' ] ) ) {
wp_set_object_terms ( $id , wc_clean ( $data [ 'shipping_class' ] ), 'product_shipping_class' );
}
}
/**
* Save downloadable files
*
* @ since 2.2
* @ param int $product_id
* @ param array $downloads
* @ param int $variation_id
*/
private function save_downloadable_files ( $product_id , $downloads , $variation_id = 0 ) {
$files = array ();
2015-01-05 19:26:24 +00:00
// File paths will be stored in an array keyed off md5(file path)
2014-06-26 19:47:54 +00:00
foreach ( $downloads as $key => $file ) {
2015-01-22 21:20:05 +00:00
if ( isset ( $file [ 'url' ] ) ) {
$file [ 'file' ] = $file [ 'url' ];
}
if ( ! isset ( $file [ 'file' ] ) ) {
2014-06-26 19:47:54 +00:00
continue ;
}
$file_name = isset ( $file [ 'name' ] ) ? wc_clean ( $file [ 'name' ] ) : '' ;
2015-06-10 16:12:10 +00:00
if ( 0 === strpos ( $file [ 'file' ], 'http' ) ) {
$file_url = esc_url_raw ( $file [ 'file' ] );
} else {
$file_url = wc_clean ( $file [ 'file' ] );
}
2014-06-26 19:47:54 +00:00
$files [ md5 ( $file_url ) ] = array (
'name' => $file_name ,
'file' => $file_url
);
}
2015-01-05 19:26:24 +00:00
// Grant permission to any newly added files on any existing orders for this product prior to saving
2014-06-26 19:47:54 +00:00
do_action ( 'woocommerce_process_product_file_download_paths' , $product_id , $variation_id , $files );
2015-04-25 15:19:30 +00:00
$id = ( 0 === $variation_id ) ? $product_id : $variation_id ;
update_post_meta ( $id , '_downloadable_files' , $files );
2014-06-26 19:47:54 +00:00
}
/**
2015-01-13 15:24:28 +00:00
* Get attribute taxonomy by slug .
2014-06-26 19:47:54 +00:00
*
2014-06-26 19:55:24 +00:00
* @ since 2.2
2015-01-13 15:44:30 +00:00
* @ param string $slug
2014-09-07 23:37:55 +00:00
* @ return string | null
2014-06-26 19:47:54 +00:00
*/
2015-01-13 15:24:28 +00:00
private function get_attribute_taxonomy_by_slug ( $slug ) {
2014-06-26 19:47:54 +00:00
$taxonomy = null ;
$attribute_taxonomies = wc_get_attribute_taxonomies ();
foreach ( $attribute_taxonomies as $key => $tax ) {
2015-01-22 20:38:39 +00:00
if ( $slug == $tax -> attribute_name ) {
2014-06-26 19:47:54 +00:00
$taxonomy = 'pa_' . $tax -> attribute_name ;
break ;
}
}
return $taxonomy ;
}
2013-11-14 17:40:35 +00:00
/**
* Get the images for a product or product variation
*
* @ since 2.1
* @ param WC_Product | WC_Product_Variation $product
* @ return array
*/
private function get_images ( $product ) {
$images = $attachment_ids = array ();
if ( $product -> is_type ( 'variation' ) ) {
if ( has_post_thumbnail ( $product -> get_variation_id () ) ) {
2015-01-05 19:26:24 +00:00
// Add variation image if set
2013-11-14 17:40:35 +00:00
$attachment_ids [] = get_post_thumbnail_id ( $product -> get_variation_id () );
} elseif ( has_post_thumbnail ( $product -> id ) ) {
2015-01-05 19:26:24 +00:00
// Otherwise use the parent product featured image if set
2013-11-14 17:40:35 +00:00
$attachment_ids [] = get_post_thumbnail_id ( $product -> id );
}
} else {
2015-01-05 19:26:24 +00:00
// Add featured image
2013-11-14 17:40:35 +00:00
if ( has_post_thumbnail ( $product -> id ) ) {
$attachment_ids [] = get_post_thumbnail_id ( $product -> id );
}
2015-01-05 19:26:24 +00:00
// Add gallery images
2013-11-14 17:40:35 +00:00
$attachment_ids = array_merge ( $attachment_ids , $product -> get_gallery_attachment_ids () );
}
2015-01-05 19:26:24 +00:00
// Build image data
2013-11-14 17:40:35 +00:00
foreach ( $attachment_ids as $position => $attachment_id ) {
$attachment_post = get_post ( $attachment_id );
2014-06-26 19:55:24 +00:00
if ( is_null ( $attachment_post ) ) {
2013-11-14 17:40:35 +00:00
continue ;
2014-06-26 19:55:24 +00:00
}
2013-11-14 17:40:35 +00:00
$attachment = wp_get_attachment_image_src ( $attachment_id , 'full' );
2014-06-26 19:55:24 +00:00
if ( ! is_array ( $attachment ) ) {
2013-11-14 17:40:35 +00:00
continue ;
2014-06-26 19:55:24 +00:00
}
2013-11-14 17:40:35 +00:00
$images [] = array (
'id' => ( int ) $attachment_id ,
2013-11-18 21:47:38 +00:00
'created_at' => $this -> server -> format_datetime ( $attachment_post -> post_date_gmt ),
'updated_at' => $this -> server -> format_datetime ( $attachment_post -> post_modified_gmt ),
2013-11-14 17:40:35 +00:00
'src' => current ( $attachment ),
'title' => get_the_title ( $attachment_id ),
'alt' => get_post_meta ( $attachment_id , '_wp_attachment_image_alt' , true ),
2015-04-03 18:01:41 +00:00
'position' => ( int ) $position ,
2013-11-14 17:40:35 +00:00
);
}
2015-01-05 19:26:24 +00:00
// Set a placeholder image if the product has no images set
2013-11-14 17:40:35 +00:00
if ( empty ( $images ) ) {
$images [] = array (
'id' => 0 ,
2015-01-05 19:26:24 +00:00
'created_at' => $this -> server -> format_datetime ( time () ), // Default to now
2013-11-18 21:47:38 +00:00
'updated_at' => $this -> server -> format_datetime ( time () ),
2013-11-25 13:56:59 +00:00
'src' => wc_placeholder_img_src (),
2013-11-14 17:40:35 +00:00
'title' => __ ( 'Placeholder' , 'woocommerce' ),
'alt' => __ ( 'Placeholder' , 'woocommerce' ),
'position' => 0 ,
);
}
return $images ;
}
2014-06-24 18:19:34 +00:00
/**
2016-01-22 18:43:52 +00:00
* Save product images .
2014-06-24 18:19:34 +00:00
*
2015-11-17 12:51:42 +00:00
* @ since 2.2
* @ param array $images
* @ param int $id
* @ throws WC_API_Exception
2014-06-24 18:19:34 +00:00
*/
2014-06-24 20:28:36 +00:00
protected function save_product_images ( $id , $images ) {
2014-06-30 21:02:31 +00:00
if ( is_array ( $images ) ) {
$gallery = array ();
2014-06-30 20:57:42 +00:00
2014-06-30 21:02:31 +00:00
foreach ( $images as $image ) {
if ( isset ( $image [ 'position' ] ) && $image [ 'position' ] == 0 ) {
$attachment_id = isset ( $image [ 'id' ] ) ? absint ( $image [ 'id' ] ) : 0 ;
2014-06-30 20:57:42 +00:00
2014-06-30 21:02:31 +00:00
if ( 0 === $attachment_id && isset ( $image [ 'src' ] ) ) {
2015-06-10 15:34:36 +00:00
$upload = $this -> upload_product_image ( esc_url_raw ( $image [ 'src' ] ) );
2014-06-26 19:55:24 +00:00
2014-06-30 21:02:31 +00:00
if ( is_wp_error ( $upload ) ) {
2015-01-05 19:26:24 +00:00
throw new WC_API_Exception ( 'woocommerce_api_cannot_upload_product_image' , $upload -> get_error_message (), 400 );
2014-06-30 21:02:31 +00:00
}
2014-06-30 20:57:42 +00:00
2014-06-30 21:02:31 +00:00
$attachment_id = $this -> set_product_image_as_attachment ( $upload , $id );
}
2014-06-26 19:55:24 +00:00
2014-06-30 21:02:31 +00:00
set_post_thumbnail ( $id , $attachment_id );
} else {
$attachment_id = isset ( $image [ 'id' ] ) ? absint ( $image [ 'id' ] ) : 0 ;
2014-06-26 19:55:24 +00:00
2014-06-30 21:02:31 +00:00
if ( 0 === $attachment_id && isset ( $image [ 'src' ] ) ) {
2015-06-10 15:34:36 +00:00
$upload = $this -> upload_product_image ( esc_url_raw ( $image [ 'src' ] ) );
2014-06-30 20:57:42 +00:00
2014-06-30 21:02:31 +00:00
if ( is_wp_error ( $upload ) ) {
2015-01-05 19:26:24 +00:00
throw new WC_API_Exception ( 'woocommerce_api_cannot_upload_product_image' , $upload -> get_error_message (), 400 );
2014-06-30 21:02:31 +00:00
}
2014-06-26 19:55:24 +00:00
2016-01-22 18:43:52 +00:00
$attachment_id = $this -> set_product_image_as_attachment ( $upload , $id );
2014-12-10 17:23:36 +00:00
}
2016-01-22 18:43:52 +00:00
$gallery [] = $attachment_id ;
2014-06-30 21:02:31 +00:00
}
2015-09-15 15:17:14 +00:00
// Set the image alt if present.
2016-01-22 18:43:52 +00:00
if ( ! empty ( $image [ 'alt' ] ) && $attachment_id ) {
2015-09-15 17:25:43 +00:00
update_post_meta ( $attachment_id , '_wp_attachment_image_alt' , wc_clean ( $image [ 'alt' ] ) );
2015-09-15 15:17:14 +00:00
}
// Set the image title if present.
2016-01-22 18:43:52 +00:00
if ( ! empty ( $image [ 'title' ] ) && $attachment_id ) {
2015-09-15 15:17:14 +00:00
wp_update_post ( array ( 'ID' => $attachment_id , 'post_title' => $image [ 'title' ] ) );
}
2014-06-24 18:19:34 +00:00
}
2014-06-30 20:57:42 +00:00
2014-06-30 21:02:31 +00:00
if ( ! empty ( $gallery ) ) {
update_post_meta ( $id , '_product_image_gallery' , implode ( ',' , $gallery ) );
}
} else {
delete_post_thumbnail ( $id );
update_post_meta ( $id , '_product_image_gallery' , '' );
2014-06-30 20:57:42 +00:00
}
2014-06-24 18:19:34 +00:00
}
/**
* Upload image from URL
*
* @ since 2.2
* @ param string $image_url
2015-02-03 14:41:50 +00:00
* @ return int | WP_Error attachment id
2014-06-24 18:19:34 +00:00
*/
public function upload_product_image ( $image_url ) {
2016-01-22 18:43:52 +00:00
return $this -> upload_image_from_url ( $image_url , 'product_image' );
2015-09-10 06:33:05 +00:00
}
/**
* Upload product category image from URL .
*
2016-01-22 18:43:52 +00:00
* @ since 2.5 . 0
* @ param string $image_url
2015-09-10 06:33:05 +00:00
* @ return int | WP_Error attachment id
*/
public function upload_product_category_image ( $image_url ) {
return $this -> upload_image_from_url ( $image_url , 'product_category_image' );
}
/**
* Upload image from URL .
*
* @ throws WC_API_Exception
*
2016-01-22 18:43:52 +00:00
* @ since 2.5 . 0
* @ param string $image_url
* @ param string $upload_for
2015-09-10 06:33:05 +00:00
* @ return int | WP_Error Attachment id
*/
protected function upload_image_from_url ( $image_url , $upload_for = 'product_image' ) {
2014-06-24 18:19:34 +00:00
$file_name = basename ( current ( explode ( '?' , $image_url ) ) );
$wp_filetype = wp_check_filetype ( $file_name , null );
$parsed_url = @ parse_url ( $image_url );
2015-09-10 06:33:05 +00:00
// Check parsed URL.
2014-06-24 18:19:34 +00:00
if ( ! $parsed_url || ! is_array ( $parsed_url ) ) {
2015-09-10 06:33:05 +00:00
throw new WC_API_Exception ( 'woocommerce_api_invalid_' . $upload_for , sprintf ( __ ( 'Invalid URL %s' , 'woocommerce' ), $image_url ), 400 );
2014-06-24 18:19:34 +00:00
}
2016-01-22 18:43:52 +00:00
// Ensure url is valid.
2014-06-24 18:19:34 +00:00
$image_url = str_replace ( ' ' , '%20' , $image_url );
2016-01-22 18:43:52 +00:00
// Get the file.
2015-05-21 13:45:28 +00:00
$response = wp_safe_remote_get ( $image_url , array (
2014-06-24 18:19:34 +00:00
'timeout' => 10
) );
if ( is_wp_error ( $response ) || 200 !== wp_remote_retrieve_response_code ( $response ) ) {
2015-09-10 06:33:05 +00:00
throw new WC_API_Exception ( 'woocommerce_api_invalid_remote_' . $upload_for , sprintf ( __ ( 'Error getting remote image %s' , 'woocommerce' ), $image_url ), 400 );
2014-06-24 18:19:34 +00:00
}
2016-01-22 18:43:52 +00:00
// Ensure we have a file name and type.
2014-06-24 18:19:34 +00:00
if ( ! $wp_filetype [ 'type' ] ) {
$headers = wp_remote_retrieve_headers ( $response );
if ( isset ( $headers [ 'content-disposition' ] ) && strstr ( $headers [ 'content-disposition' ], 'filename=' ) ) {
$disposition = end ( explode ( 'filename=' , $headers [ 'content-disposition' ] ) );
$disposition = sanitize_file_name ( $disposition );
$file_name = $disposition ;
} elseif ( isset ( $headers [ 'content-type' ] ) && strstr ( $headers [ 'content-type' ], 'image/' ) ) {
$file_name = 'image.' . str_replace ( 'image/' , '' , $headers [ 'content-type' ] );
}
unset ( $headers );
}
2016-01-22 18:43:52 +00:00
// Upload the file.
2014-06-24 18:19:34 +00:00
$upload = wp_upload_bits ( $file_name , '' , wp_remote_retrieve_body ( $response ) );
if ( $upload [ 'error' ] ) {
2015-09-10 06:33:05 +00:00
throw new WC_API_Exception ( 'woocommerce_api_' . $upload_for . '_upload_error' , $upload [ 'error' ], 400 );
2014-06-24 18:19:34 +00:00
}
2016-01-22 18:43:52 +00:00
// Get filesize.
2014-06-24 18:19:34 +00:00
$filesize = filesize ( $upload [ 'file' ] );
if ( 0 == $filesize ) {
@ unlink ( $upload [ 'file' ] );
unset ( $upload );
2015-09-10 06:33:05 +00:00
throw new WC_API_Exception ( 'woocommerce_api_' . $upload_for . '_upload_file_error' , __ ( 'Zero size file downloaded' , 'woocommerce' ), 400 );
2014-06-24 18:19:34 +00:00
}
unset ( $response );
2015-11-11 00:59:17 +00:00
do_action ( 'woocommerce_api_uploaded_image_from_url' , $upload , $image_url , $upload_for );
2014-06-24 18:19:34 +00:00
return $upload ;
}
/**
2015-09-10 06:33:05 +00:00
* Sets product image as attachment and returns the attachment ID .
2014-06-24 18:19:34 +00:00
*
* @ since 2.2
2014-09-07 23:37:55 +00:00
* @ param integer $upload
2014-06-25 16:27:48 +00:00
* @ param int $id
2014-06-24 18:19:34 +00:00
* @ return int
*/
2014-06-25 16:27:48 +00:00
protected function set_product_image_as_attachment ( $upload , $id ) {
2015-09-10 06:33:05 +00:00
return $this -> set_uploaded_image_as_attachment ( $upload , $id );
}
/**
* Sets uploaded category image as attachment and returns the attachment ID .
*
* @ since 2.5 . 0
* @ param integer $upload Upload information from wp_upload_bits
* @ return int Attachment ID
*/
protected function set_product_category_image_as_attachment ( $upload ) {
return $this -> set_uploaded_image_as_attachment ( $upload );
}
/**
* Set uploaded image as attachment .
*
* @ since 2.5 . 0
* @ param array $upload Upload information from wp_upload_bits
* @ param int $id Post ID . Default to 0.
* @ return int Attachment ID
*/
protected function set_uploaded_image_as_attachment ( $upload , $id = 0 ) {
2014-06-30 20:31:25 +00:00
$info = wp_check_filetype ( $upload [ 'file' ] );
$title = '' ;
$content = '' ;
if ( $image_meta = @ wp_read_image_metadata ( $upload [ 'file' ] ) ) {
if ( trim ( $image_meta [ 'title' ] ) && ! is_numeric ( sanitize_title ( $image_meta [ 'title' ] ) ) ) {
$title = $image_meta [ 'title' ];
}
if ( trim ( $image_meta [ 'caption' ] ) ) {
$content = $image_meta [ 'caption' ];
}
}
2014-06-24 18:19:34 +00:00
$attachment = array (
'post_mime_type' => $info [ 'type' ],
2014-06-30 20:31:25 +00:00
'guid' => $upload [ 'url' ],
'post_parent' => $id ,
'post_title' => $title ,
'post_content' => $content
2014-06-24 18:19:34 +00:00
);
2014-06-30 20:31:25 +00:00
2014-06-25 16:27:48 +00:00
$attachment_id = wp_insert_attachment ( $attachment , $upload [ 'file' ], $id );
2014-06-30 20:31:25 +00:00
if ( ! is_wp_error ( $attachment_id ) ) {
wp_update_attachment_metadata ( $attachment_id , wp_generate_attachment_metadata ( $attachment_id , $upload [ 'file' ] ) );
}
2014-06-24 18:19:34 +00:00
return $attachment_id ;
}
2013-11-14 17:40:35 +00:00
/**
* Get the attributes for a product or product variation
*
* @ since 2.1
* @ param WC_Product | WC_Product_Variation $product
* @ return array
*/
private function get_attributes ( $product ) {
$attributes = array ();
if ( $product -> is_type ( 'variation' ) ) {
// variation attributes
foreach ( $product -> get_variation_attributes () as $attribute_name => $attribute ) {
// taxonomy-based attributes are prefixed with `pa_`, otherwise simply `attribute_`
$attributes [] = array (
2015-01-13 15:24:28 +00:00
'name' => wc_attribute_label ( str_replace ( 'attribute_' , '' , $attribute_name ) ),
'slug' => str_replace ( 'attribute_' , '' , str_replace ( 'pa_' , '' , $attribute_name ) ),
2013-11-14 17:40:35 +00:00
'option' => $attribute ,
);
}
} else {
foreach ( $product -> get_attributes () as $attribute ) {
// taxonomy-based attributes are comma-separated, others are pipe (|) separated
2014-06-26 19:55:24 +00:00
if ( $attribute [ 'is_taxonomy' ] ) {
2013-11-14 17:40:35 +00:00
$options = explode ( ',' , $product -> get_attribute ( $attribute [ 'name' ] ) );
2014-06-26 19:55:24 +00:00
} else {
2013-11-14 17:40:35 +00:00
$options = explode ( '|' , $product -> get_attribute ( $attribute [ 'name' ] ) );
2014-06-26 19:55:24 +00:00
}
2013-11-14 17:40:35 +00:00
$attributes [] = array (
2015-01-13 15:24:28 +00:00
'name' => wc_attribute_label ( $attribute [ 'name' ] ),
'slug' => str_replace ( 'pa_' , '' , $attribute [ 'name' ] ),
2015-04-03 18:01:41 +00:00
'position' => ( int ) $attribute [ 'position' ],
2013-11-14 17:40:35 +00:00
'visible' => ( bool ) $attribute [ 'is_visible' ],
'variation' => ( bool ) $attribute [ 'is_variation' ],
'options' => array_map ( 'trim' , $options ),
);
}
}
return $attributes ;
}
/**
* Get the downloads for a product or product variation
*
* @ since 2.1
* @ param WC_Product | WC_Product_Variation $product
* @ return array
*/
private function get_downloads ( $product ) {
$downloads = array ();
if ( $product -> is_downloadable () ) {
foreach ( $product -> get_files () as $file_id => $file ) {
$downloads [] = array (
'id' => $file_id , // do not cast as int as this is a hash
'name' => $file [ 'name' ],
'file' => $file [ 'file' ],
);
}
}
return $downloads ;
}
2015-04-27 14:47:17 +00:00
/**
* Get a listing of product attributes
*
2015-12-04 23:18:06 +00:00
* @ since 2.5 . 0
2015-04-27 14:47:17 +00:00
* @ param string | null $fields fields to limit response to
* @ return array
*/
public function get_product_attributes ( $fields = null ) {
try {
2015-12-04 18:02:31 +00:00
// Permissions check.
2015-04-27 14:47:17 +00:00
if ( ! current_user_can ( 'manage_product_terms' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_read_product_attributes' , __ ( 'You do not have permission to read product attributes' , 'woocommerce' ), 401 );
}
$product_attributes = array ();
$attribute_taxonomies = wc_get_attribute_taxonomies ();
foreach ( $attribute_taxonomies as $attribute ) {
$product_attributes [] = array (
'id' => intval ( $attribute -> attribute_id ),
'name' => $attribute -> attribute_label ,
'slug' => wc_attribute_taxonomy_name ( $attribute -> attribute_name ),
'type' => $attribute -> attribute_type ,
'order_by' => $attribute -> attribute_orderby ,
'has_archives' => ( bool ) $attribute -> attribute_public
);
}
return array ( 'product_attributes' => apply_filters ( 'woocommerce_api_product_attributes_response' , $product_attributes , $attribute_taxonomies , $fields , $this ) );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
2015-04-27 14:58:58 +00:00
/**
* Get the product attribute for the given ID
*
2015-12-04 23:18:06 +00:00
* @ since 2.5 . 0
2015-04-27 14:58:58 +00:00
* @ param string $id product attribute term ID
* @ param string | null $fields fields to limit response to
* @ return array
*/
public function get_product_attribute ( $id , $fields = null ) {
global $wpdb ;
try {
$id = absint ( $id );
// Validate ID
if ( empty ( $id ) ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_product_attribute_id' , __ ( 'Invalid product attribute ID' , 'woocommerce' ), 400 );
}
// Permissions check
if ( ! current_user_can ( 'manage_product_terms' ) ) {
2015-12-04 18:14:57 +00:00
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_read_product_attributes' , __ ( 'You do not have permission to read product attributes' , 'woocommerce' ), 401 );
2015-04-27 14:58:58 +00:00
}
$attribute = $wpdb -> get_row ( $wpdb -> prepare ( "
SELECT *
FROM { $wpdb -> prefix } woocommerce_attribute_taxonomies
WHERE attribute_id = % d
" , $id ) );
if ( is_wp_error ( $attribute ) || is_null ( $attribute ) ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_product_attribute_id' , __ ( 'A product attribute with the provided ID could not be found' , 'woocommerce' ), 404 );
}
$product_attribute = array (
'id' => intval ( $attribute -> attribute_id ),
'name' => $attribute -> attribute_label ,
'slug' => wc_attribute_taxonomy_name ( $attribute -> attribute_name ),
'type' => $attribute -> attribute_type ,
'order_by' => $attribute -> attribute_orderby ,
'has_archives' => ( bool ) $attribute -> attribute_public
);
return array ( 'product_attribute' => apply_filters ( 'woocommerce_api_product_attribute_response' , $product_attribute , $id , $fields , $attribute , $this ) );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
2015-04-27 19:14:31 +00:00
/**
* Validate attribute data .
*
2015-12-04 23:18:06 +00:00
* @ since 2.5 . 0
2015-04-27 19:14:31 +00:00
* @ param string $name
* @ param string $slug
* @ param string $type
* @ param string $order_by
* @ param bool $new_data
* @ return bool
2015-11-17 12:51:42 +00:00
* @ throws WC_API_Exception
2015-04-27 19:14:31 +00:00
*/
protected function validate_attribute_data ( $name , $slug , $type , $order_by , $new_data = true ) {
if ( empty ( $name ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_product_attribute_name' , sprintf ( __ ( 'Missing parameter %s' , 'woocommerce' ), 'name' ), 400 );
}
if ( strlen ( $slug ) >= 28 ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_product_attribute_slug_too_long' , sprintf ( __ ( 'Slug "%s" is too long (28 characters max). Shorten it, please.' , 'woocommerce' ), $slug ), 400 );
} else if ( wc_check_if_attribute_name_is_reserved ( $slug ) ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_product_attribute_slug_reserved_name' , sprintf ( __ ( 'Slug "%s" is not allowed because it is a reserved term. Change it, please.' , 'woocommerce' ), $slug ), 400 );
} else if ( $new_data && taxonomy_exists ( wc_attribute_taxonomy_name ( $slug ) ) ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_product_attribute_slug_already_exists' , sprintf ( __ ( 'Slug "%s" is already in use. Change it, please.' , 'woocommerce' ), $slug ), 400 );
}
// Validate the attribute type
if ( ! in_array ( wc_clean ( $type ), array_keys ( wc_get_attribute_types () ) ) ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_product_attribute_type' , sprintf ( __ ( 'Invalid product attribute type - the product attribute type must be any of these: %s' , 'woocommerce' ), implode ( ', ' , array_keys ( wc_get_attribute_types () ) ) ), 400 );
}
// Validate the attribute order by
if ( ! in_array ( wc_clean ( $order_by ), array ( 'menu_order' , 'name' , 'name_num' , 'id' ) ) ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_product_attribute_order_by' , sprintf ( __ ( 'Invalid product attribute order_by type - the product attribute order_by type must be any of these: %s' , 'woocommerce' ), implode ( ', ' , array ( 'menu_order' , 'name' , 'name_num' , 'id' ) ) ), 400 );
}
return true ;
}
2015-04-27 16:06:42 +00:00
/**
2015-12-04 18:38:17 +00:00
* Create a new product attribute .
2015-04-27 16:06:42 +00:00
*
2015-12-04 23:18:06 +00:00
* @ since 2.5 . 0
2015-12-04 18:38:17 +00:00
* @ param array $data Posted data .
2015-04-27 16:06:42 +00:00
* @ return array
*/
public function create_product_attribute ( $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 create %1$s' , 'woocommerce' ), 'product_attribute' ), 400 );
}
$data = $data [ 'product_attribute' ];
2015-12-04 18:38:17 +00:00
// Check permissions.
2015-04-27 16:06:42 +00:00
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 attributes' , 'woocommerce' ), 401 );
}
$data = apply_filters ( 'woocommerce_api_create_product_attribute_data' , $data , $this );
if ( ! isset ( $data [ 'name' ] ) ) {
2015-04-27 19:14:31 +00:00
$data [ 'name' ] = '' ;
2015-04-27 16:06:42 +00:00
}
2015-12-04 18:38:17 +00:00
// Set the attribute slug.
2015-04-27 16:06:42 +00:00
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' ] ) ) );
}
2015-12-04 18:38:17 +00:00
// Set attribute type when not sent.
2015-04-27 16:06:42 +00:00
if ( ! isset ( $data [ 'type' ] ) ) {
$data [ 'type' ] = 'select' ;
}
2015-12-04 18:38:17 +00:00
// Set order by when not sent.
2015-04-27 16:06:42 +00:00
if ( ! isset ( $data [ 'order_by' ] ) ) {
$data [ 'order_by' ] = 'menu_order' ;
}
2015-12-04 18:38:17 +00:00
// Validate the attribute data.
2015-04-27 19:14:31 +00:00
$this -> validate_attribute_data ( $data [ 'name' ], $data [ 'slug' ], $data [ 'type' ], $data [ 'order_by' ], true );
2015-04-27 16:06:42 +00:00
$insert = $wpdb -> insert (
$wpdb -> prefix . 'woocommerce_attribute_taxonomies' ,
array (
2015-04-27 19:14:31 +00:00
'attribute_label' => $data [ 'name' ],
2015-04-27 16:06:42 +00:00
'attribute_name' => $data [ 'slug' ],
2015-04-27 19:14:31 +00:00
'attribute_type' => $data [ 'type' ],
'attribute_orderby' => $data [ 'order_by' ],
2015-04-27 16:06:42 +00:00
'attribute_public' => isset ( $data [ 'has_archives' ] ) && true === $data [ 'has_archives' ] ? 1 : 0
2015-04-27 19:14:31 +00:00
),
array ( '%s' , '%s' , '%s' , '%s' , '%d' )
2015-04-27 16:06:42 +00:00
);
2015-12-04 18:38:17 +00:00
// Checks for an error in the product creation.
2015-04-27 16:06:42 +00:00
if ( is_wp_error ( $insert ) ) {
throw new WC_API_Exception ( 'woocommerce_api_cannot_create_product_attribute' , $insert -> get_error_message (), 400 );
}
$id = $wpdb -> insert_id ;
do_action ( 'woocommerce_api_create_product_attribute' , $id , $data );
2015-12-04 18:38:17 +00:00
// Clear transients.
flush_rewrite_rules ();
2015-04-27 16:06:42 +00:00
delete_transient ( 'wc_attribute_taxonomies' );
$this -> server -> send_status ( 201 );
return $this -> get_product_attribute ( $id );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
2015-04-27 19:14:31 +00:00
/**
2015-12-04 18:38:17 +00:00
* Edit a product attribute .
2015-04-27 19:14:31 +00:00
*
2015-12-04 23:18:06 +00:00
* @ since 2.5 . 0
2015-12-04 18:38:17 +00:00
* @ param int $id the attribute ID .
2015-04-27 19:14:31 +00:00
* @ param array $data
* @ return array
*/
2015-04-27 16:06:42 +00:00
public function edit_product_attribute ( $id , $data ) {
2015-04-27 19:14:31 +00:00
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 );
}
2015-04-27 16:06:42 +00:00
2015-04-27 19:14:31 +00:00
$id = absint ( $id );
$data = $data [ 'product_attribute' ];
2015-12-04 18:53:17 +00:00
// Check permissions.
2015-04-27 19:14:31 +00:00
if ( ! current_user_can ( 'manage_product_terms' ) ) {
2015-04-27 19:33:47 +00:00
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_edit_product_attribute' , __ ( 'You do not have permission to edit product attributes' , 'woocommerce' ), 401 );
2015-04-27 19:14:31 +00:00
}
$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' ];
}
2015-12-04 18:53:17 +00:00
// Validate the attribute data.
2015-04-27 19:14:31 +00:00
$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' )
);
2015-12-04 18:53:17 +00:00
// Checks for an error in the product creation.
2015-04-27 19:14:31 +00:00
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 );
2015-12-04 18:53:17 +00:00
// Clear transients.
2015-12-04 18:38:17 +00:00
flush_rewrite_rules ();
2015-04-27 19:14:31 +00:00
delete_transient ( 'wc_attribute_taxonomies' );
return $this -> get_product_attribute ( $id );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
2015-04-27 16:06:42 +00:00
}
2015-04-27 19:33:47 +00:00
/**
2015-12-04 19:01:49 +00:00
* Delete a product attribute .
2015-04-27 19:33:47 +00:00
*
2015-12-04 23:18:06 +00:00
* @ since 2.5 . 0
2015-12-04 19:01:49 +00:00
* @ param int $id the product attribute ID .
2015-04-27 19:33:47 +00:00
* @ return array
*/
public function delete_product_attribute ( $id ) {
global $wpdb ;
try {
2015-12-04 19:01:49 +00:00
// Check permissions.
2015-04-27 19:33:47 +00:00
if ( ! current_user_can ( 'manage_product_terms' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_delete_product_attribute' , __ ( 'You do not have permission to delete product attributes' , 'woocommerce' ), 401 );
}
$id = absint ( $id );
$attribute_name = $wpdb -> get_var ( $wpdb -> prepare ( "
SELECT attribute_name
FROM { $wpdb -> prefix } woocommerce_attribute_taxonomies
WHERE attribute_id = % d
" , $id ) );
if ( is_null ( $attribute_name ) ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_product_attribute_id' , __ ( 'A product attribute with the provided ID could not be found' , 'woocommerce' ), 404 );
}
$deleted = $wpdb -> delete (
$wpdb -> prefix . 'woocommerce_attribute_taxonomies' ,
array ( 'attribute_id' => $id ),
array ( '%d' )
);
if ( false === $deleted ) {
throw new WC_API_Exception ( 'woocommerce_api_cannot_delete_product_attribute' , __ ( 'Could not delete the attribute' , 'woocommerce' ), 401 );
}
$taxonomy = wc_attribute_taxonomy_name ( $attribute_name );
if ( taxonomy_exists ( $taxonomy ) ) {
$terms = get_terms ( $taxonomy , 'orderby=name&hide_empty=0' );
foreach ( $terms as $term ) {
wp_delete_term ( $term -> term_id , $taxonomy );
}
}
do_action ( 'woocommerce_attribute_deleted' , $id , $attribute_name , $taxonomy );
do_action ( 'woocommerce_api_delete_product_attribute' , $id , $this );
2015-12-04 18:38:17 +00:00
// Clear transients.
flush_rewrite_rules ();
2015-04-27 19:33:47 +00:00
delete_transient ( 'wc_attribute_taxonomies' );
return array ( 'message' => sprintf ( __ ( 'Deleted %s' , 'woocommerce' ), 'product_attribute' ) );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
2015-12-04 18:02:31 +00:00
}
}
/**
* Get a listing of product attribute terms .
*
2015-12-04 23:18:06 +00:00
* @ since 2.5 . 0
2015-12-04 18:02:31 +00:00
* @ param int $attribute_id Attribute ID .
* @ param string | null $fields Fields to limit response to .
* @ return array
*/
public function get_product_attribute_terms ( $attribute_id , $fields = null ) {
try {
// Permissions check.
if ( ! current_user_can ( 'manage_product_terms' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_read_product_attribute_terms' , __ ( 'You do not have permission to read product attribute terms' , 'woocommerce' ), 401 );
}
$taxonomy = wc_attribute_taxonomy_name_by_id ( $attribute_id );
if ( ! $taxonomy ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_product_attribute_id' , __ ( 'A product attribute with the provided ID could not be found' , 'woocommerce' ), 404 );
}
$args = array ( 'hide_empty' => false );
$orderby = wc_attribute_orderby ( $taxonomy );
switch ( $orderby ) {
case 'name' :
$args [ 'orderby' ] = 'name' ;
$args [ 'menu_order' ] = false ;
break ;
case 'id' :
$args [ 'orderby' ] = 'id' ;
$args [ 'order' ] = 'ASC' ;
$args [ 'menu_order' ] = false ;
break ;
case 'menu_order' :
$args [ 'menu_order' ] = 'ASC' ;
break ;
}
$terms = get_terms ( $taxonomy , $args );
$attribute_terms = array ();
foreach ( $terms as $term ) {
$attribute_terms [] = array (
'id' => $term -> term_id ,
'slug' => $term -> slug ,
'name' => $term -> name ,
'count' => $term -> count ,
);
}
return array ( 'product_attribute_terms' => apply_filters ( 'woocommerce_api_product_attribute_terms_response' , $attribute_terms , $terms , $fields , $this ) );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
2015-12-04 18:14:57 +00:00
}
}
/**
2015-12-04 18:53:17 +00:00
* Get the product attribute term for the given ID .
2015-12-04 18:14:57 +00:00
*
2015-12-04 23:18:06 +00:00
* @ since 2.5 . 0
2015-12-04 18:38:17 +00:00
* @ param int $attribute_id Attribute ID .
* @ param string $id Product attribute term ID .
* @ param string | null $fields Fields to limit response to .
2015-12-04 18:14:57 +00:00
* @ return array
*/
public function get_product_attribute_term ( $attribute_id , $id , $fields = null ) {
global $wpdb ;
try {
$id = absint ( $id );
// Validate ID
if ( empty ( $id ) ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_product_attribute_term_id' , __ ( 'Invalid product attribute ID' , 'woocommerce' ), 400 );
}
// Permissions check
if ( ! current_user_can ( 'manage_product_terms' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_read_product_attribute_terms' , __ ( 'You do not have permission to read product attribute terms' , 'woocommerce' ), 401 );
}
$taxonomy = wc_attribute_taxonomy_name_by_id ( $attribute_id );
if ( ! $taxonomy ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_product_attribute_id' , __ ( 'A product attribute with the provided ID could not be found' , 'woocommerce' ), 404 );
}
$term = get_term ( $id , $taxonomy );
if ( is_wp_error ( $term ) || is_null ( $term ) ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_product_attribute_term_id' , __ ( 'A product attribute term with the provided ID could not be found' , 'woocommerce' ), 404 );
}
$attribute_term = array (
'id' => $term -> term_id ,
'name' => $term -> name ,
2015-12-18 23:39:30 +00:00
'slug' => $term -> slug ,
2015-12-04 18:14:57 +00:00
'count' => $term -> count ,
);
return array ( 'product_attribute_term' => apply_filters ( 'woocommerce_api_product_attribute_response' , $attribute_term , $id , $fields , $term , $this ) );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
2015-12-04 18:38:17 +00:00
}
}
/**
2015-12-04 18:53:17 +00:00
* Create a new product attribute term .
2015-12-04 18:38:17 +00:00
*
2015-12-04 23:18:06 +00:00
* @ since 2.5 . 0
2015-12-04 18:38:17 +00:00
* @ param int $attribute_id Attribute ID .
* @ param array $data Posted data .
* @ return array
*/
public function create_product_attribute_term ( $attribute_id , $data ) {
global $wpdb ;
try {
if ( ! isset ( $data [ 'product_attribute_term' ] ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_product_attribute_term_data' , sprintf ( __ ( 'No %1$s data specified to create %1$s' , 'woocommerce' ), 'product_attribute_term' ), 400 );
}
$data = $data [ 'product_attribute_term' ];
// 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 attributes' , 'woocommerce' ), 401 );
}
$taxonomy = wc_attribute_taxonomy_name_by_id ( $attribute_id );
if ( ! $taxonomy ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_product_attribute_id' , __ ( 'A product attribute with the provided ID could not be found' , 'woocommerce' ), 404 );
}
$data = apply_filters ( 'woocommerce_api_create_product_attribute_term_data' , $data , $this );
// Check if attribute term name is specified.
if ( ! isset ( $data [ 'name' ] ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_product_attribute_term_name' , sprintf ( __ ( 'Missing parameter %s' , 'woocommerce' ), 'name' ), 400 );
}
$args = array ();
// Set the attribute term slug.
if ( isset ( $data [ 'slug' ] ) ) {
$args [ 'slug' ] = sanitize_title ( wp_unslash ( $data [ 'slug' ] ) );
}
$term = wp_insert_term ( $data [ 'name' ], $taxonomy , $args );
2015-12-04 18:53:17 +00:00
// Checks for an error in the term creation.
2015-12-04 18:38:17 +00:00
if ( is_wp_error ( $term ) ) {
throw new WC_API_Exception ( 'woocommerce_api_cannot_create_product_attribute' , $term -> get_error_message (), 400 );
}
$id = $term [ 'term_id' ];
do_action ( 'woocommerce_api_create_product_attribute_term' , $id , $data );
$this -> server -> send_status ( 201 );
2015-12-04 18:53:17 +00:00
return $this -> get_product_attribute_term ( $attribute_id , $id );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
/**
* Edit a product attribute term .
*
2015-12-04 23:18:06 +00:00
* @ since 2.5 . 0
2015-12-04 18:53:17 +00:00
* @ param int $attribute_id Attribute ID .
* @ param int $id the attribute ID .
* @ param array $data
* @ return array
*/
public function edit_product_attribute_term ( $attribute_id , $id , $data ) {
global $wpdb ;
try {
if ( ! isset ( $data [ 'product_attribute_term' ] ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_product_attribute_term_data' , sprintf ( __ ( 'No %1$s data specified to edit %1$s' , 'woocommerce' ), 'product_attribute_term' ), 400 );
}
$id = absint ( $id );
$data = $data [ 'product_attribute_term' ];
// 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 );
}
$taxonomy = wc_attribute_taxonomy_name_by_id ( $attribute_id );
if ( ! $taxonomy ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_product_attribute_id' , __ ( 'A product attribute with the provided ID could not be found' , 'woocommerce' ), 404 );
}
$data = apply_filters ( 'woocommerce_api_edit_product_attribute_term_data' , $data , $this );
$args = array ();
// Update name.
if ( isset ( $data [ 'name' ] ) ) {
$args [ 'name' ] = wc_clean ( wp_unslash ( $data [ 'name' ] ) );
}
// Update slug.
if ( isset ( $data [ 'slug' ] ) ) {
$args [ 'slug' ] = sanitize_title ( wp_unslash ( $data [ 'slug' ] ) );
}
$term = wp_update_term ( $id , $taxonomy , $args );
if ( is_wp_error ( $term ) ) {
throw new WC_API_Exception ( 'woocommerce_api_cannot_edit_product_attribute_term' , $term -> get_error_message (), 400 );
}
do_action ( 'woocommerce_api_edit_product_attribute_term' , $id , $data );
2015-12-04 18:38:17 +00:00
return $this -> get_product_attribute_term ( $attribute_id , $id );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
2015-12-04 19:01:49 +00:00
}
}
/**
* Delete a product attribute term .
*
2015-12-04 23:18:06 +00:00
* @ since 2.5 . 0
2015-12-04 19:01:49 +00:00
* @ param int $attribute_id Attribute ID .
* @ param int $id the product attribute ID .
* @ return array
*/
public function delete_product_attribute_term ( $attribute_id , $id ) {
global $wpdb ;
try {
// Check permissions.
if ( ! current_user_can ( 'manage_product_terms' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_delete_product_attribute_term' , __ ( 'You do not have permission to delete product attribute terms' , 'woocommerce' ), 401 );
}
$taxonomy = wc_attribute_taxonomy_name_by_id ( $attribute_id );
if ( ! $taxonomy ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_product_attribute_id' , __ ( 'A product attribute with the provided ID could not be found' , 'woocommerce' ), 404 );
}
$id = absint ( $id );
$term = wp_delete_term ( $id , $taxonomy );
if ( ! $term ) {
throw new WC_API_Exception ( 'woocommerce_api_cannot_delete_product_attribute_term' , sprintf ( __ ( 'This %s cannot be deleted' , 'woocommerce' ), 'product_attribute_term' ), 500 );
} else if ( is_wp_error ( $term ) ) {
throw new WC_API_Exception ( 'woocommerce_api_cannot_delete_product_attribute_term' , $term -> get_error_message (), 400 );
}
do_action ( 'woocommerce_api_delete_product_attribute_term' , $id , $this );
return array ( 'message' => sprintf ( __ ( 'Deleted %s' , 'woocommerce' ), 'product_attribute' ) );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
2015-04-27 19:33:47 +00:00
}
}
2015-03-16 17:54:38 +00:00
/**
* Clear product
*/
protected function clear_product ( $product_id ) {
2015-07-10 09:48:29 +00:00
if ( ! is_numeric ( $product_id ) || 0 >= $product_id ) {
2015-07-10 13:49:18 +00:00
return ;
}
2015-03-16 17:54:38 +00:00
// Delete product attachments
$attachments = get_children ( array (
'post_parent' => $product_id ,
'post_status' => 'any' ,
'post_type' => 'attachment' ,
) );
foreach ( ( array ) $attachments as $attachment ) {
wp_delete_attachment ( $attachment -> ID , true );
}
// Delete product
wp_delete_post ( $product_id , true );
}
2015-04-24 21:43:33 +00:00
2015-04-09 20:48:51 +00:00
/**
* Bulk update or insert products
* Accepts an array with products in the formats supported by
* WC_API_Products -> create_product () and WC_API_Products -> edit_product ()
*
2015-04-24 21:43:33 +00:00
* @ since 2.4 . 0
2015-05-06 21:41:48 +00:00
* @ param array $data
2015-04-24 21:43:33 +00:00
* @ return array
2015-04-09 20:48:51 +00:00
*/
2015-04-24 21:43:33 +00:00
public function bulk ( $data ) {
try {
if ( ! isset ( $data [ 'products' ] ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_products_data' , sprintf ( __ ( 'No %1$s data specified to create/edit %1$s' , 'woocommerce' ), 'products' ), 400 );
}
$data = $data [ 'products' ];
$limit = apply_filters ( 'woocommerce_api_bulk_limit' , 100 , 'products' );
// Limit bulk operation
2015-04-28 18:10:29 +00:00
if ( count ( $data ) > $limit ) {
2015-04-24 21:43:33 +00:00
throw new WC_API_Exception ( 'woocommerce_api_products_request_entity_too_large' , sprintf ( __ ( 'Unable to accept more than %s items for this request' , 'woocommerce' ), $limit ), 413 );
}
$products = array ();
foreach ( $data as $_product ) {
$product_id = 0 ;
$product_sku = '' ;
// Try to get the product ID
if ( isset ( $_product [ 'id' ] ) ) {
$product_id = intval ( $_product [ 'id' ] );
}
if ( ! $product_id && isset ( $_product [ 'sku' ] ) ) {
$product_sku = wc_clean ( $_product [ 'sku' ] );
$product_id = wc_get_product_id_by_sku ( $product_sku );
}
// Product exists / edit product
if ( $product_id ) {
$edit = $this -> edit_product ( $product_id , array ( 'product' => $_product ) );
if ( is_wp_error ( $edit ) ) {
$products [] = array (
'id' => $product_id ,
'sku' => $product_sku ,
'error' => array ( 'code' => $edit -> get_error_code (), 'message' => $edit -> get_error_message () )
);
} else {
$products [] = $edit [ 'product' ];
}
}
// Product don't exists / create product
else {
$new = $this -> create_product ( array ( 'product' => $_product ) );
if ( is_wp_error ( $new ) ) {
$products [] = array (
'id' => $product_id ,
'sku' => $product_sku ,
'error' => array ( 'code' => $new -> get_error_code (), 'message' => $new -> get_error_message () )
);
} else {
$products [] = $new [ 'product' ];
}
}
}
return array ( 'products' => apply_filters ( 'woocommerce_api_products_bulk_response' , $products , $this ) );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
2015-04-09 20:48:51 +00:00
}
2015-09-09 13:38:11 +00:00
/**
* Get a listing of product shipping classes .
*
* @ since 2.5 . 0
* @ param string | null $fields Fields to limit response to
* @ return array | WP_Error List of product shipping classes if succeed ,
* otherwise WP_Error will be returned
*/
public function get_product_shipping_classes ( $fields = null ) {
try {
// Permissions check
if ( ! current_user_can ( 'manage_product_terms' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_read_product_shipping_classes' , __ ( 'You do not have permission to read product shipping classes' , 'woocommerce' ), 401 );
}
$product_shipping_classes = array ();
$terms = get_terms ( 'product_shipping_class' , array ( 'hide_empty' => false , 'fields' => 'ids' ) );
foreach ( $terms as $term_id ) {
$product_shipping_classes [] = current ( $this -> get_product_shipping_class ( $term_id , $fields ) );
}
return array ( 'product_shipping_classes' => apply_filters ( 'woocommerce_api_product_shipping_classes_response' , $product_shipping_classes , $terms , $fields , $this ) );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
/**
* Get the product shipping class for the given ID .
*
* @ since 2.5 . 0
* @ param string $id Product shipping class term ID
* @ param string | null $fields Fields to limit response to
* @ return array | WP_Error Product shipping class if succeed , otherwise
* WP_Error will be returned
*/
public function get_product_shipping_class ( $id , $fields = null ) {
try {
$id = absint ( $id );
if ( ! $id ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_product_shipping_class_id' , __ ( 'Invalid product shipping class ID' , 'woocommerce' ), 400 );
}
// Permissions check
if ( ! current_user_can ( 'manage_product_terms' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_read_product_shipping_classes' , __ ( 'You do not have permission to read product shipping classes' , 'woocommerce' ), 401 );
}
$term = get_term ( $id , 'product_shipping_class' );
if ( is_wp_error ( $term ) || is_null ( $term ) ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_product_shipping_class_id' , __ ( 'A product shipping class with the provided ID could not be found' , 'woocommerce' ), 404 );
}
$term_id = intval ( $term -> term_id );
$product_shipping_class = array (
'id' => $term_id ,
'name' => $term -> name ,
'slug' => $term -> slug ,
'parent' => $term -> parent ,
'description' => $term -> description ,
'count' => intval ( $term -> count )
);
return array ( 'product_shipping_class' => apply_filters ( 'woocommerce_api_product_shipping_class_response' , $product_shipping_class , $id , $fields , $term , $this ) );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
/**
* Create a new product shipping class .
*
* @ since 2.5 . 0
* @ param array $data Posted data
* @ return array | WP_Error Product shipping class if succeed , otherwise
* WP_Error will be returned
*/
public function create_product_shipping_class ( $data ) {
global $wpdb ;
try {
if ( ! isset ( $data [ 'product_shipping_class' ] ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_product_shipping_class_data' , sprintf ( __ ( 'No %1$s data specified to create %1$s' , 'woocommerce' ), 'product_shipping_class' ), 400 );
}
// Check permissions
if ( ! current_user_can ( 'manage_product_terms' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_create_product_shipping_class' , __ ( 'You do not have permission to create product shipping classes' , 'woocommerce' ), 401 );
}
$defaults = array (
'name' => '' ,
'slug' => '' ,
'description' => '' ,
'parent' => 0 ,
);
$data = wp_parse_args ( $data [ 'product_shipping_class' ], $defaults );
$data = apply_filters ( 'woocommerce_api_create_product_shipping_class_data' , $data , $this );
// Check parent.
$data [ 'parent' ] = absint ( $data [ 'parent' ] );
if ( $data [ 'parent' ] ) {
2016-02-10 16:27:00 +00:00
$parent = get_term_by ( 'id' , $data [ 'parent' ], 'product_shipping_class' );
2015-09-09 13:38:11 +00:00
if ( ! $parent ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_product_shipping_class_parent' , __ ( 'Product shipping class parent is invalid' , 'woocommerce' ), 400 );
}
}
2015-12-16 18:46:31 +00:00
$insert = wp_insert_term ( $data [ 'name' ], 'product_shipping_class' , $data );
2015-09-09 13:38:11 +00:00
if ( is_wp_error ( $insert ) ) {
throw new WC_API_Exception ( 'woocommerce_api_cannot_create_product_shipping_class' , $insert -> get_error_message (), 400 );
}
2016-02-10 16:27:00 +00:00
$id = $insert [ 'term_id' ];
do_action ( 'woocommerce_api_create_product_shipping_class' , $id , $data );
2015-09-09 13:38:11 +00:00
$this -> server -> send_status ( 201 );
2016-02-10 16:27:00 +00:00
return $this -> get_product_shipping_class ( $id );
2015-09-09 13:38:11 +00:00
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
/**
* Edit a product shipping class .
*
* @ since 2.5 . 0
* @ param int $id Product shipping class term ID
* @ param array $data Posted data
* @ return array | WP_Error Product shipping class if succeed , otherwise
* WP_Error will be returned
*/
public function edit_product_shipping_class ( $id , $data ) {
global $wpdb ;
try {
if ( ! isset ( $data [ 'product_shipping_class' ] ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_product_shipping_class' , sprintf ( __ ( 'No %1$s data specified to edit %1$s' , 'woocommerce' ), 'product_shipping_class' ), 400 );
}
$id = absint ( $id );
$data = $data [ 'product_shipping_class' ];
// Check permissions
if ( ! current_user_can ( 'manage_product_terms' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_edit_product_shipping_class' , __ ( 'You do not have permission to edit product shipping classes' , 'woocommerce' ), 401 );
}
$data = apply_filters ( 'woocommerce_api_edit_product_shipping_class_data' , $data , $this );
$shipping_class = $this -> get_product_shipping_class ( $id );
if ( is_wp_error ( $shipping_class ) ) {
return $shipping_class ;
}
$update = wp_update_term ( $id , 'product_shipping_class' , $data );
if ( is_wp_error ( $update ) ) {
throw new WC_API_Exception ( 'woocommerce_api_cannot_edit_product_shipping_class' , __ ( 'Could not edit the shipping class' , 'woocommerce' ), 400 );
}
do_action ( 'woocommerce_api_edit_product_shipping_class' , $id , $data );
return $this -> get_product_shipping_class ( $id );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
/**
* Delete a product shipping class .
*
* @ since 2.5 . 0
* @ param int $id Product shipping class term ID
* @ return array | WP_Error Success message if succeed , otherwise WP_Error
* will be returned
*/
public function delete_product_shipping_class ( $id ) {
global $wpdb ;
try {
// Check permissions
if ( ! current_user_can ( 'manage_product_terms' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_delete_product_shipping_class' , __ ( 'You do not have permission to delete product shipping classes' , 'woocommerce' ), 401 );
}
$id = absint ( $id );
$deleted = wp_delete_term ( $id , 'product_shipping_class' );
if ( ! $deleted || is_wp_error ( $deleted ) ) {
throw new WC_API_Exception ( 'woocommerce_api_cannot_delete_product_shipping_class' , __ ( 'Could not delete the shipping class' , 'woocommerce' ), 401 );
}
do_action ( 'woocommerce_api_delete_product_shipping_class' , $id , $this );
return array ( 'message' => sprintf ( __ ( 'Deleted %s' , 'woocommerce' ), 'product_shipping_class' ) );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
2013-11-04 06:36:31 +00:00
}