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
);
2014-07-28 23:01:50 +00:00
# GET /products/categories
$routes [ $this -> base . '/categories' ] = array (
array ( array ( $this , 'get_product_categories' ), WC_API_Server :: READABLE ),
);
# GET /products/categories/<id>
$routes [ $this -> base . '/categories/(?P<id>\d+)' ] = array (
array ( array ( $this , 'get_product_category' ), WC_API_Server :: READABLE ),
);
2013-11-04 06:36:31 +00:00
return $routes ;
}
/**
* 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
if ( $product -> is_type ( 'variation' ) ) {
$product_data [ 'parent' ] = $this -> get_product_data ( $product -> parent );
}
2013-11-04 06:36:31 +00:00
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 () ) {
2013-11-04 06:36:31 +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
2014-06-26 19:55:24 +00:00
if ( ! current_user_can ( 'read_private_products' ) ) {
2013-11-23 22:11:16 +00:00
return new WP_Error ( 'woocommerce_api_user_cannot_read_products_count' , __ ( 'You do not have permission to read the products count' , 'woocommerce' ), array ( 'status' => 401 ) );
2014-06-26 19:55:24 +00:00
}
2013-11-04 06:36:31 +00:00
2013-11-11 00:29:36 +00:00
$query = $this -> query_products ( $filter );
2013-11-04 06:36:31 +00:00
2013-11-23 21:36:56 +00:00
return array ( 'count' => ( int ) $query -> found_posts );
2013-11-04 06:36:31 +00:00
}
2014-06-24 17:42:10 +00:00
/**
* Create a new product
*
* @ since 2.2
* @ param array $data posted data
* @ return array
*/
public function create_product ( $data ) {
2014-08-30 19:50:40 +00:00
$data = isset ( $data [ 'product' ] ) ? $data [ 'product' ] : array ();
// Check permissions
2014-06-24 17:42:10 +00:00
if ( ! current_user_can ( 'publish_products' ) ) {
return new WP_Error ( 'woocommerce_api_user_cannot_create_product' , __ ( 'You do not have permission to create products' , 'woocommerce' ), array ( 'status' => 401 ) );
}
2014-08-30 19:50:40 +00:00
$data = apply_filters ( 'woocommerce_api_create_product_data' , $data , $this );
2014-06-24 17:42:10 +00:00
// Check if product title is specified
if ( ! isset ( $data [ 'title' ] ) ) {
2014-08-15 19:08:43 +00:00
return new WP_Error ( 'woocommerce_api_missing_product_title' , sprintf ( __ ( 'Missing parameter %s' , 'woocommerce' ), 'title' ), array ( 'status' => 400 ) );
2014-06-24 17:42:10 +00:00
}
// Check product type
if ( ! isset ( $data [ 'type' ] ) ) {
$data [ 'type' ] = 'simple' ;
}
// Validate the product type
if ( ! in_array ( wc_clean ( $data [ 'type' ] ), array_keys ( wc_get_product_types () ) ) ) {
return new WP_Error ( '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 () ) ) ), array ( 'status' => 400 ) );
}
$new_product = array (
'post_title' => wc_clean ( $data [ 'title' ] ),
'post_status' => ( isset ( $data [ 'status' ] ) ? wc_clean ( $data [ 'status' ] ) : 'publish' ),
'post_type' => 'product' ,
'post_excerpt' => ( isset ( $data [ 'short_description' ] ) ? wc_clean ( $data [ 'short_description' ] ) : '' ),
'post_content' => ( isset ( $data [ 'description' ] ) ? wc_clean ( $data [ 'description' ] ) : '' ),
'post_author' => get_current_user_id (),
);
2014-06-24 18:19:34 +00:00
// Attempts to create the new product
2014-06-24 17:42:10 +00:00
$id = wp_insert_post ( $new_product , true );
2014-06-24 18:19:34 +00:00
// Checks for an error in the product creation
2014-06-24 17:42:10 +00:00
if ( is_wp_error ( $id ) ) {
return new WP_Error ( 'woocommerce_api_cannot_create_product' , $id -> get_error_message (), array ( 'status' => 400 ) );
}
2014-06-24 18:19:34 +00:00
// Check for featured/gallery images, upload it and set it
if ( isset ( $data [ 'images' ] ) ) {
2014-06-24 20:28:36 +00:00
$images = $this -> save_product_images ( $id , $data [ 'images' ] );
2014-06-24 18:19:34 +00:00
if ( is_wp_error ( $images ) ) {
return $images ;
}
}
2014-06-24 18:30:11 +00:00
// Save product meta fields
2014-06-24 20:16:36 +00:00
$meta = $this -> save_product_meta ( $id , $data );
2014-06-24 18:30:11 +00:00
if ( is_wp_error ( $meta ) ) {
return $meta ;
}
2014-06-26 19:47:54 +00:00
// Save variations
if ( isset ( $data [ 'type' ] ) && 'variable' == $data [ 'type' ] && isset ( $data [ 'variations' ] ) && is_array ( $data [ 'variations' ] ) ) {
$variations = $this -> save_variations ( $id , $data );
if ( is_wp_error ( $variations ) ) {
return $variations ;
}
}
2014-06-26 19:51:03 +00:00
do_action ( 'woocommerce_api_create_product' , $id , $data );
2014-06-26 19:47:54 +00:00
// Clear cache/transients
wc_delete_product_transients ( $id );
2014-06-24 17:42:10 +00:00
$this -> server -> send_status ( 201 );
return $this -> get_product ( $id );
}
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 ) {
2014-08-30 19:50:40 +00:00
$data = isset ( $data [ 'product' ] ) ? $data [ 'product' ] : array ();
2013-11-11 00:29:36 +00:00
$id = $this -> validate_request ( $id , 'product' , 'edit' );
2014-06-24 20:28:36 +00:00
if ( is_wp_error ( $id ) ) {
2013-11-11 00:29:36 +00:00
return $id ;
2014-06-24 20:28:36 +00:00
}
2014-08-30 19:50:40 +00:00
$data = apply_filters ( 'woocommerce_api_edit_product_data' , $data , $this );
2014-06-24 20:34:06 +00:00
// Product name.
if ( isset ( $data [ 'title' ] ) ) {
wp_update_post ( array ( 'ID' => $id , 'post_title' => wc_clean ( $data [ 'title' ] ) ) );
}
// Product status.
if ( isset ( $data [ 'status' ] ) ) {
wp_update_post ( array ( 'ID' => $id , 'post_status' => wc_clean ( $data [ 'status' ] ) ) );
}
// Product short description.
if ( isset ( $data [ 'short_description' ] ) ) {
wp_update_post ( array ( 'ID' => $id , 'post_excerpt' => wc_clean ( $data [ 'short_description' ] ) ) );
}
// Product description.
if ( isset ( $data [ 'description' ] ) ) {
wp_update_post ( array ( 'ID' => $id , 'post_content' => wc_clean ( $data [ 'description' ] ) ) );
}
2014-06-25 16:27:48 +00:00
// Validate the product type
if ( isset ( $data [ 'type' ] ) && ! in_array ( wc_clean ( $data [ 'type' ] ), array_keys ( wc_get_product_types () ) ) ) {
return new WP_Error ( '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 () ) ) ), array ( 'status' => 400 ) );
}
2014-06-24 20:28:36 +00:00
// Check for featured/gallery images, upload it and set it
if ( isset ( $data [ 'images' ] ) ) {
$images = $this -> save_product_images ( $id , $data [ 'images' ] );
if ( is_wp_error ( $images ) ) {
return $images ;
}
}
// Save product meta fields
$meta = $this -> save_product_meta ( $id , $data );
if ( is_wp_error ( $meta ) ) {
return $meta ;
}
2013-11-04 06:36:31 +00:00
2014-06-26 19:47:54 +00:00
// Save variations
if ( isset ( $data [ 'type' ] ) && 'variable' == $data [ 'type' ] && isset ( $data [ 'variations' ] ) && is_array ( $data [ 'variations' ] ) ) {
$variations = $this -> save_variations ( $id , $data );
if ( is_wp_error ( $variations ) ) {
return $variations ;
}
}
2014-06-26 19:51:03 +00:00
do_action ( 'woocommerce_api_edit_product' , $id , $data );
2014-06-26 19:47:54 +00:00
// Clear cache/transients
wc_delete_product_transients ( $id );
2013-11-11 00:29:36 +00:00
return $this -> get_product ( $id );
2013-11-04 06:36:31 +00:00
}
/**
* Delete a product
*
2014-06-24 20:16:08 +00:00
* @ since 2.2
2013-11-04 06:36:31 +00:00
* @ param int $id the product ID
* @ param bool $force true to permanently delete order , false to move to trash
* @ 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 );
2013-11-11 00:29:36 +00:00
return $this -> delete ( $id , 'product' , ( 'true' === $force ) );
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
2013-11-14 17:40:35 +00:00
$args = array (
'post_id' => $id ,
'approve' => 'approve' ,
);
$comments = get_comments ( $args );
$reviews = array ();
2013-11-04 06:36:31 +00:00
2013-11-14 17:40:35 +00:00
foreach ( $comments as $comment ) {
$reviews [] = array (
'id' => $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 ,
2013-11-25 12:52:53 +00:00
'verified' => ( bool ) wc_customer_bought_product ( $comment -> comment_author_email , $comment -> user_id , $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
}
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 ) {
2014-07-28 23:01:50 +00:00
// permissions check
if ( ! current_user_can ( 'manage_product_terms' ) ) {
return new WP_Error ( " woocommerce_api_user_cannot_read_product_categories " , __ ( 'You do not have permission to read product categories' , 'woocommerce' ), array ( 'status' => 401 ) );
}
$product_categories = array ();
$terms = get_terms ( 'product_cat' , array ( 'hide_empty' => false , 'fields' => 'ids' ) );
foreach ( $terms as $term_id ) {
2014-08-30 19:02:19 +00:00
$product_categories [] = current ( $this -> get_product_category ( $term_id , $fields ) );
2014-07-28 23:01:50 +00:00
}
2014-08-30 19:02:19 +00:00
return array ( 'product_categories' => apply_filters ( 'woocommerce_api_product_categories_response' , $product_categories , $terms , $fields , $this ) );
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 ) {
2014-07-28 23:01:50 +00:00
$id = absint ( $id );
// validate ID
if ( empty ( $id ) ) {
return new WP_Error ( 'woocommerce_api_invalid_product_category_id' , __ ( 'Invalid product category ID' , 'woocommerce' ), array ( 'status' => 400 ) );
}
// permissions check
if ( ! current_user_can ( 'manage_product_terms' ) ) {
return new WP_Error ( 'woocommerce_api_user_cannot_read_product_categories' , __ ( 'You do not have permission to read product categories' , 'woocommerce' ), array ( 'status' => 401 ) );
}
$term = get_term ( $id , 'product_cat' );
if ( is_wp_error ( $term ) || is_null ( $term ) ) {
return new WP_Error ( 'woocommerce_api_invalid_product_category_id' , __ ( 'A product category with the provided ID could not be found' , 'woocommerce' ), array ( 'status' => 404 ) );
}
$product_category = array (
'id' => intval ( $term -> term_id ),
'name' => $term -> name ,
'slug' => $term -> slug ,
'parent' => $term -> parent ,
'description' => $term -> description ,
'count' => intval ( $term -> count ),
);
2014-08-30 19:02:19 +00:00
return array ( 'product_category' => apply_filters ( 'woocommerce_api_product_category_response' , $product_category , $id , $fields , $term , $this ) );
2014-07-28 23:01:50 +00:00
}
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
// set base query arguments
$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
);
2013-11-11 00:29:36 +00:00
if ( ! empty ( $args [ 'type' ] ) ) {
2013-11-18 21:47:38 +00:00
$types = explode ( ',' , $args [ 'type' ] );
2013-11-11 00:29:36 +00:00
$query_args [ 'tax_query' ] = array (
array (
'taxonomy' => 'product_type' ,
'field' => 'slug' ,
2013-11-18 21:47:38 +00:00
'terms' => $types ,
2013-11-11 00:29:36 +00:00
),
);
unset ( $args [ 'type' ] );
}
$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 (),
2013-11-25 13:34:21 +00:00
'price' => wc_format_decimal ( $product -> get_price (), 2 ),
'regular_price' => wc_format_decimal ( $product -> get_regular_price (), 2 ),
'sale_price' => $product -> get_sale_price () ? wc_format_decimal ( $product -> get_sale_price (), 2 ) : 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 (),
2013-11-18 21:47:38 +00:00
'stock_quantity' => ( int ) $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 (),
2013-11-25 13:34:21 +00:00
'weight' => $product -> get_weight () ? wc_format_decimal ( $product -> get_weight (), 2 ) : 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 () ),
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 ),
2014-01-14 03:14:20 +00:00
'featured_src' => 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-02-13 04:49:30 +00:00
'purchase_note' => wpautop ( do_shortcode ( $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 (),
);
}
/**
* Get an individual variation ' s data
*
* @ since 2.1
* @ param WC_Product $product
* @ return array
*/
private function get_variation_data ( $product ) {
$variations = array ();
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 (
'id' => $variation -> get_variation_id (),
2013-11-18 21:47:38 +00:00
'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 ),
2013-11-14 17:40:35 +00:00
'downloadable' => $variation -> is_downloadable (),
'virtual' => $variation -> is_virtual (),
'permalink' => $variation -> get_permalink (),
'sku' => $variation -> get_sku (),
2013-11-25 13:34:21 +00:00
'price' => wc_format_decimal ( $variation -> get_price (), 2 ),
'regular_price' => wc_format_decimal ( $variation -> get_regular_price (), 2 ),
'sale_price' => $variation -> get_sale_price () ? wc_format_decimal ( $variation -> get_sale_price (), 2 ) : null ,
2013-11-14 17:40:35 +00:00
'taxable' => $variation -> is_taxable (),
'tax_status' => $variation -> get_tax_status (),
'tax_class' => $variation -> get_tax_class (),
2013-11-18 21:47:38 +00:00
'stock_quantity' => ( int ) $variation -> get_stock_quantity (),
2013-11-14 17:40:35 +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 (),
2013-11-25 13:34:21 +00:00
'weight' => $variation -> get_weight () ? wc_format_decimal ( $variation -> get_weight (), 2 ) : null ,
2013-11-14 17:40:35 +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 ),
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
);
}
return $variations ;
}
2014-06-24 18:30:11 +00:00
/**
* Save product meta
*
* @ since 2.2
* @ param int $id
* @ param array $data
* @ return bool | WP_Error
*/
2014-06-25 19:27:24 +00:00
protected function save_product_meta ( $id , $data ) {
2014-06-24 18:30:11 +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' ] );
wp_set_object_terms ( $id , $product_type , 'product_type' );
} else {
$_product_type = get_the_terms ( $id , 'product_type' );
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
}
}
2014-06-26 19:47:54 +00:00
// Virtual
if ( isset ( $data [ 'virtual' ] ) ) {
update_post_meta ( $id , '_virtual' , ( true === $data [ 'virtual' ] ) ? 'yes' : 'no' );
}
2014-06-24 18:30:11 +00:00
// Tax status
if ( isset ( $data [ 'tax_status' ] ) ) {
2014-06-24 20:17:51 +00:00
update_post_meta ( $id , '_tax_status' , wc_clean ( $data [ 'tax_status' ] ) );
2014-06-24 18:30:11 +00:00
}
// Tax Class
if ( isset ( $data [ 'tax_class' ] ) ) {
2014-06-24 20:17:51 +00:00
update_post_meta ( $id , '_tax_class' , wc_clean ( $data [ 'tax_class' ] ) );
2014-06-24 18:30:11 +00:00
}
// Catalog Visibility
if ( isset ( $data [ 'catalog_visibility' ] ) ) {
2014-06-24 20:17:51 +00:00
update_post_meta ( $id , '_visibility' , wc_clean ( $data [ 'catalog_visibility' ] ) );
2014-06-24 18:30:11 +00:00
}
// Purchase Note
if ( isset ( $data [ 'purchase_note' ] ) ) {
2014-06-24 20:17:51 +00:00
update_post_meta ( $id , '_purchase_note' , wc_clean ( $data [ 'purchase_note' ] ) );
2014-06-24 18:30:11 +00:00
}
// Featured Product
if ( isset ( $data [ 'featured' ] ) ) {
2014-06-25 19:20:28 +00:00
update_post_meta ( $id , '_featured' , ( true === $data [ 'featured' ] ) ? 'yes' : 'no' );
2014-06-24 18:30:11 +00:00
}
2014-06-26 19:47:54 +00:00
// Shipping data
$this -> save_product_shipping_data ( $id , $data );
2014-06-24 18:30:11 +00:00
// SKU
if ( isset ( $data [ 'sku' ] ) ) {
2014-06-27 18:58:29 +00:00
$sku = get_post_meta ( $id , '_sku' , true );
$new_sku = wc_clean ( $data [ 'sku' ] );
if ( '' == $new_sku ) {
update_post_meta ( $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 ( $id , $new_sku );
2014-06-27 18:58:29 +00:00
if ( ! $unique_sku ) {
2014-08-15 19:08:43 +00:00
return new WP_Error ( 'woocommerce_api_product_sku_already_exists' , __ ( 'The SKU already exists on another product' , 'woocommerce' ), array ( 'status' => 400 ) );
2014-06-27 18:58:29 +00:00
} else {
update_post_meta ( $id , '_sku' , $new_sku );
}
} else {
update_post_meta ( $id , '_sku' , '' );
}
2014-06-24 18:30:11 +00:00
}
}
// Attributes
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 ) {
2014-06-24 21:32:37 +00:00
$is_taxonomy = 0 ;
if ( ! isset ( $attribute [ 'name' ] ) ) {
continue ;
}
2014-06-26 19:47:54 +00:00
$taxonomy = $this -> get_attribute_taxonomy_by_label ( $attribute [ 'name' ] );
if ( $taxonomy ) {
$is_taxonomy = 1 ;
2014-06-24 21:32:37 +00:00
}
if ( $is_taxonomy ) {
if ( isset ( $attribute [ 'options' ] ) ) {
// Select based attributes - Format values (posted values are slugs)
if ( is_array ( $attribute [ 'options' ] ) ) {
$values = array_map ( 'sanitize_title' , $attribute [ 'options' ] );
// Text based attributes - Posted values are term names - don't change to slugs
} else {
$values = array_map ( 'stripslashes' , array_map ( 'strip_tags' , explode ( WC_DELIMITER , $attribute [ 'options' ] ) ) );
}
$values = array_filter ( $values , 'strlen' );
} else {
$values = array ();
}
// Update post terms
if ( taxonomy_exists ( $taxonomy ) ) {
wp_set_object_terms ( $id , $values , $taxonomy );
}
if ( $values ) {
// Add attribute to array, but don't set values
$attributes [ $taxonomy ] = array (
'name' => $taxonomy ,
'value' => '' ,
'position' => isset ( $attribute [ 'position' ] ) ? absint ( $attribute [ 'position' ] ) : 0 ,
'is_visible' => ( isset ( $attribute [ 'position' ] ) && $attribute [ 'position' ] ) ? 1 : 0 ,
'is_variation' => ( isset ( $attribute [ 'variation' ] ) && $attribute [ 'variation' ] ) ? 1 : 0 ,
'is_taxonomy' => $is_taxonomy
);
}
} elseif ( isset ( $attribute [ 'options' ] ) ) {
// Array based
if ( is_array ( $attribute [ 'options' ] ) ) {
$values = implode ( ' ' . WC_DELIMITER . ' ' , array_map ( 'wc_clean' , $attribute [ 'options' ] ) );
// Text based, separate by pipe
} else {
$values = implode ( ' ' . WC_DELIMITER . ' ' , array_map ( 'wc_clean' , explode ( WC_DELIMITER , $attribute [ 'options' ] ) ) );
}
// Custom attribute - Add attribute to array and set the values
$attributes [ sanitize_title ( $attribute [ 'name' ] ) ] = array (
'name' => wc_clean ( $attribute [ 'name' ] ),
'value' => $values ,
'position' => isset ( $attribute [ 'position' ] ) ? absint ( $attribute [ 'position' ] ) : 0 ,
'is_visible' => ( isset ( $attribute [ 'position' ] ) && $attribute [ 'position' ] ) ? 1 : 0 ,
'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' );
update_post_meta ( $id , '_product_attributes' , $attributes );
2014-06-24 18:30:11 +00:00
}
2014-06-25 16:27:48 +00:00
// Sales and prices
if ( in_array ( $product_type , array ( 'variable' , 'grouped' ) ) ) {
// Variable and grouped products have no prices
update_post_meta ( $id , '_regular_price' , '' );
update_post_meta ( $id , '_sale_price' , '' );
update_post_meta ( $id , '_sale_price_dates_from' , '' );
update_post_meta ( $id , '_sale_price_dates_to' , '' );
update_post_meta ( $id , '_price' , '' );
} else {
// Regular Price
if ( isset ( $data [ 'regular_price' ] ) ) {
2014-06-25 19:20:28 +00:00
$regular_price = ( '' === $data [ 'regular_price' ] ) ? '' : wc_format_decimal ( $data [ 'regular_price' ] );
2014-06-25 16:27:48 +00:00
update_post_meta ( $id , '_regular_price' , $regular_price );
} else {
$regular_price = get_post_meta ( $id , '_regular_price' , true );
}
// Sale Price
if ( isset ( $data [ 'sale_price' ] ) ) {
2014-06-25 19:20:28 +00:00
$sale_price = ( '' === $data [ 'sale_price' ] ) ? '' : wc_format_decimal ( $data [ 'sale_price' ] );
2014-06-25 16:27:48 +00:00
update_post_meta ( $id , '_sale_price' , $sale_price );
} else {
$sale_price = get_post_meta ( $id , '_sale_price' , true );
}
$date_from = isset ( $data [ 'sale_price_dates_from' ] ) ? $data [ 'sale_price_dates_from' ] : get_post_meta ( $id , '_sale_price_dates_from' , true );
2014-06-25 19:20:28 +00:00
$date_to = isset ( $data [ 'sale_price_dates_to' ] ) ? $data [ 'sale_price_dates_to' ] : get_post_meta ( $id , '_sale_price_dates_to' , true );
2014-06-25 16:27:48 +00:00
// Dates
if ( $date_from ) {
update_post_meta ( $id , '_sale_price_dates_from' , strtotime ( $date_from ) );
} else {
update_post_meta ( $id , '_sale_price_dates_from' , '' );
}
if ( $date_to ) {
update_post_meta ( $id , '_sale_price_dates_to' , strtotime ( $date_to ) );
} else {
update_post_meta ( $id , '_sale_price_dates_to' , '' );
}
2014-06-24 18:30:11 +00:00
2014-06-25 16:27:48 +00:00
if ( $date_to && ! $date_from ) {
update_post_meta ( $id , '_sale_price_dates_from' , strtotime ( 'NOW' , current_time ( 'timestamp' ) ) );
}
// Update price if on sale
2014-06-25 19:20:28 +00:00
if ( '' !== $sale_price && '' == $date_to && '' == $date_from ) {
2014-06-25 16:27:48 +00:00
update_post_meta ( $id , '_price' , wc_format_decimal ( $sale_price ) );
} else {
update_post_meta ( $id , '_price' , $regular_price );
}
2014-06-25 19:20:28 +00:00
if ( '' !== $sale_price && $date_from && strtotime ( $date_from ) < strtotime ( 'NOW' , current_time ( 'timestamp' ) ) ) {
2014-06-25 16:27:48 +00:00
update_post_meta ( $id , '_price' , wc_format_decimal ( $sale_price ) );
}
if ( $date_to && strtotime ( $date_to ) < strtotime ( 'NOW' , current_time ( 'timestamp' ) ) ) {
update_post_meta ( $id , '_price' , $regular_price );
update_post_meta ( $id , '_sale_price_dates_from' , '' );
update_post_meta ( $id , '_sale_price_dates_to' , '' );
}
2014-06-24 18:30:11 +00:00
}
2014-06-25 19:20:28 +00:00
// Update parent if grouped so price sorting works and stays in sync with the cheapest child
2014-08-19 10:09:29 +00:00
$_product = wc_get_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' ) {
$clear_parent_ids [] = $id ;
}
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 );
}
}
}
}
}
2014-06-24 18:30:11 +00:00
// Sold Individually
if ( isset ( $data [ 'sold_individually' ] ) ) {
2014-06-25 19:20:28 +00:00
update_post_meta ( $id , '_sold_individually' , ( true === $data [ 'sold_individually' ] ) ? 'yes' : '' );
}
// Stock status
if ( isset ( $data [ 'in_stock' ] ) ) {
$stock_status = ( true === $data [ 'in_stock' ] ) ? 'instock' : 'outofstock' ;
} else {
$stock_status = get_post_meta ( $id , '_stock_status' , true );
if ( '' === $stock_status ) {
$stock_status = 'instock' ;
2014-06-24 18:30:11 +00:00
}
}
2014-06-25 19:20:28 +00:00
// Stock Data
if ( 'yes' == get_option ( 'woocommerce_manage_stock' ) ) {
// Manage stock
if ( isset ( $data [ 'managing_stock' ] ) ) {
$managing_stock = ( true === $data [ 'managing_stock' ] ) ? 'yes' : 'no' ;
update_post_meta ( $id , '_manage_stock' , $managing_stock );
} else {
$managing_stock = get_post_meta ( $id , '_manage_stock' , true );
}
// Backorders
if ( isset ( $data [ 'backorders' ] ) ) {
if ( 'notify' == $data [ 'backorders' ] ) {
$backorders = 'notify' ;
} else {
$backorders = ( true === $data [ 'backorders' ] ) ? 'yes' : 'no' ;
}
update_post_meta ( $id , '_backorders' , $backorders );
} else {
$backorders = get_post_meta ( $id , '_backorders' , true );
}
if ( 'grouped' == $product_type ) {
update_post_meta ( $id , '_manage_stock' , 'no' );
update_post_meta ( $id , '_backorders' , 'no' );
update_post_meta ( $id , '_stock' , '' );
wc_update_product_stock_status ( $id , $stock_status );
} elseif ( 'external' == $product_type ) {
update_post_meta ( $id , '_manage_stock' , 'no' );
update_post_meta ( $id , '_backorders' , 'no' );
update_post_meta ( $id , '_stock' , '' );
wc_update_product_stock_status ( $id , 'instock' );
} elseif ( 'yes' == $managing_stock ) {
update_post_meta ( $id , '_backorders' , $backorders );
wc_update_product_stock_status ( $id , $stock_status );
// Stock quantity
if ( isset ( $data [ 'stock_quantity' ] ) ) {
wc_update_product_stock ( $id , intval ( $data [ 'stock_quantity' ] ) );
}
2014-06-24 18:30:11 +00:00
} else {
2014-06-25 19:20:28 +00:00
// Don't manage stock
2014-06-24 20:17:51 +00:00
update_post_meta ( $id , '_manage_stock' , 'no' );
2014-06-25 19:20:28 +00:00
update_post_meta ( $id , '_backorders' , $backorders );
update_post_meta ( $id , '_stock' , '' );
wc_update_product_stock_status ( $id , $stock_status );
2014-06-24 18:30:11 +00:00
}
2014-06-25 19:20:28 +00:00
} else {
wc_update_product_stock_status ( $id , $stock_status );
2014-06-24 18:30:11 +00:00
}
2014-06-25 19:20:28 +00:00
// Upsells
if ( isset ( $data [ 'upsell_ids' ] ) ) {
$upsells = array ();
$ids = $data [ 'upsell_ids' ];
if ( ! empty ( $ids ) ) {
foreach ( $ids as $id ) {
if ( $id && $id > 0 ) {
$upsells [] = $id ;
}
}
update_post_meta ( $id , '_upsell_ids' , $upsells );
} else {
delete_post_meta ( $id , '_upsell_ids' );
}
}
// Cross sells
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 ;
}
}
update_post_meta ( $id , '_crosssell_ids' , $crosssells );
} else {
delete_post_meta ( $id , '_crosssell_ids' );
}
}
2014-06-25 19:38:51 +00:00
// Product categories
if ( isset ( $data [ 'categories' ] ) && is_array ( $data [ 'categories' ] ) ) {
$terms = array_map ( 'wc_clean' , $data [ 'categories' ] );
wp_set_object_terms ( $id , $terms , 'product_cat' );
}
// Product tags
if ( isset ( $data [ 'tags' ] ) && is_array ( $data [ 'tags' ] ) ) {
$terms = array_map ( 'wc_clean' , $data [ 'tags' ] );
wp_set_object_terms ( $id , $terms , 'product_tag' );
}
2014-06-25 19:20:28 +00:00
// Downloadable
if ( isset ( $data [ 'downloadable' ] ) ) {
$is_downloadable = ( true === $data [ 'downloadable' ] ) ? 'yes' : 'no' ;
update_post_meta ( $id , '_downloadable' , $is_downloadable );
} else {
$is_downloadable = get_post_meta ( $id , '_downloadable' , true );
}
// Downloadable options
if ( 'yes' == $is_downloadable ) {
2014-06-26 19:47:54 +00:00
// Downloadable files
2014-06-25 19:27:24 +00:00
if ( isset ( $data [ 'downloads' ] ) && is_array ( $data [ 'downloads' ] ) ) {
2014-06-26 19:47:54 +00:00
$this -> save_downloadable_files ( $id , $data [ 'downloads' ] );
2014-06-25 19:20:28 +00:00
}
// Download limit
if ( isset ( $data [ 'download_limit' ] ) ) {
$download_limit = absint ( $data [ 'download_limit' ] );
update_post_meta ( $id , '_download_limit' , ( ! $download_limit ) ? '' : $download_limit );
}
// Download expiry
if ( isset ( $data [ 'download_expiry' ] ) ) {
$download_expiry = absint ( $data [ 'download_expiry' ] );
update_post_meta ( $id , '_download_expiry' , ( ! $download_expiry ) ? '' : $download_expiry );
}
// Download type
if ( isset ( $data [ 'download_type' ] ) ) {
update_post_meta ( $id , '_download_type' , wc_clean ( $data [ 'download_type' ] ) );
}
}
// Product url
if ( $product_type == 'external' ) {
if ( isset ( $data [ 'product_url' ] ) ) {
update_post_meta ( $id , '_product_url' , wc_clean ( $data [ 'product_url' ] ) );
}
if ( isset ( $data [ 'button_text' ] ) ) {
update_post_meta ( $id , '_button_text' , wc_clean ( $data [ 'button_text' ] ) );
}
}
2014-06-26 19:51:03 +00:00
// Do action for product type
do_action ( 'woocommerce_api_process_product_meta_' . $product_type , $id , $data );
2014-06-26 19:47:54 +00:00
return true ;
}
/**
* Save variations
*
* @ since 2.2
* @ param int $id
* @ param array $data
* @ return bool | WP_Error
*/
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 ) ) {
return $variation_id ;
}
// 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 ) {
2014-08-15 19:08:43 +00:00
return new WP_Error ( 'woocommerce_api_product_sku_already_exists' , __ ( 'The SKU already exists on another product' , 'woocommerce' ), array ( 'status' => 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
}
}
// Thumbnail
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' ] ) );
if ( is_wp_error ( $upload ) ) {
return new WP_Error ( 'woocommerce_api_cannot_upload_product_image' , $upload -> get_error_message (), array ( 'status' => 400 ) );
}
$attachment_id = $this -> set_product_image_as_attachment ( $upload , $id );
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
if ( isset ( $variation [ 'stock_quantity' ] ) ) {
wc_update_product_stock ( $variation_id , intval ( $variation [ 'stock_quantity' ] ) );
}
// Backorders
if ( isset ( $variation [ 'backorders' ] ) ) {
if ( $variation [ 'backorders' ] !== 'parent' ) {
if ( 'notify' == $variation [ 'backorders' ] ) {
$backorders = 'notify' ;
} else {
$backorders = ( true === $variation [ 'backorders' ] ) ? 'yes' : 'no' ;
}
update_post_meta ( $variation_id , '_backorders' , $backorders );
} else {
delete_post_meta ( $variation_id , '_backorders' );
}
}
// Regular Price
if ( isset ( $variation [ 'regular_price' ] ) ) {
$regular_price = ( '' === $variation [ 'regular_price' ] ) ? '' : wc_format_decimal ( $variation [ 'regular_price' ] );
update_post_meta ( $variation_id , '_regular_price' , $regular_price );
} else {
$regular_price = get_post_meta ( $variation_id , '_regular_price' , true );
}
// Sale Price
if ( isset ( $variation [ 'sale_price' ] ) ) {
$sale_price = ( '' === $variation [ 'sale_price' ] ) ? '' : wc_format_decimal ( $variation [ 'sale_price' ] );
update_post_meta ( $variation_id , '_sale_price' , $sale_price );
} else {
$sale_price = get_post_meta ( $variation_id , '_sale_price' , true );
}
$date_from = isset ( $variation [ 'sale_price_dates_from' ] ) ? $variation [ 'sale_price_dates_from' ] : get_post_meta ( $variation_id , '_sale_price_dates_from' , true );
$date_to = isset ( $variation [ 'sale_price_dates_to' ] ) ? $variation [ 'sale_price_dates_to' ] : get_post_meta ( $variation_id , '_sale_price_dates_to' , true );
// Save Dates
if ( $date_from ) {
update_post_meta ( $variation_id , '_sale_price_dates_from' , strtotime ( $date_from ) );
} else {
update_post_meta ( $variation_id , '_sale_price_dates_from' , '' );
}
if ( $date_to ) {
update_post_meta ( $variation_id , '_sale_price_dates_to' , strtotime ( $date_to ) );
} else {
update_post_meta ( $variation_id , '_sale_price_dates_to' , '' );
}
if ( $date_to && ! $date_from ) {
update_post_meta ( $variation_id , '_sale_price_dates_from' , strtotime ( 'NOW' , current_time ( 'timestamp' ) ) );
}
// Update price if on sale
if ( '' != $sale_price && '' == $date_to && '' == $date_from ) {
update_post_meta ( $variation_id , '_price' , $sale_price );
} else {
update_post_meta ( $variation_id , '_price' , $regular_price );
}
if ( '' != $sale_price && $date_from && strtotime ( $date_from ) < strtotime ( 'NOW' , current_time ( 'timestamp' ) ) ) {
update_post_meta ( $variation_id , '_price' , $sale_price );
}
if ( $date_to && strtotime ( $date_to ) < strtotime ( 'NOW' , current_time ( 'timestamp' ) ) ) {
update_post_meta ( $variation_id , '_price' , $regular_price );
update_post_meta ( $variation_id , '_sale_price_dates_from' , '' );
update_post_meta ( $variation_id , '_sale_price_dates_to' , '' );
}
// 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' , '' );
}
// 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 ) {
2014-06-30 19:36:13 +00:00
2014-06-26 19:47:54 +00:00
if ( ! isset ( $attribute [ 'name' ] ) ) {
continue ;
}
$taxonomy = $this -> get_attribute_taxonomy_by_label ( $attribute [ 'name' ] );
if ( isset ( $attributes [ $taxonomy ] ) ) {
$_attribute = $attributes [ $taxonomy ];
2014-08-28 19:25:08 +00:00
} elseif ( isset ( $attributes [ strtolower ( $attribute [ 'name' ] ) ] ) ) {
$_attribute = $attributes [ strtolower ( $attribute [ 'name' ] ) ];
}
2014-06-26 19:47:54 +00:00
2014-08-28 19:25:08 +00:00
if ( isset ( $_attribute [ 'is_variation' ] ) && $_attribute [ 'is_variation' ] ) {
$attribute_key = 'attribute_' . sanitize_title ( $_attribute [ 'name' ] );
$attribute_value = isset ( $attribute [ 'option' ] ) ? sanitize_title ( stripslashes ( $attribute [ 'option' ] ) ) : '' ;
$updated_attribute_keys [] = $attribute_key ;
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 );
// Update default attribute options setting
if ( isset ( $data [ 'default_attribute' ] ) && is_array ( $data [ 'default_attribute' ] ) ) {
$default_attributes = array ();
foreach ( $data [ 'default_attribute' ] as $default_attr_key => $default_attr ) {
if ( ! isset ( $default_attr [ 'name' ] ) ) {
continue ;
}
$taxonomy = $this -> get_attribute_taxonomy_by_label ( $default_attr [ 'name' ] );
if ( isset ( $attributes [ $taxonomy ] ) ) {
$_attribute = $attributes [ $taxonomy ];
if ( $_attribute [ 'is_variation' ] ) {
// Don't use wc_clean as it destroys sanitized characters
if ( isset ( $default_attr [ 'option' ] ) ) {
$value = sanitize_title ( trim ( stripslashes ( $default_attr [ 'option' ] ) ) );
} else {
$value = '' ;
}
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
* @ return void
*/
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
* @ return void
*/
private function save_downloadable_files ( $product_id , $downloads , $variation_id = 0 ) {
$files = array ();
// file paths will be stored in an array keyed off md5(file path)
foreach ( $downloads as $key => $file ) {
if ( ! isset ( $file [ 'url' ] ) ) {
continue ;
}
$file_name = isset ( $file [ 'name' ] ) ? wc_clean ( $file [ 'name' ] ) : '' ;
$file_url = wc_clean ( $file [ 'url' ] );
$files [ md5 ( $file_url ) ] = array (
'name' => $file_name ,
'file' => $file_url
);
}
// grant permission to any newly added files on any existing orders for this product prior to saving
do_action ( 'woocommerce_process_product_file_download_paths' , $product_id , $variation_id , $files );
update_post_meta ( $product_id , '_downloadable_files' , $files );
}
/**
* Get attribute taxonomy by label .
*
2014-06-26 19:55:24 +00:00
* @ since 2.2
* @ param string $label
2014-09-07 23:37:55 +00:00
* @ return string | null
2014-06-26 19:47:54 +00:00
*/
private function get_attribute_taxonomy_by_label ( $label ) {
$taxonomy = null ;
$attribute_taxonomies = wc_get_attribute_taxonomies ();
foreach ( $attribute_taxonomies as $key => $tax ) {
if ( $label == $tax -> attribute_label ) {
$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 () ) ) {
// add variation image if set
$attachment_ids [] = get_post_thumbnail_id ( $product -> get_variation_id () );
} elseif ( has_post_thumbnail ( $product -> id ) ) {
// otherwise use the parent product featured image if set
$attachment_ids [] = get_post_thumbnail_id ( $product -> id );
}
} else {
// add featured image
if ( has_post_thumbnail ( $product -> id ) ) {
$attachment_ids [] = get_post_thumbnail_id ( $product -> id );
}
// add gallery images
$attachment_ids = array_merge ( $attachment_ids , $product -> get_gallery_attachment_ids () );
}
// build image data
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 ),
'position' => $position ,
);
}
// set a placeholder image if the product has no images set
if ( empty ( $images ) ) {
$images [] = array (
'id' => 0 ,
2013-11-18 21:47:38 +00:00
'created_at' => $this -> server -> format_datetime ( time () ), // default to now
'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
/**
* Save product images
*
* @ since 2.2
* @ param array $images
2014-06-24 20:28:36 +00:00
* @ param int $id
2014-06-24 18:19:34 +00:00
* @ return void | WP_Error
*/
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' ] ) ) {
$upload = $this -> upload_product_image ( wc_clean ( $image [ 'src' ] ) );
2014-06-26 19:55:24 +00:00
2014-06-30 21:02:31 +00:00
if ( is_wp_error ( $upload ) ) {
return new WP_Error ( 'woocommerce_api_cannot_upload_product_image' , $upload -> get_error_message (), array ( 'status' => 400 ) );
}
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' ] ) ) {
$upload = $this -> upload_product_image ( wc_clean ( $image [ 'src' ] ) );
2014-06-30 20:57:42 +00:00
2014-06-30 21:02:31 +00:00
if ( is_wp_error ( $upload ) ) {
return new WP_Error ( 'woocommerce_api_cannot_upload_product_image' , $upload -> get_error_message (), array ( 'status' => 400 ) );
}
2014-06-30 20:57:42 +00:00
}
2014-06-26 19:55:24 +00:00
2014-06-30 21:02:31 +00:00
$gallery [] = $this -> set_product_image_as_attachment ( $upload , $id );
}
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
* @ return integer attachment id
*/
public function upload_product_image ( $image_url ) {
$file_name = basename ( current ( explode ( '?' , $image_url ) ) );
$wp_filetype = wp_check_filetype ( $file_name , null );
$parsed_url = @ parse_url ( $image_url );
// Check parsed URL
if ( ! $parsed_url || ! is_array ( $parsed_url ) ) {
2014-08-15 19:08:43 +00:00
return new WP_Error ( 'woocommerce_api_invalid_product_image' , sprintf ( __ ( 'Invalid URL %s' , 'woocommerce' ), $image_url ), array ( 'status' => 400 ) );
2014-06-24 18:19:34 +00:00
}
// Ensure url is valid
$image_url = str_replace ( ' ' , '%20' , $image_url );
// Get the file
$response = wp_remote_get ( $image_url , array (
'timeout' => 10
) );
if ( is_wp_error ( $response ) || 200 !== wp_remote_retrieve_response_code ( $response ) ) {
2014-08-15 19:08:43 +00:00
return new WP_Error ( 'woocommerce_api_invalid_remote_product_image' , sprintf ( __ ( 'Error getting remote image %s' , 'woocommerce' ), $image_url ), array ( 'status' => 400 ) );
2014-06-24 18:19:34 +00:00
}
// Ensure we have a file name and type
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 );
}
// Upload the file
$upload = wp_upload_bits ( $file_name , '' , wp_remote_retrieve_body ( $response ) );
if ( $upload [ 'error' ] ) {
return new WP_Error ( 'woocommerce_api_product_image_upload_error' , $upload [ 'error' ], array ( 'status' => 400 ) );
}
// Get filesize
$filesize = filesize ( $upload [ 'file' ] );
if ( 0 == $filesize ) {
@ unlink ( $upload [ 'file' ] );
unset ( $upload );
return new WP_Error ( 'woocommerce_api_product_image_upload_file_error' , __ ( 'Zero size file downloaded' , 'woocommerce' ), array ( 'status' => 400 ) );
}
unset ( $response );
return $upload ;
}
/**
2014-06-24 18:22:49 +00:00
* Get product image as attachment
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 ) {
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 (
'name' => ucwords ( str_replace ( 'attribute_' , '' , str_replace ( 'pa_' , '' , $attribute_name ) ) ),
'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 (
'name' => ucwords ( str_replace ( 'pa_' , '' , $attribute [ 'name' ] ) ),
'position' => $attribute [ 'position' ],
'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 ;
}
2014-07-14 15:18:07 +00:00
/**
* Get a job by applying to WooThemes
*
* @ ignore
* @ since 2.1
*/
private function get_a_job () {
/**
* Hey there coder ! At WooThemes we ' re always looking for talented people .
* It looks like you aren ' t afraid of digging in the code which we like .
* Want a new job ? Apply to work for us .
*
* @ param string $resume a description of your experience
* @ param string $cover_letter a description of why you ' re awesome . Do mention that you found this easter egg .
* @ link http :// www . woothemes . com / careers / #op-35124-woocommerce-product-developer
* @ return bool
*/
}
2013-11-04 06:36:31 +00:00
}