Fix final failing tests and implementing setters/getters and attributes functionality.

This commit is contained in:
Justin Shreve 2016-10-27 09:22:18 -07:00
parent 11dfc44778
commit e2b90f11f3
4 changed files with 219 additions and 241 deletions

View File

@ -24,52 +24,50 @@ class WC_Product extends WC_Abstract_Legacy_Product {
/**
* Stores product data.
*
* @todo download_limit download_expiry download_type downloads
*
* @var array
*/
protected $data = array(
'name' => '',
'slug' => '',
'date_created' => '',
'date_modified' => '',
'status' => false,
'featured' => false,
'catalog_visibility' => 'hidden',
'description' => '',
'short_description' => '',
'sku' => '',
'price' => '',
'regular_price' => '',
'sale_price' => '',
'date_on_sale_from' => '',
'date_on_sale_to' => '',
'total_sales' => '0',
'tax_status' => 'taxable',
'tax_class' => '',
'manage_stock' => false,
'stock_quantity' => null,
'stock_status' => '',
'backorders' => 'no',
'sold_individually' => false,
'weight' => '',
'length' => '',
'width' => '',
'height' => '',
'upsell_ids' => array(),
'cross_sell_ids' => array(),
'parent_id' => 0,
'reviews_allowed' => true,
'purchase_note' => '',
'attributes' => array(),
'default_attributes' => array(),
'menu_order' => 0,
'virtual' => false,
'downloadable' => false,
'category_ids' => array(),
'tag_ids' => array(),
'shipping_class_id' => 0,
'downloads' => array(),
'name' => '',
'slug' => '',
'date_created' => '',
'date_modified' => '',
'status' => false,
'featured' => false,
'catalog_visibility' => 'hidden',
'description' => '',
'short_description' => '',
'sku' => '',
'price' => '',
'regular_price' => '',
'sale_price' => '',
'date_on_sale_from' => '',
'date_on_sale_to' => '',
'total_sales' => '0',
'tax_status' => 'taxable',
'tax_class' => '',
'manage_stock' => false,
'stock_quantity' => null,
'stock_status' => '',
'backorders' => 'no',
'sold_individually' => false,
'weight' => '',
'length' => '',
'width' => '',
'height' => '',
'upsell_ids' => array(),
'cross_sell_ids' => array(),
'parent_id' => 0,
'reviews_allowed' => true,
'purchase_note' => '',
'attributes' => array(),
'default_attributes' => array(),
'menu_order' => 0,
'virtual' => false,
'downloadable' => false,
'category_ids' => array(),
'tag_ids' => array(),
'shipping_class_id' => 0,
'downloads' => array(),
'thumbnail_id' => '',
'gallery_attachment_ids' => array(),
'download_limit' => -1,
@ -1010,7 +1008,6 @@ class WC_Product extends WC_Abstract_Legacy_Product {
*/
public function set_attributes( $raw_attributes ) {
$attributes = array_fill_keys( array_keys( $this->data['attributes'] ), null );
foreach ( $raw_attributes as $attribute ) {
if ( is_a( $attribute, 'WC_Product_Attribute' ) ) {
$attributes[ sanitize_title( $attribute->get_name() ) ] = $attribute;
@ -1268,47 +1265,47 @@ class WC_Product extends WC_Abstract_Legacy_Product {
$this->set_id( $id );
$this->set_props( array(
'name' => get_the_title( $post_object ),
'slug' => $post_object->post_name,
'permalink' => get_permalink( $post_object ),
'date_created' => $post_object->post_date,
'date_modified' => $post_object->post_modified,
'type' => '',
'status' => $post_object->post_status,
'featured' => get_post_meta( $id, '_featured', true ),
'catalog_visibility' => get_post_meta( $id, '_visibility', true ),
'description' => $post_object->post_content,
'short_description' => $post_object->post_excerpt,
'sku' => get_post_meta( $id, '_sku', true ),
'regular_price' => get_post_meta( $id, '_regular_price', true ),
'sale_price' => get_post_meta( $id, '_sale_price', true ),
'date_on_sale_from' => get_post_meta( $id, '_sale_price_dates_from', true ),
'date_on_sale_to' => get_post_meta( $id, '_sale_price_dates_to', true ),
'total_sales' => get_post_meta( $id, 'total_sales', true ),
'tax_status' => get_post_meta( $id, '_tax_status', true ),
'tax_class' => get_post_meta( $id, '_tax_class', true ),
'manage_stock' => get_post_meta( $id, '_manage_stock', true ),
'stock_quantity' => get_post_meta( $id, '_stock', true ),
'stock_status' => get_post_meta( $id, '_stock_status', true ),
'backorders' => get_post_meta( $id, '_backorders', true ),
'sold_individually' => get_post_meta( $id, '_sold_individually', true ),
'weight' => get_post_meta( $id, '_weight', true ),
'length' => get_post_meta( $id, '_length', true ),
'width' => get_post_meta( $id, '_width', true ),
'height' => get_post_meta( $id, '_height', true ),
'upsell_ids' => get_post_meta( $id, '_upsell_ids', true ),
'cross_sell_ids' => get_post_meta( $id, '_crosssell_ids', true ),
'parent_id' => $post_object->post_parent,
'reviews_allowed' => 'open' === $post_object->comment_status,
'purchase_note' => get_post_meta( $id, '_purchase_note', true ),
'default_attributes' => get_post_meta( $id, '_default_attributes', true ),
'menu_order' => $post_object->menu_order,
'category_ids' => $this->get_term_ids( 'product_cat' ),
'tag_ids' => $this->get_term_ids( 'product_tag' ),
'shipping_class_id' => current( $this->get_term_ids( 'product_shipping_class' ) ),
'virtual' => get_post_meta( $id, '_virtual', true ),
'downloadable' => get_post_meta( $id, '_downloadable', true ),
'downloads' => array_filter( (array) get_post_meta( $id, '_downloadable_files', true ) ),
'name' => get_the_title( $post_object ),
'slug' => $post_object->post_name,
'permalink' => get_permalink( $post_object ),
'date_created' => $post_object->post_date,
'date_modified' => $post_object->post_modified,
'type' => '',
'status' => $post_object->post_status,
'featured' => get_post_meta( $id, '_featured', true ),
'catalog_visibility' => get_post_meta( $id, '_visibility', true ),
'description' => $post_object->post_content,
'short_description' => $post_object->post_excerpt,
'sku' => get_post_meta( $id, '_sku', true ),
'regular_price' => get_post_meta( $id, '_regular_price', true ),
'sale_price' => get_post_meta( $id, '_sale_price', true ),
'date_on_sale_from' => get_post_meta( $id, '_sale_price_dates_from', true ),
'date_on_sale_to' => get_post_meta( $id, '_sale_price_dates_to', true ),
'total_sales' => get_post_meta( $id, 'total_sales', true ),
'tax_status' => get_post_meta( $id, '_tax_status', true ),
'tax_class' => get_post_meta( $id, '_tax_class', true ),
'manage_stock' => get_post_meta( $id, '_manage_stock', true ),
'stock_quantity' => get_post_meta( $id, '_stock', true ),
'stock_status' => get_post_meta( $id, '_stock_status', true ),
'backorders' => get_post_meta( $id, '_backorders', true ),
'sold_individually' => get_post_meta( $id, '_sold_individually', true ),
'weight' => get_post_meta( $id, '_weight', true ),
'length' => get_post_meta( $id, '_length', true ),
'width' => get_post_meta( $id, '_width', true ),
'height' => get_post_meta( $id, '_height', true ),
'upsell_ids' => get_post_meta( $id, '_upsell_ids', true ),
'cross_sell_ids' => get_post_meta( $id, '_crosssell_ids', true ),
'parent_id' => $post_object->post_parent,
'reviews_allowed' => 'open' === $post_object->comment_status,
'purchase_note' => get_post_meta( $id, '_purchase_note', true ),
'default_attributes' => get_post_meta( $id, '_default_attributes', true ),
'menu_order' => $post_object->menu_order,
'category_ids' => $this->get_term_ids( 'product_cat' ),
'tag_ids' => $this->get_term_ids( 'product_tag' ),
'shipping_class_id' => current( $this->get_term_ids( 'product_shipping_class' ) ),
'virtual' => get_post_meta( $id, '_virtual', true ),
'downloadable' => get_post_meta( $id, '_downloadable', true ),
'downloads' => array_filter( (array) get_post_meta( $id, '_downloadable_files', true ) ),
'gallery_attachment_ids' => array_filter( explode( ',', get_post_meta( $id, '_product_image_gallery', true ) ) ),
'download_limit' => get_post_meta( $id, '_download_limit', true ),
'download_expiry' => get_post_meta( $id, '_download_expiry', true ),
@ -1381,7 +1378,6 @@ class WC_Product extends WC_Abstract_Legacy_Product {
if ( $id && ! is_wp_error( $id ) ) {
$this->set_id( $id );
wp_set_object_terms( $id, $this->get_type(), 'product_type' );
$this->update_post_meta();
$this->update_terms();
$this->update_attributes();
@ -1665,14 +1661,14 @@ class WC_Product extends WC_Abstract_Legacy_Product {
* @return bool
*/
public function is_on_sale() {
if ( '' !== $this->get_sale_price() && $this->get_regular_price() > $this->get_sale_price() ) {
if ( ! empty( $this->get_sale_price() ) && $this->get_regular_price() > $this->get_sale_price() ) {
$onsale = true;
if ( '' !== $this->get_date_on_sale_from() && $this->get_date_on_sale_from() > strtotime( 'NOW', current_time( 'timestamp' ) ) ) {
if ( ! empty( $this->get_date_on_sale_from() ) && $this->get_date_on_sale_from() > strtotime( 'NOW', current_time( 'timestamp' ) ) ) {
$onsale = false;
}
if ( '' !== $this->get_date_on_sale_to() && $this->get_date_on_sale_to() < strtotime( 'NOW', current_time( 'timestamp' ) ) ) {
if ( ! empty( $this->get_date_on_sale_to() ) && $this->get_date_on_sale_to() < strtotime( 'NOW', current_time( 'timestamp' ) ) ) {
$onsale = false;
}
} else {

View File

@ -269,15 +269,16 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
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() );
$variation = wc_get_product( $product->get_variation_id() );
$attachment_ids[] = $variation->get_thumbnail_id();
} elseif ( has_post_thumbnail( $product->get_id() ) ) {
// Otherwise use the parent product featured image if set.
$attachment_ids[] = get_post_thumbnail_id( $product->get_id() );
$attachment_ids[] = $product->get_thumbnail_id();
}
} else {
// Add featured image.
if ( has_post_thumbnail( $product->get_id() ) ) {
$attachment_ids[] = get_post_thumbnail_id( $product->get_id() );
$attachment_ids[] = $product->get_thumbnail_id();
}
// Add gallery images.
$attachment_ids = array_merge( $attachment_ids, $product->get_gallery_attachment_ids() );
@ -345,7 +346,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
$default = array();
if ( $product->is_type( 'variable' ) ) {
foreach ( array_filter( (array) get_post_meta( $product->get_id(), '_default_attributes', true ), 'strlen' ) as $key => $value ) {
foreach ( array_filter( (array) $product->get_default_attributes(), 'strlen' ) as $key => $value ) {
if ( 0 === strpos( $key, 'pa_' ) ) {
$default[] = array(
'id' => wc_attribute_taxonomy_id_by_name( $key ),
@ -491,7 +492,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
'price_html' => $product->get_price_html(),
'on_sale' => $product->is_on_sale(),
'purchasable' => $product->is_purchasable(),
'total_sales' => (int) get_post_meta( $product->get_id(), 'total_sales', true ),
'total_sales' => $product->get_total_sales(),
'virtual' => $product->is_virtual(),
'downloadable' => $product->is_downloadable(),
'downloads' => $this->get_downloads( $product ),
@ -551,7 +552,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
$variations = array();
foreach ( $product->get_children() as $child_id ) {
$variation = $product->get_child( $child_id );
$variation = wc_get_product( $child_id );
if ( ! $variation->exists() ) {
continue;
}
@ -576,8 +577,8 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
'virtual' => $variation->is_virtual(),
'downloadable' => $variation->is_downloadable(),
'downloads' => $this->get_downloads( $variation ),
'download_limit' => '' !== $variation->download_limit ? (int) $variation->download_limit : -1,
'download_expiry' => '' !== $variation->download_expiry ? (int) $variation->download_expiry : -1,
'download_limit' => '' !== $variation->get_download_limit() ? (int) $variation->get_download_limit() : -1,
'download_expiry' => '' !== $variation->get_download_expiry() ? (int) $variation->get_download_expiry() : -1,
'tax_status' => $variation->get_tax_status(),
'tax_class' => $variation->get_tax_class(),
'manage_stock' => $variation->managing_stock(),
@ -683,23 +684,13 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
* @return WP_Error|stdClass $data Post object.
*/
protected function prepare_item_for_database( $request ) {
$allowed_types = array( 'simple', 'grouped', 'external', 'variable', 'variation' );
// @ todo - use WC_Product_Factory::get_classname_from_product_type instead
/*
$classname = WC_Product_Factory::get_classname_from_product_type( $product_type );
if ( ! class_exists( $classname ) ) {
$classname = 'WC_Product_Simple';
}
*/
if ( isset( $request['type'] ) && in_array( $request['type'], $allowed_types ) ) {
$classname = "WC_Product_{$request['type']}";
} else {
$classname = "WC_Product_Simple";
}
if ( isset( $request['id'] ) ) {
$product = new $classname( absint( $request['id'] ) );
$product = wc_get_product( absint( $request['id'] ) );
} else {
$classname = WC_Product_Factory::get_classname_from_product_type( $request['type'] );
if ( ! class_exists( $classname ) ) {
$classname = 'WC_Product_Simple';
}
$product = new $classname();
}
@ -811,6 +802,8 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
$post = get_post( $product_id );
$this->update_additional_fields_for_object( $post, $request );
$this->update_post_meta_fields( $post, $request );
/**
* Fires after a single item is created or updated via the REST API.
*
@ -846,13 +839,12 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
/**
* Save product images.
*
* @param int $product_id
* @param WC_Product $product
* @param array $images
* @throws WC_REST_Exception
* @return WC_Product
*/
protected function save_product_images( $product_id, $images ) {
$product = wc_get_product( $product_id );
protected function save_product_images( $product, $images ) {
if ( is_array( $images ) ) {
$gallery = array();
@ -863,14 +855,14 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
$upload = wc_rest_upload_image_from_url( esc_url_raw( $image['src'] ) );
if ( is_wp_error( $upload ) ) {
if ( ! apply_filters( 'woocommerce_rest_suppress_image_upload_error', false, $upload, $product_id, $images ) ) {
if ( ! apply_filters( 'woocommerce_rest_suppress_image_upload_error', false, $upload, $product->get_id(), $images ) ) {
throw new WC_REST_Exception( 'woocommerce_product_image_upload_error', $upload->get_error_message(), 400 );
} else {
continue;
}
}
$attachment_id = wc_rest_set_uploaded_image_as_attachment( $upload, $product_id );
$attachment_id = wc_rest_set_uploaded_image_as_attachment( $upload, $product->get_id() );
}
if ( isset( $image['position'] ) && 0 === $image['position'] ) {
@ -898,18 +890,17 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
$product->set_gallery_attachment_ids( array() );
}
$product->save();
return $product;
}
/**
* Save product shipping data.
*
* @param int $product_id
* @param WC_Product $product
* @param array $data
* @return WC_Product
*/
private function save_product_shipping_data( $product_id, $data ) {
$product = wc_get_product( $product_id );
private function save_product_shipping_data( $product, $data ) {
// Virtual.
if ( isset( $data['virtual'] ) && true === $data['virtual'] ) {
$product->set_weight( '' );
@ -939,23 +930,25 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
// Shipping class.
if ( isset( $data['shipping_class'] ) ) {
// @todo needs shipping class in CRUD
wp_set_object_terms( $product_id, wc_clean( $data['shipping_class'] ), 'product_shipping_class' );
$shipping_class_term = get_term_by( 'slug', wc_clean( $data['shipping_class'] ), 'product_shipping_class' );
if ( $shipping_class_term ) {
$product->set_shipping_class_id( $shipping_class_term->term_id );
}
}
$product->save();
return $product;
}
/**
* Save downloadable files.
*
* @param in $product_id
* @param WC_Product $product
* @param array $downloads
* @param int $variation_id
* @return WC_Product
*/
private function save_downloadable_files( $product_id, $downloads, $variation_id = 0 ) {
private function save_downloadable_files( $product, $downloads, $variation_id = 0 ) {
$files = array();
$product = wc_get_product( $product_id );
// File paths will be stored in an array keyed off md5(file path).
foreach ( $downloads as $key => $file ) {
@ -982,24 +975,24 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
}
// 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 );
do_action( 'woocommerce_process_product_file_download_paths', $product->get_id(), $variation_id, $files );
$id = ( 0 === $variation_id ) ? $product_id : $variation_id;
$id = ( 0 === $variation_id ) ? $product->get_id() : $variation_id;
// @todo needs downloadable files in CRUD
update_post_meta( $id, '_downloadable_files', $files );
$product->set_downloads( $files );
return $product;
}
/**
* Save taxonomy terms.
*
* @param int $product_id
* @param WC_Product $product
* @param array $terms
* @param string $taxonomy
* @return array
* @return WC_Product
*/
protected function save_taxonomy_terms( $product_id, $terms, $taxonomy = 'cat' ) {
$product = wc_get_product( $product_id );
protected function save_taxonomy_terms( $product, $terms, $taxonomy = 'cat' ) {
$term_ids = wp_list_pluck( $terms, 'id' );
$term_ids = array_unique( array_map( 'intval', $term_ids ) );
@ -1009,9 +1002,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
$product->set_tag_ids( $term_ids );
}
$product->save();
return $terms;
return $product;
}
/**
@ -1019,7 +1010,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
*
* @param WC_Product $product
* @param WP_REST_Request $request
* @return bool
* @return WC_Product
* @throws WC_REST_Exception
*/
protected function save_product_meta( $product, $request ) {
@ -1030,8 +1021,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
// Virtual.
if ( isset( $request['virtual'] ) ) {
// @todo update
update_post_meta( $product->get_id(), '_virtual', true === $request['virtual'] ? 'yes' : 'no' );
$product->set_virtual( true === $request['virtual'] ? 'yes' : 'no' );
}
// Tax status.
@ -1060,7 +1050,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
}
// Shipping data.
$this->save_product_shipping_data( $product->get_id(), $request );
$product = $this->save_product_shipping_data( $product, $request );
// SKU.
if ( isset( $request['sku'] ) ) {
@ -1084,7 +1074,6 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
}
// Attributes.
error_log( print_r ( $request['attributes'], 1 ) );
if ( isset( $request['attributes'] ) ) {
$attributes = array();
@ -1104,10 +1093,6 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
continue;
}
error_log( print_r ( 'test', 1 ) );
error_log( print_r ( $attribute_id, 1 ) );
error_log( print_r ( $attribute_name, 1 ) );
if ( $attribute_id ) {
if ( isset( $attribute['options'] ) ) {
@ -1124,47 +1109,34 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
$values = array();
}
// Update post terms.
if ( taxonomy_exists( $attribute_name ) ) {
wp_set_object_terms( $product->get_id(), $values, $attribute_name );
}
if ( ! empty( $values ) ) {
// Add attribute to array, but don't set values.
$attributes[ $attribute_name ] = array(
'name' => $attribute_name,
'value' => '',
'position' => isset( $attribute['position'] ) ? (string) absint( $attribute['position'] ) : '0',
'is_visible' => ( isset( $attribute['visible'] ) && $attribute['visible'] ) ? 1 : 0,
'is_variation' => ( isset( $attribute['variation'] ) && $attribute['variation'] ) ? 1 : 0,
'is_taxonomy' => 1,
);
$attribute_object = new WC_Product_Attribute();
$attribute_object->set_id( $attribute_id );
$attribute_object->set_name( $attribute_name );
$attribute_object->set_options( array() );
$attribute_object->set_position( isset( $attribute['position'] ) ? (string) absint( $attribute['position'] ) : '0' );
$attribute_object->set_visible( ( isset( $attribute['visible'] ) && $attribute['visible'] ) ? 1 : 0 );
$attribute_object->set_variation( ( isset( $attribute['variation'] ) && $attribute['variation'] ) ? 1 : 0 );
$attributes[] = $attribute_object;
}
} elseif ( isset( $attribute['options'] ) ) {
// Array based.
if ( is_array( $attribute['options'] ) ) {
$values = wc_implode_text_attributes( array_map( 'wc_clean', $attribute['options'] ) );
// Text based, separate by pipe.
} else {
$values = wc_implode_text_attributes( 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' => $attribute_name,
'value' => $values,
'position' => isset( $attribute['position'] ) ? (string) absint( $attribute['position'] ) : '0',
'is_visible' => ( isset( $attribute['visible'] ) && $attribute['visible'] ) ? 1 : 0,
'is_variation' => ( isset( $attribute['variation'] ) && $attribute['variation'] ) ? 1 : 0,
'is_taxonomy' => 0,
);
if ( is_array( $attribute['options'] ) ) {
$values = $attribute['options'];
} else {
$values = explode( WC_DELIMITER, $attribute['options'] );
}
$attribute_object = new WC_Product_Attribute();
$attribute_object->set_name( $attribute_name );
$attribute_object->set_options( $values);
$attribute_object->set_position( isset( $attribute['position'] ) ? (string) absint( $attribute['position'] ) : '0' );
$attribute_object->set_visible( ( isset( $attribute['visible'] ) && $attribute['visible'] ) ? 1 : 0 );
$attribute_object->set_variation( ( isset( $attribute['variation'] ) && $attribute['variation'] ) ? 1 : 0 );
$attributes[] = $attribute_object;
}
}
uasort( $attributes, 'wc_product_attribute_uasort_comparison' );
// @todo attributes is being worked on
update_post_meta( $product->get_id(), '_product_attributes', $attributes );
$product->set_attributes( $attributes );
}
// Sales and prices.
@ -1198,14 +1170,14 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
$date_from = $request['date_on_sale_from'];
} else {
$date_from = $product->get_date_on_sale_from();
$date_from = ( '' === $date_from ) ? '' : date( 'Y-m-d', $date_from );
$date_from = ( '' === (string) $date_from ) ? '' : date( 'Y-m-d', $date_from );
}
if ( isset( $request['date_on_sale_to'] ) ) {
$date_to = $request['date_on_sale_to'];
} else {
$date_to = $product->get_date_on_sale_to();
$date_to = ( '' === $date_to ) ? '' : date( 'Y-m-d', $date_to );
$date_to = ( '' === (string) $date_to ) ? '' : date( 'Y-m-d', $date_to );
}
if ( $date_to && ! $date_from ) {
@ -1286,7 +1258,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
$manage_stock = ( true === $request['manage_stock'] ) ? 'yes' : 'no';
$product->set_manage_stock( $manage_stock );
} else {
$manage_stock = $product->get_manage_stock();
$manage_stock = $product->get_manage_stock() ? 'yes' : 'no';
}
// Backorders.
@ -1346,7 +1318,6 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
}
}
update_post_meta( $product->get_id(), '_upsell_ids', $upsells );
$product->set_upsell_ids( $upsells );
} else {
$product->set_upsell_ids( array() );
@ -1373,21 +1344,20 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
// Product categories.
if ( isset( $request['categories'] ) && is_array( $request['categories'] ) ) {
$this->save_taxonomy_terms( $product->get_id(), $request['categories'] );
$product = $this->save_taxonomy_terms( $product, $request['categories'] );
}
// Product tags.
if ( isset( $request['tags'] ) && is_array( $request['tags'] ) ) {
$this->save_taxonomy_terms( $product->get_id(), $request['tags'], 'tag' );
$product = $this->save_taxonomy_terms( $product, $request['tags'], 'tag' );
}
// Downloadable.
// @todo needs crud
if ( isset( $request['downloadable'] ) ) {
$is_downloadable = true === $request['downloadable'] ? 'yes' : 'no';
update_post_meta( $product->get_id(), '_downloadable', $is_downloadable );
$product->set_downloadable( $is_downloadable );
} else {
$is_downloadable = get_post_meta( $product->get_id(), '_downloadable', true );
$is_downloadable = $product->get_downloadable() ? 'yes' : 'no';
}
// Downloadable options.
@ -1395,7 +1365,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
// Downloadable files.
if ( isset( $request['downloads'] ) && is_array( $request['downloads'] ) ) {
$this->save_downloadable_files( $product->get_id(), $request['downloads'] );
$product = $this->save_downloadable_files( $product, $request['downloads'] );
}
// Download limit.
@ -1425,9 +1395,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
}
}
$product->save();
return true;
return $product;
}
/**
@ -1489,6 +1457,8 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
throw new WC_REST_Exception( 'woocommerce_rest_cannot_save_product_variation', $variation_id->get_error_message(), 400 );
}
$variation_object = wc_get_product( $variation_id );
// SKU.
if ( isset( $variation['sku'] ) ) {
$sku = get_post_meta( $variation_id, '_sku', true );
@ -1567,7 +1537,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
}
// Shipping data.
$this->save_product_shipping_data( $variation_id, $variation );
$variation_object = $this->save_product_shipping_data( $variation_object, $variation );
// Stock handling.
if ( isset( $variation['manage_stock'] ) ) {
@ -1651,7 +1621,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
if ( 'yes' === $is_downloadable ) {
// Downloadable files.
if ( isset( $variation['downloads'] ) && is_array( $variation['downloads'] ) ) {
$this->save_downloadable_files( $product->get_id(), $variation['downloads'], $variation_id );
$product = $this->save_downloadable_files( $product, $variation['downloads'], $variation_id );
}
// Download limit.
@ -1799,15 +1769,16 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
// Check for featured/gallery images, upload it and set it.
if ( isset( $request['images'] ) ) {
$this->save_product_images( $product->get_id(), $request['images'] );
$product = $this->save_product_images( $product, $request['images'] );
}
// Save product meta fields.
$this->save_product_meta( $product, $request );
$product = $this->save_product_meta( $product, $request );
$product->save();
// Save variations.
if ( isset( $request['type'] ) && 'variable' === $request['type'] && isset( $request['variations'] ) && is_array( $request['variations'] ) ) {
$this->save_variations_data( $product, $request );
$product = $this->save_variations_data( $product, $request );
}
return true;
@ -1829,16 +1800,17 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
// Check for featured/gallery images, upload it and set it.
if ( isset( $request['images'] ) ) {
$this->save_product_images( $product->get_id(), $request['images'] );
$product = $this->save_product_images( $product, $request['images'] );
}
// Save product meta fields.
$this->save_product_meta( $product, $request );
$product = $this->save_product_meta( $product, $request );
$product->save();
// Save variations.
if ( $product->is_type( 'variable' ) ) {
if ( isset( $request['variations'] ) && is_array( $request['variations'] ) ) {
$this->save_variations_data( $product, $request );
$product = $this->save_variations_data( $product, $request );
} else {
// Just sync variations.
WC_Product_Variable::sync( $product->get_id() );

View File

@ -171,8 +171,6 @@ class Products_API extends WC_REST_Unit_Test_Case {
$response = $this->server->dispatch( $request );
$data = $response->get_data();
error_log( print_r ( $data, 1 ) );
$this->assertContains( 'Testing', $data['description'] );
$this->assertEquals( '8', $data['price'] );
$this->assertEquals( '8', $data['sale_price'] );
@ -190,12 +188,16 @@ class Products_API extends WC_REST_Unit_Test_Case {
$request = new WP_REST_Request( 'PUT', '/wc/v1/products/' . $product->id );
$request->set_body_params( array(
'attributes' => array( array( 'id' => 0, 'name' => 'pa_size', 'options' => array( 'small', 'medium' ), 'visible' => false, 'variation' => 1 ) )
'attributes' => array(
array( 'id' => 0, 'name' => 'pa_color', 'options' => array( 'red', 'yellow' ), 'visible' => false, 'variation' => 1 ),
array( 'name' => 'pa_size', 'options' => array( 'small' ), 'visible' => false, 'variation' => 1 ),
),
) );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( array( 'small', 'medium' ), $data['attributes'][0]['options'] );
$this->assertEquals( array( 'small' ), $data['attributes'][0]['options'] );
$this->assertEquals( array( 'red', 'yellow' ), $data['attributes'][1]['options'] );
// test external product
$product = WC_Helper_Product::create_external_product();
@ -260,6 +262,14 @@ class Products_API extends WC_REST_Unit_Test_Case {
public function test_create_product() {
wp_set_current_user( $this->user );
$request = new WP_REST_Request( 'POST', '/wc/v1/products/shipping_classes' );
$request->set_body_params( array(
'name' => 'Test',
) );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$shipping_class_id = $data['id'];
// Create simple
$request = new WP_REST_Request( 'POST', '/wc/v1/products' );
$request->set_body_params( array(
@ -267,6 +277,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
'name' => 'Test Simple Product',
'sku' => 'DUMMY SKU SIMPLE API',
'regular_price' => '10',
'shipping_class' => 'test',
) );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
@ -277,6 +288,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
$this->assertEquals( 'DUMMY SKU SIMPLE API', $data['sku'] );
$this->assertEquals( 'Test Simple Product', $data['name'] );
$this->assertEquals( 'simple', $data['type'] );
$this->assertEquals( $shipping_class_id, $data['shipping_class_id'] );
// Create external
$request = new WP_REST_Request( 'POST', '/wc/v1/products' );
@ -311,8 +323,6 @@ class Products_API extends WC_REST_Unit_Test_Case {
$response = $this->server->dispatch( $request );
$data = $response->get_data();
//error_log( print_r ( $data, 1 ) );
$this->assertEquals( 'DUMMY SKU VARIABLE API', $data['sku'] );
$this->assertEquals( 'Test Variable Product', $data['name'] );
$this->assertEquals( 'variable', $data['type'] );

View File

@ -72,37 +72,37 @@ class WC_Tests_Product_CRUD extends WC_Unit_Test_Case {
*/
public function test_product_getters_and_setters() {
$getters_and_setters = array(
'name' => 'Test',
'slug' => 'test',
'status' => 'publish',
'catalog_visibility' => 'search',
'featured' => false,
'description' => 'Hello world',
'short_description' => 'hello',
'sku' => 'TEST SKU',
'regular_price' => 15.00,
'sale_price' => 10.00,
'date_on_sale_from' => '1475798400',
'date_on_sale_to' => '1477267200',
'total_sales' => 20,
'tax_status' => 'none',
'tax_class' => '',
'manage_stock' => true,
'stock_quantity' => 10,
'stock_status' => 'instock',
'backorders' => 'notify',
'sold_individually' => false,
'weight' => 100,
'length' => 10,
'width' => 10,
'height' => 10,
'upsell_ids' => array( 2, 3 ),
'cross_sell_ids' => array( 4, 5 ),
'parent_id' => 0,
'reviews_allowed' => true,
'default_attributes' => array(),
'purchase_note' => 'A note',
'menu_order' => 2,
'name' => 'Test',
'slug' => 'test',
'status' => 'publish',
'catalog_visibility' => 'search',
'featured' => false,
'description' => 'Hello world',
'short_description' => 'hello',
'sku' => 'TEST SKU',
'regular_price' => 15.00,
'sale_price' => 10.00,
'date_on_sale_from' => '1475798400',
'date_on_sale_to' => '1477267200',
'total_sales' => 20,
'tax_status' => 'none',
'tax_class' => '',
'manage_stock' => true,
'stock_quantity' => 10,
'stock_status' => 'instock',
'backorders' => 'notify',
'sold_individually' => false,
'weight' => 100,
'length' => 10,
'width' => 10,
'height' => 10,
'upsell_ids' => array( 2, 3 ),
'cross_sell_ids' => array( 4, 5 ),
'parent_id' => 0,
'reviews_allowed' => true,
'default_attributes' => array(),
'purchase_note' => 'A note',
'menu_order' => 2,
'gallery_attachment_ids' => array(),
'download_type' => 'standard',
'download_expiry' => -1,