/** * Save product meta * * @since 2.2 * @param WC_Product $product * @param array $data * @return WC_Product * @throws WC_API_Exception */ protected function save_product_meta($product, $data) { global $wpdb; // Virtual if (isset($data['virtual'])) { $product->set_virtual($data['virtual']); } // Tax status if (isset($data['tax_status'])) { $product->set_tax_status(wc_clean($data['tax_status'])); } // Tax Class if (isset($data['tax_class'])) { $product->set_tax_class(wc_clean($data['tax_class'])); } // Catalog Visibility if (isset($data['catalog_visibility'])) { $product->set_catalog_visibility(wc_clean($data['catalog_visibility'])); } // Purchase Note if (isset($data['purchase_note'])) { $product->set_purchase_note(wc_clean($data['purchase_note'])); } // Featured Product if (isset($data['featured'])) { $product->set_featured($data['featured']); } // Shipping data $product = $this->save_product_shipping_data($product, $data); // SKU if (isset($data['sku'])) { $sku = $product->get_sku(); $new_sku = wc_clean($data['sku']); if ('' == $new_sku) { $product->set_sku(''); } elseif ($new_sku !== $sku) { if (!empty($new_sku)) { $unique_sku = wc_product_has_unique_sku($product->get_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 { $product->set_sku($new_sku); } } else { $product->set_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) { $attribute_id = wc_attribute_taxonomy_id_by_name($attribute['name']); 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->get_id(), $values, $taxonomy); } if (!empty($values)) { // Add attribute to array, but don't set values. $attribute_object = new WC_Product_Attribute(); $attribute_object->set_id($attribute_id); $attribute_object->set_name($taxonomy); $attribute_object->set_options($values); $attribute_object->set_position(isset($attribute['position']) ? 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 = $attribute['options']; // Text based, separate by pipe } else { $values = array_map('wc_clean', explode(WC_DELIMITER, $attribute['options'])); } // Custom attribute - Add attribute to array and set the values. $attribute_object = new WC_Product_Attribute(); $attribute_object->set_name($attribute['name']); $attribute_object->set_options($values); $attribute_object->set_position(isset($attribute['position']) ? 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'); $product->set_attributes($attributes); } // Sales and prices if (in_array($product->get_type(), array('variable', 'grouped'))) { // Variable and grouped products have no prices. $product->set_regular_price(''); $product->set_sale_price(''); $product->set_date_on_sale_to(''); $product->set_date_on_sale_from(''); $product->set_price(''); } else { // Regular Price if (isset($data['regular_price'])) { $regular_price = '' === $data['regular_price'] ? '' : $data['regular_price']; } else { $regular_price = $product->get_regular_price(); } // Sale Price if (isset($data['sale_price'])) { $sale_price = '' === $data['sale_price'] ? '' : $data['sale_price']; } else { $sale_price = $product->get_sale_price(); } $product->set_regular_price($regular_price); $product->set_sale_price($sale_price); if (isset($data['sale_price_dates_from'])) { $date_from = $data['sale_price_dates_from']; } else { $date_from = $product->get_date_on_sale_from() ? date('Y-m-d', $date_from) : ''; } if (isset($data['sale_price_dates_to'])) { $date_to = $data['sale_price_dates_to']; } else { $date_to = $product->get_date_on_sale_to() ? date('Y-m-d', $date_to) : ''; } if ($date_to && !$date_from) { $date_from = strtotime('NOW', current_time('timestamp')); } $product->set_date_on_sale_to($date_to); $product->set_date_on_sale_from($date_from); if ($product->is_on_sale()) { $product->set_price($product->get_sale_price()); } else { $product->set_price($product->get_regular_price()); } } // Product parent ID for groups if (isset($data['parent_id'])) { $product->set_parent_id(absint($data['parent_id'])); } // Sold Individually if (isset($data['sold_individually'])) { $product->set_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 = $product->get_stock_status(); 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'; $product->set_manage_stock($managing_stock); } else { $managing_stock = $product->get_manage_stock() ? 'yes' : 'no'; } // Backorders if (isset($data['backorders'])) { if ('notify' == $data['backorders']) { $backorders = 'notify'; } else { $backorders = true === $data['backorders'] ? 'yes' : 'no'; } $product->set_backorders($backorders); } else { $backorders = $product->get_backorders(); } if ($product->is_type('grouped')) { $product->set_manage_stock('no'); $product->set_backorders('no'); $product->set_stock_quantity(''); $product->set_stock_status($stock_status); } elseif ($product->is_type('external')) { $product->set_manage_stock('no'); $product->set_backorders('no'); $product->set_stock_quantity(''); $product->set_stock_status('instock'); } elseif ('yes' == $managing_stock) { $product->set_backorders($backorders); // Stock status is always determined by children so sync later. if (!$product->is_type('variable')) { $product->set_stock_status($stock_status); } // Stock quantity if (isset($data['stock_quantity'])) { $product->set_stock_quantity(wc_stock_amount($data['stock_quantity'])); } } else { // Don't manage stock. $product->set_manage_stock('no'); $product->set_backorders($backorders); $product->set_stock_quantity(''); $product->set_stock_status($stock_status); } } elseif (!$product->is_type('variable')) { $product->set_stock_status($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; } } $product->set_upsell_ids($upsells); } else { $product->set_upsell_ids(array()); } } // 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; } } $product->set_cross_sell_ids($crosssells); } else { $product->set_cross_sell_ids(array()); } } // Product categories if (isset($data['categories']) && is_array($data['categories'])) { $term_ids = array_unique(array_map('intval', $data['categories'])); $product->set_category_ids($term_ids); } // Product tags if (isset($data['tags']) && is_array($data['tags'])) { $term_ids = array_unique(array_map('intval', $data['tags'])); $product->set_tag_ids($term_ids); } // Downloadable if (isset($data['downloadable'])) { $is_downloadable = true === $data['downloadable'] ? 'yes' : 'no'; $product->set_downloadable($is_downloadable); } else { $is_downloadable = $product->get_downloadable() ? 'yes' : 'no'; } // Downloadable options if ('yes' == $is_downloadable) { // Downloadable files if (isset($data['downloads']) && is_array($data['downloads'])) { $product = $this->save_downloadable_files($product, $data['downloads']); } // Download limit if (isset($data['download_limit'])) { $product->set_download_limit($data['download_limit']); } // Download expiry if (isset($data['download_expiry'])) { $product->set_download_expiry($data['download_expiry']); } } // Product url if ($product->is_type('external')) { if (isset($data['product_url'])) { $product->set_product_url($data['product_url']); } if (isset($data['button_text'])) { $product->set_button_text($data['button_text']); } } // Reviews allowed if (isset($data['reviews_allowed'])) { $product->set_reviews_allowed($data['reviews_allowed']); } // Save default attributes for variable products. if ($product->is_type('variable')) { $product = $this->save_default_attributes($product, $data); } // Do action for product type do_action('woocommerce_api_process_product_meta_' . $product->get_type(), $product->get_id(), $data); return $product; }
/** * Save product meta. * * @throws WC_REST_Exception REST API exceptions. * @param WC_Product $product Product instance. * @param WP_REST_Request $request Request data. * @return WC_Product */ protected function save_product_meta($product, $request) { global $wpdb; // Virtual. if (isset($request['virtual'])) { $product->set_virtual($request['virtual']); } // Tax status. if (isset($request['tax_status'])) { $product->set_tax_status($request['tax_status']); } // Tax Class. if (isset($request['tax_class'])) { $product->set_tax_class($request['tax_class']); } // Catalog Visibility. if (isset($request['catalog_visibility'])) { $product->set_catalog_visibility($request['catalog_visibility']); } // Purchase Note. if (isset($request['purchase_note'])) { $product->set_purchase_note(wc_clean($request['purchase_note'])); } // Featured Product. if (isset($request['featured'])) { $product->set_featured($request['featured']); } // Shipping data. $product = $this->save_product_shipping_data($product, $request); // SKU. if (isset($request['sku'])) { $product->set_sku(wc_clean($request['sku'])); } // Attributes. if (isset($request['attributes'])) { $attributes = array(); foreach ($request['attributes'] as $attribute) { $attribute_id = 0; $attribute_name = ''; // Check ID for global attributes or name for product attributes. if (!empty($attribute['id'])) { $attribute_id = absint($attribute['id']); $attribute_name = wc_attribute_taxonomy_name_by_id($attribute_id); } elseif (!empty($attribute['name'])) { $attribute_name = wc_clean($attribute['name']); } if (!$attribute_id && !$attribute_name) { continue; } if ($attribute_id) { 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(); } if (!empty($values)) { // Add attribute to array, but don't set values. $attribute_object = new WC_Product_Attribute(); $attribute_object->set_id($attribute_id); $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; } } elseif (isset($attribute['options'])) { // Custom attribute - Add attribute to array and set the values. 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; } } $product->set_attributes($attributes); } // Sales and prices. if (in_array($product->get_type(), array('variable', 'grouped'), true)) { $product->set_regular_price(''); $product->set_sale_price(''); $product->set_date_on_sale_to(''); $product->set_date_on_sale_from(''); $product->set_price(''); } else { // Regular Price. if (isset($request['regular_price'])) { $product->set_regular_price($request['regular_price']); } // Sale Price. if (isset($request['sale_price'])) { $product->set_sale_price($request['sale_price']); } if (isset($request['date_on_sale_from'])) { $product->set_date_on_sale_from($request['date_on_sale_from']); } if (isset($request['date_on_sale_to'])) { $product->set_date_on_sale_to($request['date_on_sale_to']); } } // Product parent ID for groups. if (isset($request['parent_id'])) { $product->set_parent_id($request['parent_id']); } // Sold individually. if (isset($request['sold_individually'])) { $product->set_sold_individually($request['sold_individually']); } // Stock status. if (isset($request['in_stock'])) { $stock_status = true === $request['in_stock'] ? 'instock' : 'outofstock'; } else { $stock_status = $product->get_stock_status(); } // Stock data. if ('yes' === get_option('woocommerce_manage_stock')) { // Manage stock. if (isset($request['manage_stock'])) { $product->set_manage_stock($request['manage_stock']); } // Backorders. if (isset($request['backorders'])) { $product->set_backorders($request['backorders']); } if ($product->is_type('grouped')) { $product->set_manage_stock('no'); $product->set_backorders('no'); $product->set_stock_quantity(''); $product->set_stock_status($status); } elseif ($product->is_type('external')) { $product->set_manage_stock('no'); $product->set_backorders('no'); $product->set_stock_quantity(''); $product->set_stock_status('instock'); } elseif ($product->get_manage_stock()) { // Stock status is always determined by children so sync later. if (!$product->is_type('variable')) { $product->set_stock_status($stock_status); } // Stock quantity. if (isset($request['stock_quantity'])) { $product->set_stock_quantity(wc_stock_amount($request['stock_quantity'])); } elseif (isset($request['inventory_delta'])) { $stock_quantity = wc_stock_amount($product->get_stock_amount()); $stock_quantity += wc_stock_amount($request['inventory_delta']); $product->set_stock_quantity(wc_stock_amount($stock_quantity)); } } else { // Don't manage stock. $product->set_manage_stock('no'); $product->set_stock_quantity(''); $product->set_stock_status($stock_status); } } elseif (!$product->is_type('variable')) { $product->set_stock_status($stock_status); } // Upsells. if (isset($request['upsell_ids'])) { $upsells = array(); $ids = $request['upsell_ids']; if (!empty($ids)) { foreach ($ids as $id) { if ($id && $id > 0) { $upsells[] = $id; } } } $product->set_upsell_ids($upsells); } // Cross sells. if (isset($request['cross_sell_ids'])) { $crosssells = array(); $ids = $request['cross_sell_ids']; if (!empty($ids)) { foreach ($ids as $id) { if ($id && $id > 0) { $crosssells[] = $id; } } } $product->set_cross_sell_ids($crosssells); } // Product categories. if (isset($request['categories']) && is_array($request['categories'])) { $product = $this->save_taxonomy_terms($product, $request['categories']); } // Product tags. if (isset($request['tags']) && is_array($request['tags'])) { $product = $this->save_taxonomy_terms($product, $request['tags'], 'tag'); } // Downloadable. if (isset($request['downloadable'])) { $product->set_downloadable($request['downloadable']); } // Downloadable options. if ($product->get_downloadable()) { // Downloadable files. if (isset($request['downloads']) && is_array($request['downloads'])) { $product = $this->save_downloadable_files($product, $request['downloads']); } // Download limit. if (isset($request['download_limit'])) { $product->set_download_limit($request['download_limit']); } // Download expiry. if (isset($request['download_expiry'])) { $product->set_download_expiry($request['download_expiry']); } } // Product url and button text for external products. if ($product->is_type('external')) { if (isset($request['external_url'])) { $product->set_product_url($request['external_url']); } if (isset($request['button_text'])) { $product->set_button_text($request['button_text']); } } // Save default attributes for variable products. if ($product->is_type('variable')) { $product = $this->save_default_attributes($product, $request); } return $product; }
/** * Bulk edit. * * @param integer $post_id * @param WC_Product $product */ public function bulk_edit_save($post_id, $product) { $old_regular_price = $product->get_regular_price(); $old_sale_price = $product->get_sale_price(); // Save fields if (!empty($_REQUEST['change_weight']) && isset($_REQUEST['_weight'])) { $product->set_weight(wc_clean(stripslashes($_REQUEST['_weight']))); } if (!empty($_REQUEST['change_dimensions'])) { if (isset($_REQUEST['_length'])) { $product->set_length(wc_clean(stripslashes($_REQUEST['_length']))); } if (isset($_REQUEST['_width'])) { $product->set_width(wc_clean(stripslashes($_REQUEST['_width']))); } if (isset($_REQUEST['_height'])) { $product->set_height(wc_clean(stripslashes($_REQUEST['_height']))); } } if (!empty($_REQUEST['_tax_status'])) { $product->set_tax_status(wc_clean($_REQUEST['_tax_status'])); } if (!empty($_REQUEST['_tax_class'])) { $tax_class = wc_clean($_REQUEST['_tax_class']); if ('standard' == $tax_class) { $tax_class = ''; } $product->set_tax_class($tax_class); } if (!empty($_REQUEST['_shipping_class'])) { $shipping_class = '_no_shipping_class' == $_REQUEST['_shipping_class'] ? '' : wc_clean($_REQUEST['_shipping_class']); $shipping_class_id = $data_store->get_shipping_class_id_by_slug($shipping_class); if ($shipping_class_id) { $product->set_shipping_class_id($shipping_class_id); } } if (!empty($_REQUEST['_visibility'])) { $product->set_catalog_visibility(wc_clean($_REQUEST['_visibility'])); } if (!empty($_REQUEST['_featured'])) { $product->set_featured(stripslashes($_REQUEST['_featured'])); } // Sold Individually if (!empty($_REQUEST['_sold_individually'])) { if ('yes' === $_REQUEST['_sold_individually']) { $product->set_sold_individually('yes'); } else { $product->set_sold_individually(''); } } // Handle price - remove dates and set to lowest $change_price_product_types = apply_filters('woocommerce_bulk_edit_save_price_product_types', array('simple', 'external')); $can_product_type_change_price = false; foreach ($change_price_product_types as $product_type) { if ($product->is_type($product_type)) { $can_product_type_change_price = true; break; } } if ($can_product_type_change_price) { $price_changed = false; if (!empty($_REQUEST['change_regular_price'])) { $change_regular_price = absint($_REQUEST['change_regular_price']); $regular_price = esc_attr(stripslashes($_REQUEST['_regular_price'])); switch ($change_regular_price) { case 1: $new_price = $regular_price; break; case 2: if (strstr($regular_price, '%')) { $percent = str_replace('%', '', $regular_price) / 100; $new_price = $old_regular_price + round($old_regular_price * $percent, wc_get_price_decimals()); } else { $new_price = $old_regular_price + $regular_price; } break; case 3: if (strstr($regular_price, '%')) { $percent = str_replace('%', '', $regular_price) / 100; $new_price = max(0, $old_regular_price - round($old_regular_price * $percent, wc_get_price_decimals())); } else { $new_price = max(0, $old_regular_price - $regular_price); } break; default: break; } if (isset($new_price) && $new_price != $old_regular_price) { $price_changed = true; $new_price = round($new_price, wc_get_price_decimals()); $product->set_regular_price($new_price); } } if (!empty($_REQUEST['change_sale_price'])) { $change_sale_price = absint($_REQUEST['change_sale_price']); $sale_price = esc_attr(stripslashes($_REQUEST['_sale_price'])); switch ($change_sale_price) { case 1: $new_price = $sale_price; break; case 2: if (strstr($sale_price, '%')) { $percent = str_replace('%', '', $sale_price) / 100; $new_price = $old_sale_price + $old_sale_price * $percent; } else { $new_price = $old_sale_price + $sale_price; } break; case 3: if (strstr($sale_price, '%')) { $percent = str_replace('%', '', $sale_price) / 100; $new_price = max(0, $old_sale_price - $old_sale_price * $percent); } else { $new_price = max(0, $old_sale_price - $sale_price); } break; case 4: if (strstr($sale_price, '%')) { $percent = str_replace('%', '', $sale_price) / 100; $new_price = max(0, $product->regular_price - $product->regular_price * $percent); } else { $new_price = max(0, $product->regular_price - $sale_price); } break; default: break; } if (isset($new_price) && $new_price != $old_sale_price) { $price_changed = true; $new_price = !empty($new_price) || '0' === $new_price ? round($new_price, wc_get_price_decimals()) : ''; $product->set_sale_price($new_price); } } if ($price_changed) { $product->set_date_on_sale_to(''); $product->set_date_on_sale_from(''); if ($product->get_regular_price() < $product->get_sale_price()) { $product->set_sale_price(''); } } } // Handle Stock Data $was_managing_stock = $product->get_manage_stock() ? 'yes' : 'no'; $stock_status = $product->get_stock_status(); $backorders = $product->get_backorders(); $backorders = !empty($_REQUEST['_backorders']) ? wc_clean($_REQUEST['_backorders']) : $backorders; $stock_status = !empty($_REQUEST['_stock_status']) ? wc_clean($_REQUEST['_stock_status']) : $stock_status; if (!empty($_REQUEST['_manage_stock'])) { $manage_stock = 'yes' === wc_clean($_REQUEST['_manage_stock']) && 'grouped' !== $product->product_type ? 'yes' : 'no'; } else { $manage_stock = $was_managing_stock; } $stock_amount = 'yes' === $manage_stock && isset($_REQUEST['_change_stock']) ? wc_stock_amount($_REQUEST['_change_stock']) : ''; if ('yes' === get_option('woocommerce_manage_stock')) { // Apply product type constraints to stock status if ($product->is_type('external')) { // External always in stock $stock_status = 'instock'; } elseif ($product->is_type('variable')) { // Stock status is always determined by children foreach ($product->get_children() as $child_id) { $child = wc_get_product($child_id); if (!$product->get_manage_stock()) { $child->set_stock_status($stock_status); $child->save(); } } $product = WC_Product_Variable::sync($product, false); } $product->set_manage_stock($manage_stock); $product->set_backorders($backorders); $product->save(); if (!$product->is_type('variable')) { wc_update_product_stock_status($post_id, $stock_status); } wc_update_product_stock($post_id, $stock_amount); } else { $product->save(); wc_update_product_stock_status($post_id, $stock_status); } do_action('woocommerce_product_bulk_edit_save', $product); }