/**
  * Read attributes from post meta.
  *
  * @param WC_Product
  * @since 2.7.0
  */
 protected function read_attributes(&$product)
 {
     $meta_values = maybe_unserialize(get_post_meta($product->get_id(), '_product_attributes', true));
     if ($meta_values) {
         $attributes = array();
         foreach ($meta_values as $meta_value) {
             if (!empty($meta_value['is_taxonomy'])) {
                 if (!taxonomy_exists($meta_value['name'])) {
                     continue;
                 }
                 $options = wc_get_object_terms($product->get_id(), $meta_value['name'], 'term_id');
             } else {
                 $options = wc_get_text_attributes($meta_value['value']);
             }
             $attribute = new WC_Product_Attribute();
             $attribute->set_id(wc_attribute_taxonomy_id_by_name($meta_value['name']));
             $attribute->set_name($meta_value['name']);
             $attribute->set_options($options);
             $attribute->set_position($meta_value['position']);
             $attribute->set_visible($meta_value['is_visible']);
             $attribute->set_variation($meta_value['is_variation']);
             $attributes[] = $attribute;
         }
         $product->set_attributes($attributes);
     }
 }
 /**
  * Get the attributes for a product or product variation.
  *
  * @param WC_Product|WC_Product_Variation $product Product instance.
  * @return array
  */
 protected function get_attributes($product)
 {
     $attributes = array();
     if ($product->is_type('variation')) {
         // Variation attributes.
         foreach ($product->get_variation_attributes() as $attribute_name => $attribute) {
             $name = str_replace('attribute_', '', $attribute_name);
             if (!$attribute) {
                 continue;
             }
             // Taxonomy-based attributes are prefixed with `pa_`, otherwise simply `attribute_`.
             if (0 === strpos($attribute_name, 'attribute_pa_')) {
                 $option_term = get_term_by('slug', $attribute, $name);
                 $attributes[] = array('id' => wc_attribute_taxonomy_id_by_name($name), 'name' => $this->get_attribute_taxonomy_label($name), 'option' => $option_term && !is_wp_error($option_term) ? $option_term->name : $attribute);
             } else {
                 $attributes[] = array('id' => 0, 'name' => $name, 'option' => $attribute);
             }
         }
     } else {
         foreach ($product->get_attributes() as $attribute) {
             if ($attribute['is_taxonomy']) {
                 $attributes[] = array('id' => wc_attribute_taxonomy_id_by_name($attribute['name']), 'name' => $this->get_attribute_taxonomy_label($attribute['name']), 'position' => (int) $attribute['position'], 'visible' => (bool) $attribute['is_visible'], 'variation' => (bool) $attribute['is_variation'], 'options' => $this->get_attribute_options($product->get_id(), $attribute));
             } else {
                 $attributes[] = array('id' => 0, 'name' => $attribute['name'], 'position' => (int) $attribute['position'], 'visible' => (bool) $attribute['is_visible'], 'variation' => (bool) $attribute['is_variation'], 'options' => $this->get_attribute_options($product->get_id(), $attribute));
             }
         }
     }
     return $attributes;
 }
 /**
  * Add an attribute row.
  */
 public static function add_attribute()
 {
     ob_start();
     check_ajax_referer('add-attribute', 'security');
     if (!current_user_can('edit_products')) {
         die(-1);
     }
     $i = absint($_POST['i']);
     $metabox_class = array();
     $attribute = new WC_Product_Attribute();
     $attribute->set_id(wc_attribute_taxonomy_id_by_name(sanitize_text_field($_POST['taxonomy'])));
     $attribute->set_name(sanitize_text_field($_POST['taxonomy']));
     $attribute->set_visible(apply_filters('woocommerce_attribute_default_visibility', 1));
     $attribute->set_variation(apply_filters('woocommerce_attribute_default_is_variation', 0));
     if ($attribute->is_taxonomy()) {
         $metabox_class[] = 'taxonomy';
         $metabox_class[] = $attribute->get_name();
     }
     include 'admin/meta-boxes/views/html-product-attribute.php';
     die;
 }
 /**
  * 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;
 }
 /**
  * Prepare attributes for save.
  * @return array
  */
 public static function prepare_attributes($data = false)
 {
     $attributes = array();
     if (!$data) {
         $data = $_POST;
     }
     if (isset($data['attribute_names'], $data['attribute_values'])) {
         $attribute_names = $data['attribute_names'];
         $attribute_values = $data['attribute_values'];
         $attribute_visibility = isset($data['attribute_visibility']) ? $data['attribute_visibility'] : array();
         $attribute_variation = isset($data['attribute_variation']) ? $data['attribute_variation'] : array();
         $attribute_position = $data['attribute_position'];
         $attribute_names_max_key = max(array_keys($attribute_names));
         for ($i = 0; $i <= $attribute_names_max_key; $i++) {
             if (empty($attribute_names[$i]) || !isset($attribute_values[$i])) {
                 continue;
             }
             $attribute_name = wc_clean($attribute_names[$i]);
             $attribute_id = wc_attribute_taxonomy_id_by_name($attribute_name);
             $options = isset($attribute_values[$i]) ? $attribute_values[$i] : '';
             if (is_array($options)) {
                 // Term ids sent as array.
                 $options = wp_parse_id_list($options);
             } else {
                 // Terms or text sent in textarea.
                 $options = 0 < $attribute_id ? wc_sanitize_textarea(wc_sanitize_term_text_based($options)) : wc_sanitize_textarea($options);
                 $options = wc_get_text_attributes($options);
             }
             $attribute = new WC_Product_Attribute();
             $attribute->set_id($attribute_id);
             $attribute->set_name($attribute_name);
             $attribute->set_options($options);
             $attribute->set_position($attribute_position[$i]);
             $attribute->set_visible(isset($attribute_visibility[$i]));
             $attribute->set_variation(isset($attribute_variation[$i]));
             $attributes[] = $attribute;
         }
     }
     return $attributes;
 }
 /**
  * Get the attributes for a product or product variation.
  *
  * @param WC_Product|WC_Product_Variation $product
  * @return array
  */
 protected function get_attributes($product)
 {
     $attributes = array();
     if ($product->is_type('variation')) {
         // Variation attributes.
         foreach ($product->get_variation_attributes() as $attribute_name => $attribute) {
             $name = str_replace('attribute_', '', $attribute_name);
             // Taxonomy-based attributes are prefixed with `pa_`, otherwise simply `attribute_`.
             if (0 === strpos($attribute_name, 'attribute_pa_')) {
                 $attributes[] = array('id' => wc_attribute_taxonomy_id_by_name($name), 'name' => $this->get_attribute_taxonomy_label($name), 'option' => $attribute);
             } else {
                 $attributes[] = array('id' => 0, 'name' => str_replace('pa_', '', $name), 'option' => $attribute);
             }
         }
     } else {
         foreach ($product->get_attributes() as $attribute) {
             // Taxonomy-based attributes are comma-separated, others are pipe (|) separated.
             if ($attribute['is_taxonomy']) {
                 $attributes[] = array('id' => $attribute['is_taxonomy'] ? wc_attribute_taxonomy_id_by_name($attribute['name']) : 0, 'name' => $this->get_attribute_taxonomy_label($attribute['name']), 'position' => (int) $attribute['position'], 'visible' => (bool) $attribute['is_visible'], 'variation' => (bool) $attribute['is_variation'], 'options' => array_map('trim', explode(',', $product->get_attribute($attribute['name']))));
             } else {
                 $attributes[] = array('id' => 0, 'name' => str_replace('pa_', '', $attribute['name']), 'position' => (int) $attribute['position'], 'visible' => (bool) $attribute['is_visible'], 'variation' => (bool) $attribute['is_variation'], 'options' => array_map('trim', explode('|', $product->get_attribute($attribute['name']))));
             }
         }
     }
     return $attributes;
 }