/wp-content/plugins/woocommerce/includes/api/v2/class-wc-api-products.php
PHP | 1440 lines | 927 code | 260 blank | 253 comment | 237 complexity | ee7f92086dcff488b18317d87290bf82 MD5 | raw file
- <?php
- /**
- * WooCommerce API Products Class
- *
- * Handles requests to the /products endpoint
- *
- * @author WooThemes
- * @category API
- * @package WooCommerce/API
- * @since 2.1
- */
- if ( ! defined( 'ABSPATH' ) ) {
- exit; // Exit if accessed directly
- }
- class WC_API_Products extends WC_API_Resource {
- /** @var string $base the route base */
- protected $base = '/products';
- /**
- * Register the routes for this class
- *
- * GET/POST /products
- * GET /products/count
- * GET/PUT/DELETE /products/<id>
- * GET /products/<id>/reviews
- *
- * @since 2.1
- * @param array $routes
- * @return array
- */
- public function register_routes( $routes ) {
- # GET/POST /products
- $routes[ $this->base ] = array(
- array( array( $this, 'get_products' ), WC_API_Server::READABLE ),
- array( array( $this, 'create_product' ), WC_API_SERVER::CREATABLE | WC_API_Server::ACCEPT_DATA ),
- );
- # GET /products/count
- $routes[ $this->base . '/count'] = array(
- array( array( $this, 'get_products_count' ), WC_API_Server::READABLE ),
- );
- # GET/PUT/DELETE /products/<id>
- $routes[ $this->base . '/(?P<id>\d+)' ] = array(
- array( array( $this, 'get_product' ), WC_API_Server::READABLE ),
- array( array( $this, 'edit_product' ), WC_API_Server::EDITABLE | WC_API_Server::ACCEPT_DATA ),
- array( array( $this, 'delete_product' ), WC_API_Server::DELETABLE ),
- );
- # GET /products/<id>/reviews
- $routes[ $this->base . '/(?P<id>\d+)/reviews' ] = array(
- array( array( $this, 'get_product_reviews' ), WC_API_Server::READABLE ),
- );
- # GET /products/<id>/orders
- $routes[ $this->base . '/(?P<id>\d+)/orders' ] = array(
- array( array( $this, 'get_product_orders' ), WC_API_Server::READABLE ),
- );
- # 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 ),
- );
- # GET/POST /products/attributes
- $routes[ $this->base . '/attributes' ] = array(
- array( array( $this, 'get_product_attributes' ), WC_API_Server::READABLE ),
- array( array( $this, 'create_product_attribute' ), WC_API_SERVER::CREATABLE | WC_API_Server::ACCEPT_DATA ),
- );
- # GET/PUT/DELETE /attributes/<id>
- $routes[ $this->base . '/attributes/(?P<id>\d+)' ] = array(
- array( array( $this, 'get_product_attribute' ), WC_API_Server::READABLE ),
- array( array( $this, 'edit_product_attribute' ), WC_API_Server::EDITABLE | WC_API_Server::ACCEPT_DATA ),
- array( array( $this, 'delete_product_attribute' ), WC_API_Server::DELETABLE ),
- );
- # GET /products/sku/<product sku>
- $routes[ $this->base . '/sku/(?P<sku>\w[\w\s\-]*)' ] = array(
- array( array( $this, 'get_product_by_sku' ), WC_API_Server::READABLE ),
- );
- # POST|PUT /products/bulk
- $routes[ $this->base . '/bulk' ] = array(
- array( array( $this, 'bulk' ), WC_API_Server::EDITABLE | WC_API_Server::ACCEPT_DATA ),
- );
- return $routes;
- }
- /**
- * Get all products
- *
- * @since 2.1
- * @param string $fields
- * @param string $type
- * @param array $filter
- * @param int $page
- * @return array
- */
- public function get_products( $fields = null, $type = null, $filter = array(), $page = 1 ) {
- if ( ! empty( $type ) ) {
- $filter['type'] = $type;
- }
- $filter['page'] = $page;
- $query = $this->query_products( $filter );
- $products = array();
- foreach ( $query->posts as $product_id ) {
- if ( ! $this->is_readable( $product_id ) ) {
- continue;
- }
- $products[] = current( $this->get_product( $product_id, $fields ) );
- }
- $this->server->add_pagination_headers( $query );
- 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
- */
- public function get_product( $id, $fields = null ) {
- $id = $this->validate_request( $id, 'product', 'read' );
- if ( is_wp_error( $id ) ) {
- return $id;
- }
- $product = wc_get_product( $id );
- // 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->parent ) {
- $product_data['parent'] = $this->get_product_data( $product->parent );
- }
- return array( 'product' => apply_filters( 'woocommerce_api_product_response', $product_data, $product, $fields, $this->server ) );
- }
- /**
- * Get the total number of products
- *
- * @since 2.1
- * @param string $type
- * @param array $filter
- * @return array
- */
- public function get_products_count( $type = null, $filter = array() ) {
- try {
- if ( ! current_user_can( 'read_private_products' ) ) {
- throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_products_count', __( 'You do not have permission to read the products count', 'woocommerce' ), 401 );
- }
- if ( ! empty( $type ) ) {
- $filter['type'] = $type;
- }
- $query = $this->query_products( $filter );
- return array( 'count' => (int) $query->found_posts );
- } catch ( WC_API_Exception $e ) {
- return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
- }
- }
- /**
- * Create a new product
- *
- * @since 2.2
- * @param array $data posted data
- * @return array
- */
- public function create_product( $data ) {
- $id = 0;
- try {
- if ( ! isset( $data['product'] ) ) {
- throw new WC_API_Exception( 'woocommerce_api_missing_product_data', sprintf( __( 'No %1$s data specified to create %1$s', 'woocommerce' ), 'product' ), 400 );
- }
- $data = $data['product'];
- // Check permissions
- if ( ! current_user_can( 'publish_products' ) ) {
- throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_product', __( 'You do not have permission to create products', 'woocommerce' ), 401 );
- }
- $data = apply_filters( 'woocommerce_api_create_product_data', $data, $this );
- // Check if product title is specified
- if ( ! isset( $data['title'] ) ) {
- throw new WC_API_Exception( 'woocommerce_api_missing_product_title', sprintf( __( 'Missing parameter %s', 'woocommerce' ), 'title' ), 400 );
- }
- // Check product type
- if ( ! isset( $data['type'] ) ) {
- $data['type'] = 'simple';
- }
- // Set visible visibility when not sent
- if ( ! isset( $data['catalog_visibility'] ) ) {
- $data['catalog_visibility'] = 'visible';
- }
- // Validate the product type
- if ( ! in_array( wc_clean( $data['type'] ), array_keys( wc_get_product_types() ) ) ) {
- throw new WC_API_Exception( 'woocommerce_api_invalid_product_type', sprintf( __( 'Invalid product type - the product type must be any of these: %s', 'woocommerce' ), implode( ', ', array_keys( wc_get_product_types() ) ) ), 400 );
- }
- // Enable description html tags.
- $post_content = isset( $data['description'] ) ? wc_clean( $data['description'] ) : '';
- if ( $post_content && isset( $data['enable_html_description'] ) && true === $data['enable_html_description'] ) {
- $post_content = $data['description'];
- }
- // Enable short description html tags.
- $post_excerpt = isset( $data['short_description'] ) ? wc_clean( $data['short_description'] ) : '';
- if ( $post_excerpt && isset( $data['enable_html_short_description'] ) && true === $data['enable_html_short_description'] ) {
- $post_excerpt = $data['short_description'];
- }
- $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'] ) ? $post_excerpt : '' ),
- 'post_content' => ( isset( $data['description'] ) ? $post_content : '' ),
- 'post_author' => get_current_user_id(),
- );
- // Attempts to create the new product
- $id = wp_insert_post( $new_product, true );
- // Checks for an error in the product creation
- if ( is_wp_error( $id ) ) {
- throw new WC_API_Exception( 'woocommerce_api_cannot_create_product', $id->get_error_message(), 400 );
- }
- // Check for featured/gallery images, upload it and set it
- if ( isset( $data['images'] ) ) {
- $this->save_product_images( $id, $data['images'] );
- }
- // Save product meta fields
- $this->save_product_meta( $id, $data );
- // Save variations
- if ( isset( $data['type'] ) && 'variable' == $data['type'] && isset( $data['variations'] ) && is_array( $data['variations'] ) ) {
- $this->save_variations( $id, $data );
- }
- do_action( 'woocommerce_api_create_product', $id, $data );
- // Clear cache/transients
- wc_delete_product_transients( $id );
- $this->server->send_status( 201 );
- return $this->get_product( $id );
- } catch ( WC_API_Exception $e ) {
- // Remove the product when fails
- $this->clear_product( $id );
- return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
- }
- }
- /**
- * Edit a product
- *
- * @since 2.2
- * @param int $id the product ID
- * @param array $data
- * @return array
- */
- public function edit_product( $id, $data ) {
- try {
- if ( ! isset( $data['product'] ) ) {
- throw new WC_API_Exception( 'woocommerce_api_missing_product_data', sprintf( __( 'No %1$s data specified to edit %1$s', 'woocommerce' ), 'product' ), 400 );
- }
- $data = $data['product'];
- $id = $this->validate_request( $id, 'product', 'edit' );
- if ( is_wp_error( $id ) ) {
- return $id;
- }
- $data = apply_filters( 'woocommerce_api_edit_product_data', $data, $this );
- // Product title.
- if ( isset( $data['title'] ) ) {
- wp_update_post( array( 'ID' => $id, 'post_title' => wc_clean( $data['title'] ) ) );
- }
- // Product name (slug).
- if ( isset( $data['name'] ) ) {
- wp_update_post( array( 'ID' => $id, 'post_name' => sanitize_title( $data['name'] ) ) );
- }
- // 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'] ) ) {
- // Enable short description html tags.
- $post_excerpt = ( isset( $data['enable_html_short_description'] ) && true === $data['enable_html_short_description'] ) ? $data['short_description'] : wc_clean( $data['short_description'] );
- wp_update_post( array( 'ID' => $id, 'post_excerpt' => $post_excerpt ) );
- }
- // Product description.
- if ( isset( $data['description'] ) ) {
- // Enable description html tags.
- $post_content = ( isset( $data['enable_html_description'] ) && true === $data['enable_html_description'] ) ? $data['description'] : wc_clean( $data['description'] );
- wp_update_post( array( 'ID' => $id, 'post_content' => $post_content ) );
- }
- // Validate the product type
- if ( isset( $data['type'] ) && ! in_array( wc_clean( $data['type'] ), array_keys( wc_get_product_types() ) ) ) {
- throw new WC_API_Exception( 'woocommerce_api_invalid_product_type', sprintf( __( 'Invalid product type - the product type must be any of these: %s', 'woocommerce' ), implode( ', ', array_keys( wc_get_product_types() ) ) ), 400 );
- }
- // Check for featured/gallery images, upload it and set it
- if ( isset( $data['images'] ) ) {
- $this->save_product_images( $id, $data['images'] );
- }
- // Save product meta fields
- $this->save_product_meta( $id, $data );
- // Save variations
- $product = get_product( $id );
- if ( $product->is_type( 'variable' ) ) {
- if ( isset( $data['variations'] ) && is_array( $data['variations'] ) ) {
- $this->save_variations( $id, $data );
- } else {
- // Just sync variations
- WC_Product_Variable::sync( $id );
- }
- }
- do_action( 'woocommerce_api_edit_product', $id, $data );
- // Clear cache/transients
- wc_delete_product_transients( $id );
- return $this->get_product( $id );
- } catch ( WC_API_Exception $e ) {
- return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
- }
- }
- /**
- * Delete a product.
- *
- * @since 2.2
- * @param int $id the product ID.
- * @param bool $force true to permanently delete order, false to move to trash.
- * @return array
- */
- public function delete_product( $id, $force = false ) {
- $id = $this->validate_request( $id, 'product', 'delete' );
- if ( is_wp_error( $id ) ) {
- return $id;
- }
- do_action( 'woocommerce_api_delete_product', $id, $this );
- $parent_id = wp_get_post_parent_id( $id );
- $result = ( $force ) ? wp_delete_post( $id, true ) : wp_trash_post( $id );
- if ( ! $result ) {
- return new WP_Error( 'woocommerce_api_cannot_delete_product', sprintf( __( 'This %s cannot be deleted', 'woocommerce' ), 'product' ), array( 'status' => 500 ) );
- }
- // Delete parent product transients.
- if ( $parent_id ) {
- wc_delete_product_transients( $parent_id );
- }
- if ( $force ) {
- return array( 'message' => sprintf( __( 'Permanently deleted %s', 'woocommerce' ), 'product' ) );
- } else {
- $this->server->send_status( '202' );
- return array( 'message' => sprintf( __( 'Deleted %s', 'woocommerce' ), 'product' ) );
- }
- }
- /**
- * Get the reviews for a product
- *
- * @since 2.1
- * @param int $id the product ID to get reviews for
- * @param string $fields fields to include in response
- * @return array
- */
- public function get_product_reviews( $id, $fields = null ) {
- $id = $this->validate_request( $id, 'product', 'read' );
- if ( is_wp_error( $id ) ) {
- return $id;
- }
- $comments = get_approved_comments( $id );
- $reviews = array();
- foreach ( $comments as $comment ) {
- $reviews[] = array(
- 'id' => intval( $comment->comment_ID ),
- 'created_at' => $this->server->format_datetime( $comment->comment_date_gmt ),
- 'review' => $comment->comment_content,
- 'rating' => get_comment_meta( $comment->comment_ID, 'rating', true ),
- 'reviewer_name' => $comment->comment_author,
- 'reviewer_email' => $comment->comment_author_email,
- 'verified' => wc_review_is_from_verified_owner( $comment->comment_ID ),
- );
- }
- return array( 'product_reviews' => apply_filters( 'woocommerce_api_product_reviews_response', $reviews, $id, $fields, $comments, $this->server ) );
- }
- /**
- * Get the orders for a product
- *
- * @since 2.4.0
- * @param int $id the product ID to get orders for
- * @param string fields fields to retrieve
- * @param string $filter filters to include in response
- * @param string $status the order status to retrieve
- * @param $page $page page to retrieve
- * @return array
- */
- public function get_product_orders( $id, $fields = null, $filter = array(), $status = null, $page = 1 ) {
- global $wpdb;
- $id = $this->validate_request( $id, 'product', 'read' );
- if ( is_wp_error( $id ) ) {
- return $id;
- }
- $order_ids = $wpdb->get_col( $wpdb->prepare( "
- SELECT order_id
- FROM {$wpdb->prefix}woocommerce_order_items
- WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key = '_product_id' AND meta_value = %d )
- AND order_item_type = 'line_item'
- ", $id ) );
- if ( empty( $order_ids ) ) {
- return array( 'orders' => array() );
- }
- $filter = array_merge( $filter, array(
- 'in' => implode( ',', $order_ids )
- ) );
- $orders = WC()->api->WC_API_Orders->get_orders( $fields, $filter, $status, $page );
- return array( 'orders' => apply_filters( 'woocommerce_api_product_orders_response', $orders['orders'], $id, $filter, $fields, $this->server ) );
- }
- /**
- * Get a listing of product categories
- *
- * @since 2.2
- * @param string|null $fields fields to limit response to
- * @return array
- */
- public function get_product_categories( $fields = null ) {
- try {
- // Permissions check
- if ( ! current_user_can( 'manage_product_terms' ) ) {
- throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_product_categories', __( 'You do not have permission to read product categories', 'woocommerce' ), 401 );
- }
- $product_categories = array();
- $terms = get_terms( 'product_cat', array( 'hide_empty' => false, 'fields' => 'ids' ) );
- foreach ( $terms as $term_id ) {
- $product_categories[] = current( $this->get_product_category( $term_id, $fields ) );
- }
- return array( 'product_categories' => apply_filters( 'woocommerce_api_product_categories_response', $product_categories, $terms, $fields, $this ) );
- } catch ( WC_API_Exception $e ) {
- return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
- }
- }
- /**
- * Get the product category for the given ID
- *
- * @since 2.2
- * @param string $id product category term ID
- * @param string|null $fields fields to limit response to
- * @return array
- */
- public function get_product_category( $id, $fields = null ) {
- try {
- $id = absint( $id );
- // Validate ID
- if ( empty( $id ) ) {
- throw new WC_API_Exception( 'woocommerce_api_invalid_product_category_id', __( 'Invalid product category ID', 'woocommerce' ), 400 );
- }
- // Permissions check
- if ( ! current_user_can( 'manage_product_terms' ) ) {
- throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_product_categories', __( 'You do not have permission to read product categories', 'woocommerce' ), 401 );
- }
- $term = get_term( $id, 'product_cat' );
- if ( is_wp_error( $term ) || is_null( $term ) ) {
- throw new WC_API_Exception( 'woocommerce_api_invalid_product_category_id', __( 'A product category with the provided ID could not be found', 'woocommerce' ), 404 );
- }
- $term_id = intval( $term->term_id );
- // Get category display type
- $display_type = get_woocommerce_term_meta( $term_id, 'display_type' );
- // Get category image
- $image = '';
- if ( $image_id = get_woocommerce_term_meta( $term_id, 'thumbnail_id' ) ) {
- $image = wp_get_attachment_url( $image_id );
- }
- $product_category = array(
- 'id' => $term_id,
- 'name' => $term->name,
- 'slug' => $term->slug,
- 'parent' => $term->parent,
- 'description' => $term->description,
- 'display' => $display_type ? $display_type : 'default',
- 'image' => $image ? esc_url( $image ) : '',
- 'count' => intval( $term->count )
- );
- return array( 'product_category' => apply_filters( 'woocommerce_api_product_category_response', $product_category, $id, $fields, $term, $this ) );
- } catch ( WC_API_Exception $e ) {
- return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
- }
- }
- /**
- * Helper method to get product post objects
- *
- * @since 2.1
- * @param array $args request arguments for filtering query
- * @return WP_Query
- */
- private function query_products( $args ) {
- // Set base query arguments
- $query_args = array(
- 'fields' => 'ids',
- 'post_type' => 'product',
- 'post_status' => 'publish',
- 'meta_query' => array(),
- );
- if ( ! empty( $args['type'] ) ) {
- $types = explode( ',', $args['type'] );
- $query_args['tax_query'] = array(
- array(
- 'taxonomy' => 'product_type',
- 'field' => 'slug',
- 'terms' => $types,
- ),
- );
- unset( $args['type'] );
- }
- // Filter products by category
- if ( ! empty( $args['category'] ) ) {
- $query_args['product_cat'] = $args['category'];
- }
- // Filter by specific sku
- if ( ! empty( $args['sku'] ) ) {
- if ( ! is_array( $query_args['meta_query'] ) ) {
- $query_args['meta_query'] = array();
- }
- $query_args['meta_query'][] = array(
- 'key' => '_sku',
- 'value' => $args['sku'],
- 'compare' => '='
- );
- $query_args['post_type'] = array( 'product', 'product_variation' );
- }
- $query_args = $this->merge_query_args( $query_args, $args );
- return new WP_Query( $query_args );
- }
- /**
- * Get standard product data that applies to every product type
- *
- * @since 2.1
- * @param WC_Product $product
- * @return WC_Product
- */
- private function get_product_data( $product ) {
- $prices_precision = wc_get_price_decimals();
- return array(
- 'title' => $product->get_title(),
- 'id' => (int) $product->is_type( 'variation' ) ? $product->get_variation_id() : $product->id,
- '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 ),
- '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(),
- 'price' => wc_format_decimal( $product->get_price(), $prices_precision ),
- 'regular_price' => wc_format_decimal( $product->get_regular_price(), $prices_precision ),
- 'sale_price' => $product->get_sale_price() ? wc_format_decimal( $product->get_sale_price(), $prices_precision ) : null,
- '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(),
- 'stock_quantity' => (int) $product->get_stock_quantity(),
- '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(),
- 'product_url' => $product->is_type( 'external' ) ? $product->get_product_url() : '',
- 'button_text' => $product->is_type( 'external' ) ? $product->get_button_text() : '',
- 'weight' => $product->get_weight() ? wc_format_decimal( $product->get_weight(), 2 ) : null,
- '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,
- 'description' => wpautop( do_shortcode( $product->get_post_data()->post_content ) ),
- 'short_description' => apply_filters( 'woocommerce_short_description', $product->get_post_data()->post_excerpt ),
- 'reviews_allowed' => ( 'open' === $product->get_post_data()->comment_status ),
- 'average_rating' => wc_format_decimal( $product->get_average_rating(), 2 ),
- '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() ),
- 'parent_id' => $product->post->post_parent,
- '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 ),
- 'featured_src' => (string) wp_get_attachment_url( get_post_thumbnail_id( $product->is_type( 'variation' ) ? $product->variation_id : $product->id ) ),
- 'attributes' => $this->get_attributes( $product ),
- 'downloads' => $this->get_downloads( $product ),
- 'download_limit' => (int) $product->download_limit,
- 'download_expiry' => (int) $product->download_expiry,
- 'download_type' => $product->download_type,
- 'purchase_note' => wpautop( do_shortcode( wp_kses_post( $product->purchase_note ) ) ),
- 'total_sales' => metadata_exists( 'post', $product->id, 'total_sales' ) ? (int) get_post_meta( $product->id, 'total_sales', true ) : 0,
- '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 ) {
- $prices_precision = wc_get_price_decimals();
- $variations = array();
- foreach ( $product->get_children() as $child_id ) {
- $variation = $product->get_child( $child_id );
- if ( ! $variation->exists() ) {
- continue;
- }
- $variations[] = array(
- 'id' => $variation->get_variation_id(),
- 'created_at' => $this->server->format_datetime( $variation->get_post_data()->post_date_gmt ),
- 'updated_at' => $this->server->format_datetime( $variation->get_post_data()->post_modified_gmt ),
- 'downloadable' => $variation->is_downloadable(),
- 'virtual' => $variation->is_virtual(),
- 'permalink' => $variation->get_permalink(),
- 'sku' => $variation->get_sku(),
- 'price' => wc_format_decimal( $variation->get_price(), $prices_precision ),
- 'regular_price' => wc_format_decimal( $variation->get_regular_price(), $prices_precision ),
- 'sale_price' => $variation->get_sale_price() ? wc_format_decimal( $variation->get_sale_price(), $prices_precision ) : null,
- 'taxable' => $variation->is_taxable(),
- 'tax_status' => $variation->get_tax_status(),
- 'tax_class' => $variation->get_tax_class(),
- 'managing_stock' => $variation->managing_stock(),
- 'stock_quantity' => (int) $variation->get_stock_quantity(),
- '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(),
- 'weight' => $variation->get_weight() ? wc_format_decimal( $variation->get_weight(), 2 ) : null,
- 'dimensions' => array(
- 'length' => $variation->length,
- 'width' => $variation->width,
- 'height' => $variation->height,
- 'unit' => get_option( 'woocommerce_dimension_unit' ),
- ),
- 'shipping_class' => $variation->get_shipping_class(),
- 'shipping_class_id' => ( 0 !== $variation->get_shipping_class_id() ) ? $variation->get_shipping_class_id() : null,
- 'image' => $this->get_images( $variation ),
- 'attributes' => $this->get_attributes( $variation ),
- 'downloads' => $this->get_downloads( $variation ),
- 'download_limit' => (int) $product->download_limit,
- 'download_expiry' => (int) $product->download_expiry,
- );
- }
- return $variations;
- }
- /**
- * Save product meta
- *
- * @since 2.2
- * @param int $product_id
- * @param array $data
- * @return bool
- * @throws WC_API_Exception
- */
- protected function save_product_meta( $product_id, $data ) {
- global $wpdb;
- // Product Type
- $product_type = null;
- if ( isset( $data['type'] ) ) {
- $product_type = wc_clean( $data['type'] );
- wp_set_object_terms( $product_id, $product_type, 'product_type' );
- } else {
- $_product_type = get_the_terms( $product_id, 'product_type' );
- if ( is_array( $_product_type ) ) {
- $_product_type = current( $_product_type );
- $product_type = $_product_type->slug;
- }
- }
- // Virtual
- if ( isset( $data['virtual'] ) ) {
- update_post_meta( $product_id, '_virtual', ( true === $data['virtual'] ) ? 'yes' : 'no' );
- }
- // Tax status
- if ( isset( $data['tax_status'] ) ) {
- update_post_meta( $product_id, '_tax_status', wc_clean( $data['tax_status'] ) );
- }
- // Tax Class
- if ( isset( $data['tax_class'] ) ) {
- update_post_meta( $product_id, '_tax_class', wc_clean( $data['tax_class'] ) );
- }
- // Catalog Visibility
- if ( isset( $data['catalog_visibility'] ) ) {
- update_post_meta( $product_id, '_visibility', wc_clean( $data['catalog_visibility'] ) );
- }
- // Purchase Note
- if ( isset( $data['purchase_note'] ) ) {
- update_post_meta( $product_id, '_purchase_note', wc_clean( $data['purchase_note'] ) );
- }
- // Featured Product
- if ( isset( $data['featured'] ) ) {
- update_post_meta( $product_id, '_featured', ( true === $data['featured'] ) ? 'yes' : 'no' );
- }
- // Shipping data
- $this->save_product_shipping_data( $product_id, $data );
- // SKU
- if ( isset( $data['sku'] ) ) {
- $sku = get_post_meta( $product_id, '_sku', true );
- $new_sku = wc_clean( $data['sku'] );
- if ( '' == $new_sku ) {
- update_post_meta( $product_id, '_sku', '' );
- } elseif ( $new_sku !== $sku ) {
- if ( ! empty( $new_sku ) ) {
- $unique_sku = wc_product_has_unique_sku( $product_id, $new_sku );
- if ( ! $unique_sku ) {
- throw new WC_API_Exception( 'woocommerce_api_product_sku_already_exists', __( 'The SKU already exists on another product', 'woocommerce' ), 400 );
- } else {
- update_post_meta( $product_id, '_sku', $new_sku );
- }
- } else {
- update_post_meta( $product_id, '_sku', '' );
- }
- }
- }
- // Attributes
- if ( isset( $data['attributes'] ) ) {
- $attributes = array();
- foreach ( $data['attributes'] as $attribute ) {
- $is_taxonomy = 0;
- $taxonomy = 0;
- if ( ! isset( $attribute['name'] ) ) {
- continue;
- }
- $attribute_slug = sanitize_title( $attribute['name'] );
- if ( isset( $attribute['slug'] ) ) {
- $taxonomy = $this->get_attribute_taxonomy_by_slug( $attribute['slug'] );
- $attribute_slug = sanitize_title( $attribute['slug'] );
- }
- if ( $taxonomy ) {
- $is_taxonomy = 1;
- }
- if ( $is_taxonomy ) {
- if ( isset( $attribute['options'] ) ) {
- $options = $attribute['options'];
- if ( ! is_array( $attribute['options'] ) ) {
- // Text based attributes - Posted values are term names
- $options = explode( WC_DELIMITER, $options );
- }
- $values = array_map( 'wc_sanitize_term_text_based', $options );
- $values = array_filter( $values, 'strlen' );
- } else {
- $values = array();
- }
- // Update post terms
- if ( taxonomy_exists( $taxonomy ) ) {
- wp_set_object_terms( $product_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['visible'] ) && $attribute['visible'] ) ? 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[ $attribute_slug ] = array(
- 'name' => wc_clean( $attribute['name'] ),
- 'value' => $values,
- 'position' => isset( $attribute['position'] ) ? absint( $attribute['position'] ) : 0,
- 'is_visible' => ( isset( $attribute['visible'] ) && $attribute['visible'] ) ? 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 ) {
- if ( $a['position'] == $b['position'] ) {
- return 0;
- }
- return ( $a['position'] < $b['position'] ) ? -1 : 1;
- }
- }
- uasort( $attributes, 'attributes_cmp' );
- update_post_meta( $product_id, '_product_attributes', $attributes );
- }
- // Sales and prices
- if ( in_array( $product_type, array( 'variable', 'grouped' ) ) ) {
- // Variable and grouped products have no prices
- update_post_meta( $product_id, '_regular_price', '' );
- update_post_meta( $product_id, '_sale_price', '' );
- update_post_meta( $product_id, '_sale_price_dates_from', '' );
- update_post_meta( $product_id, '_sale_price_dates_to', '' );
- update_post_meta( $product_id, '_price', '' );
- } else {
- // Regular Price
- if ( isset( $data['regular_price'] ) ) {
- $regular_price = ( '' === $data['regular_price'] ) ? '' : wc_format_decimal( $data['regular_price'] );
- update_post_meta( $product_id, '_regular_price', $regular_price );
- } else {
- $regular_price = get_post_meta( $product_id, '_regular_price', true );
- }
- // Sale Price
- if ( isset( $data['sale_price'] ) ) {
- $sale_price = ( '' === $data['sale_price'] ) ? '' : wc_format_decimal( $data['sale_price'] );
- update_post_meta( $product_id, '_sale_price', $sale_price );
- } else {
- $sale_price = get_post_meta( $product_id, '_sale_price', true );
- }
- $date_from = isset( $data['sale_price_dates_from'] ) ? strtotime( $data['sale_price_dates_from'] ) : get_post_meta( $product_id, '_sale_price_dates_from', true );
- $date_to = isset( $data['sale_price_dates_to'] ) ? strtotime( $data['sale_price_dates_to'] ) : get_post_meta( $product_id, '_sale_price_dates_to', true );
- // Dates
- if ( $date_from ) {
- update_post_meta( $product_id, '_sale_price_dates_from', $date_from );
- } else {
- update_post_meta( $product_id, '_sale_price_dates_from', '' );
- }
- if ( $date_to ) {
- update_post_meta( $product_id, '_sale_price_dates_to', $date_to );
- } else {
- update_post_meta( $product_id, '_sale_price_dates_to', '' );
- }
- if ( $date_to && ! $date_from ) {
- $date_from = strtotime( 'NOW', current_time( 'timestamp' ) );
- update_post_meta( $product_id, '_sale_price_dates_from', $date_from );
- }
- // Update price if on sale
- if ( '' !== $sale_price && '' == $date_to && '' == $date_from ) {
- update_post_meta( $product_id, '_price', wc_format_decimal( $sale_price ) );
- } else {
- update_post_meta( $product_id, '_price', $regular_price );
- }
- if ( '' !== $sale_price && $date_from && $date_from <= strtotime( 'NOW', current_time( 'timestamp' ) ) ) {
- update_post_meta( $product_id, '_price', wc_format_decimal( $sale_price ) );
- }
- if ( $date_to && $date_to < strtotime( 'NOW', current_time( 'timestamp' ) ) ) {
- update_post_meta( $product_id, '_price', $regular_price );
- update_post_meta( $product_id, '_sale_price_dates_from', '' );
- update_post_meta( $product_id, '_sale_price_dates_to', '' );
- }
- }
- // Product parent ID for groups
- if ( isset( $data['parent_id'] ) ) {
- wp_update_post( array( 'ID' => $product_id, 'post_parent' => absint( $data['parent_id'] ) ) );
- }
- // Update parent if grouped so price sorting works and stays in sync with the cheapest child
- $_product = wc_get_product( $product_id );
- 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[] = $product_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 );
- }
- }
- }
- }
- }
- // Sold Individually
- if ( isset( $data['sold_individually'] ) ) {
- update_post_meta( $product_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( $product_id, '_stock_status', true );
- if ( '' === $stock_status ) {
- $stock_status = 'instock';
- }
- }
- // 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( $product_id, '_manage_stock', $managing_stock );
- } else {
- $managing_stock = get_post_meta( $product_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( $product_id, '_backorders', $backorders );
- } else {
- $backorders = get_post_meta( $product_id, '_backorders', true );
- }
- if ( 'grouped' == $product_type ) {
- update_post_meta( $product_id, '_manage_stock', 'no' );
- update_post_meta( $product_id, '_backorders', 'no' );
- update_post_meta( $product_id, '_stock', '' );
- wc_update_product_stock_status( $product_id, $stock_status );
- } elseif ( 'external' == $product_type ) {
- update_post_meta( $product_id, '_manage_stock', 'no' );
- update_post_meta( $product_id, '_backorders', 'no' );
- update_post_meta( $product_id, '_stock', '' );
- wc_update_product_stock_status( $product_id, 'instock' );
- } elseif ( 'yes' == $managing_stock ) {
- update_post_meta( $product_id, '_backorders', $backorders );
- wc_update_product_stock_status( $product_id, $stock_status );
- // Stock quantity
- if ( isset( $data['stock_quantity'] ) ) {
- wc_update_product_stock( $product_id, intval( $data['stock_quantity'] ) );
- }
- } else {
- // Don't manage stock
- update_post_meta( $product_id, '_manage_stock', 'no' );
- update_post_meta( $product_id, '_backorders', $backorders );
- update_post_meta( $product_id, '_stock', '' );
- wc_update_product_stock_status( $product_id, $stock_status );
- }
- } else {
- wc_update_product_stock_status( $product_id, $stock_status );
- }
- // 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( $product_id, '_upsell_ids', $upsells );
- } else {
- delete_post_meta( $product_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( $product_id, '_crosssell_ids', $crosssells );
- } else {
- delete_post_meta( $product_id, '_crosssell_ids' );
- }
- }
- // Product categories
- if ( isset( $data['categories'] ) && is_array( $data['categories'] ) ) {
- $term_ids = array_unique( array_map( 'intval', $data['categories'] ) );
- wp_set_object_terms( $product_id, $term_ids, 'product_cat' );
- }
- // Product tags
- if ( isset( $data['tags'] ) && is_array( $data['tags'] ) ) {
- $term_ids = array_unique( array_map( 'intval', $data['tags'] ) );
- wp_set_object_terms( $product_id, $term_ids, 'product_tag' );
- }
- // Downloadable
- if ( isset( $data['downloadable'] ) ) {
- $is_downloadable = ( true === $data['downloadable'] ) ? 'yes' : 'no';
- update_post_meta( $product_id, '_downloadable', $is_downloadable );
- } else {
- $is_downloadable = get_post_meta( $product_id, '_downloadable', true );
- }
- // Downloadable options
- if ( 'yes' == $is_downloadable ) {
- // Downloadable files
- if ( isset( $data['downloads'] ) && is_array( $data['downloads'] ) ) {
- $this->save_downloadable_files( $product_id, $data['downloads'] );
- }
- // Download limit
- if ( isset( $data['download_limit'] ) ) {
- update_post_meta( $product_id, '_download_limit', ( '' === $data['download_limit'] ) ? '' : absint( $data['download_limit'] ) );
- }
- // Download expiry
- if ( isset( $data['download_expiry'] ) ) {
- update_post_meta( $product_id, '_download_expiry', ( '' === $data['download_expiry'] ) ? '' : absint( $data['download_expiry'] ) );
- }
- // Download type
- if ( isset( $data['download_type'] ) ) {
- update_post_meta( $product_id, '_download_type', wc_clean( $data['download_type'] ) );
- }
- }
- // Product url
- if ( $product_type == 'external' ) {
- if ( isset( $data['product_url'] ) ) {
- update_post_meta( $product_id, '_product_url', wc_clean( $data['product_url'] ) );
- }
- if ( isset( $data['button_text'] ) ) {
- update_post_meta( $product_id, '_button_text', wc_clean( $data['button_text'] ) );
- }
- }
- // Reviews allowed
- if ( isset( $data['reviews_allowed'] ) ) {
- $reviews_allowed = ( true === $data['reviews_allowed'] ) ? 'open' : 'closed';
- $wpdb->update( $wpdb->posts, array( 'comment_status' => $reviews_allowed ), array( 'ID' => $product_id ) );
- }
- // Do action for product type
- do_action( 'woocommerce_api_process_product_meta_' . $product_type, $product_id, $data );
- return true;
- }
- /**
- * Save variations
- *
- * @since 2.2
- * @param int $id
- * @param array $data
- * @return bool
- * @throws WC_API_Exception
- */
- 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';
- $new_variation = array(
- '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
- );
- $variation_id = wp_insert_post( $new_variation );
- 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 ) ) {
- throw new WC_API_Exception( 'woocommerce_api_cannot_save_product_variation', $variation_id->get_error_message(), 400 );
- }
- // SKU
- if ( isset( $variation['sku'] ) ) {
- $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 ) ) {
- $unique_sku = wc_product_has_unique_sku( $variation_id, $new_sku );
- if ( ! $unique_sku ) {
- throw new WC_API_Exception( 'woocommerce_api_product_sku_already_exists', __( 'The SKU already exists on another product', 'woocommerce' ), 400 );
- } else {
- update_post_meta( $variation_id, '_sku', $new_sku );
- }
- } else {
- update_post_meta( $variation_id, '_sku', '' );
- }
- }
- }
- // 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 ) ) {
- throw new WC_API_Exception( 'woocommerce_api_cannot_upload_product_image', $upload->get_error_message(), 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['managing_stock'] ) ) {
- $managing_stock = ( true === $variation['managing_stock'] ) ? 'yes' : 'no';
- update_post_meta( $variation_id, '_manage_stock', $managing_stock );
- } else {
- $managing_stock = get_post_meta( $variation_id, '_manage_stock', true );
- }
- // Only update stock status to user setting if changed by the user, but do so before looking at stock levels at variation level
- if ( isset( $variation['in_stock'] ) ) {
- $stock_status = ( true === $variation['in_stock'] ) ? 'instock' : 'outofstock';
- wc_update_product_stock_status( $variation_id, $stock_status );
- }
- if ( 'yes' === $managing_stock ) {
- $backorders = get_post_meta( $variation_id, '_backorders', true );
- if ( isset( $variation['backorders'] ) ) {
- if ( 'notify' == $variation['backorders'] ) {
- $backorders = 'notify';
- } else {
- $backorders = ( true === $variation['backorders'] ) ? 'yes' : 'no';
- }
- }
- update_post_meta( $variation_id, '_backorders', '' === $backorders ? 'no' : $backorders );
- if ( isset( $variation['stock_quantity'] ) ) {
- wc_update_product_stock( $variation_id, wc_stock_amount( $variation['stock_quantity'] ) );
- }
- } else {
- delete_post_meta( $variation_id, '_backorders' );
- delete_post_meta( $variation_id, '_stock' );
- }
- // 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'] ) ? strtotime( $variation['sale_price_dates_from'] ) : get_post_meta( $variation_id, '_sale_price_dates_from', true );
- $date_to = isset( $variation['sale_price_dates_to'] ) ? strtotime( $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', $date_from );
- } else {
- update_post_meta( $variation_id, '_sale_price_dates_from', '' );
- }
- if ( $date_to ) {
- update_post_meta( $variation_id, '_sale_price_dates_to', $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 && $date_from < strtotime( 'NOW', current_time( 'timestamp' ) ) ) {
- update_post_meta( $variation_id, '_price', $sale_price );
- }
- if ( $date_to && $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