get_name() public method

Get product name.
Since: 2.7.0
public get_name ( string $context = 'view' ) : string
$context string
return string
 /**
  * Get standard product data that applies to every product type
  *
  * @since 2.1
  * @param WC_Product $product
  * @return array
  */
 private function get_product_data($product)
 {
     return array('title' => $product->get_name(), 'id' => $product->get_id(), 'created_at' => $this->server->format_datetime($product->get_date_created(), false, true), 'updated_at' => $this->server->format_datetime($product->get_date_modified(), false, true), 'type' => $product->get_type(), 'status' => $product->get_status(), 'downloadable' => $product->is_downloadable(), 'virtual' => $product->is_virtual(), 'permalink' => $product->get_permalink(), 'sku' => $product->get_sku(), 'price' => wc_format_decimal($product->get_price(), 2), 'regular_price' => wc_format_decimal($product->get_regular_price(), 2), 'sale_price' => $product->get_sale_price() ? wc_format_decimal($product->get_sale_price(), 2) : null, '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' => $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->get_catalog_visibility(), 'on_sale' => $product->is_on_sale(), 'weight' => $product->get_weight() ? wc_format_decimal($product->get_weight(), 2) : null, 'dimensions' => array('length' => $product->get_length(), 'width' => $product->get_width(), 'height' => $product->get_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' => apply_filters('the_content', $product->get_description()), 'short_description' => apply_filters('woocommerce_short_description', $product->get_short_description()), 'reviews_allowed' => $product->get_reviews_allowed(), 'average_rating' => wc_format_decimal($product->get_average_rating(), 2), 'rating_count' => $product->get_rating_count(), 'related_ids' => array_map('absint', array_values(wc_get_related_products($product->get_id()))), 'upsell_ids' => array_map('absint', $product->get_upsell_ids()), 'cross_sell_ids' => array_map('absint', $product->get_cross_sell_ids()), 'categories' => wc_get_object_terms($product->get_id(), 'product_cat', 'name'), 'tags' => wc_get_object_terms($product->get_id(), 'product_tag', 'name'), 'images' => $this->get_images($product), 'featured_src' => wp_get_attachment_url(get_post_thumbnail_id($product->get_id())), 'attributes' => $this->get_attributes($product), 'downloads' => $this->get_downloads($product), 'download_limit' => $product->get_download_limit(), 'download_expiry' => $product->get_download_expiry(), 'download_type' => 'standard', 'purchase_note' => apply_filters('the_content', $product->get_purchase_note()), 'total_sales' => $product->get_total_sales(), 'variations' => array(), 'parent' => array());
 }
Ejemplo n.º 2
1
 /**
  * Test creating a new product.
  *
  * @since 2.7.0
  */
 function test_product_create()
 {
     $product = new WC_Product();
     $product->set_regular_price(42);
     $product->set_name('My Product');
     $product->save();
     $read_product = new WC_Product($product->get_id());
     $this->assertEquals('42', $read_product->get_regular_price());
     $this->assertEquals('My Product', $read_product->get_name());
 }
 /**
  * Save variations
  *
  * @since  2.2
  * @param  WC_Product $product
  * @param  array $request
  * @return WC_Product
  * @throws WC_API_Exception
  */
 protected function save_variations($product, $request)
 {
     global $wpdb;
     $id = $product->get_id();
     $attributes = $product->get_variation_attributes();
     foreach ($request['variations'] as $menu_order => $data) {
         $variation_id = isset($data['id']) ? absint($data['id']) : 0;
         $variation = new WC_Product_Variation($variation_id);
         // Create initial name and status.
         if (!$variation->get_slug()) {
             /* translators: 1: variation id 2: product name */
             $variation->set_name(sprintf(__('Variation #%1$s of %2$s', 'woocommerce'), $variation->get_id(), $product->get_name()));
             $variation->set_status(isset($data['visible']) && false === $data['visible'] ? 'private' : 'publish');
         }
         // Parent ID.
         $variation->set_parent_id($product->get_id());
         // Menu order.
         $variation->set_menu_order($menu_order);
         // Status.
         if (isset($data['visible'])) {
             $variation->set_status(false === $data['visible'] ? 'private' : 'publish');
         }
         // SKU.
         if (isset($data['sku'])) {
             $variation->set_sku(wc_clean($data['sku']));
         }
         // Thumbnail.
         if (isset($data['image']) && is_array($data['image'])) {
             $image = current($data['image']);
             if (is_array($image)) {
                 $image['position'] = 0;
             }
             $variation = $this->save_product_images($variation, array($image));
         }
         // Virtual variation.
         if (isset($data['virtual'])) {
             $variation->set_virtual($data['virtual']);
         }
         // Downloadable variation.
         if (isset($data['downloadable'])) {
             $is_downloadable = $data['downloadable'];
             $variation->set_downloadable($is_downloadable);
         } else {
             $is_downloadable = $variation->get_downloadable();
         }
         // Downloads.
         if ($is_downloadable) {
             // Downloadable files.
             if (isset($data['downloads']) && is_array($data['downloads'])) {
                 $variation = $this->save_downloadable_files($variation, $data['downloads']);
             }
             // Download limit.
             if (isset($data['download_limit'])) {
                 $variation->set_download_limit($data['download_limit']);
             }
             // Download expiry.
             if (isset($data['download_expiry'])) {
                 $variation->set_download_expiry($data['download_expiry']);
             }
         }
         // Shipping data.
         $variation = $this->save_product_shipping_data($variation, $data);
         // Stock handling.
         $manage_stock = (bool) $variation->get_manage_stock();
         if (isset($data['managing_stock'])) {
             $manage_stock = $data['managing_stock'];
         }
         $variation->set_manage_stock($manage_stock);
         $stock_status = $variation->get_stock_status();
         if (isset($data['in_stock'])) {
             $stock_status = true === $data['in_stock'] ? 'instock' : 'outofstock';
         }
         $variation->set_stock_status($stock_status);
         $backorders = $variation->get_backorders();
         if (isset($data['backorders'])) {
             $backorders = $data['backorders'];
         }
         $variation->set_backorders($backorders);
         if ($manage_stock) {
             if (isset($data['stock_quantity'])) {
                 $variation->set_stock_quantity($data['stock_quantity']);
             }
         } else {
             $variation->set_backorders('no');
             $variation->set_stock_quantity('');
         }
         // Regular Price.
         if (isset($data['regular_price'])) {
             $variation->set_regular_price($data['regular_price']);
         }
         // Sale Price.
         if (isset($data['sale_price'])) {
             $variation->set_sale_price($data['sale_price']);
         }
         if (isset($data['sale_price_dates_from'])) {
             $variation->set_date_on_sale_from($data['sale_price_dates_from']);
         }
         if (isset($data['sale_price_dates_to'])) {
             $variation->set_date_on_sale_to($data['sale_price_dates_to']);
         }
         // Tax class.
         if (isset($data['tax_class'])) {
             $variation->set_tax_class($data['tax_class']);
         }
         // Update taxonomies.
         if (isset($variation['attributes'])) {
             $_attributes = array();
             foreach ($variation['attributes'] as $attribute_key => $attribute) {
                 if (!isset($attribute['name'])) {
                     continue;
                 }
                 $taxonomy = 0;
                 $_attribute = array();
                 if (isset($attribute['slug'])) {
                     $taxonomy = $this->get_attribute_taxonomy_by_slug($attribute['slug']);
                 }
                 if (!$taxonomy) {
                     $taxonomy = sanitize_title($attribute['name']);
                 }
                 if (isset($attributes[$taxonomy])) {
                     $_attribute = $attributes[$taxonomy];
                 }
                 if (isset($_attribute['is_variation']) && $_attribute['is_variation']) {
                     $_attribute_key = sanitize_title($_attribute['name']);
                     if (isset($_attribute['is_taxonomy']) && $_attribute['is_taxonomy']) {
                         // Don't use wc_clean as it destroys sanitized characters
                         $_attribute_value = isset($attribute['option']) ? sanitize_title(stripslashes($attribute['option'])) : '';
                     } else {
                         $_attribute_value = isset($attribute['option']) ? wc_clean(stripslashes($attribute['option'])) : '';
                     }
                     $_attributes[$_attribute_key] = $_attribute_value;
                 }
             }
             $variation->set_attributes($_attributes);
         }
         $variation->save();
         do_action('woocommerce_api_save_product_variation', $variation_id, $menu_order, $variation);
     }
     return true;
 }
 /**
  * Save variations.
  *
  * @throws WC_REST_Exception REST API exceptions.
  * @param WC_Product      $product          Product instance.
  * @param WP_REST_Request $request          Request data.
  * @param bool            $single_variation True if saving only a single variation.
  * @return bool
  */
 protected function save_variations_data($product, $request, $single_variation = false)
 {
     global $wpdb;
     if ($single_variation) {
         $variations = array($request);
     } else {
         $variations = $request['variations'];
     }
     foreach ($variations as $menu_order => $data) {
         $variation_id = isset($data['id']) ? absint($data['id']) : 0;
         $variation = new WC_Product_Variation($variation_id);
         // Create initial name and status.
         if (!$variation->get_slug()) {
             /* translators: 1: variation id 2: product name */
             $variation->set_name(sprintf(__('Variation #%1$s of %2$s', 'woocommerce'), $variation->get_id(), $product->get_name()));
             $variation->set_status(isset($data['visible']) && false === $data['visible'] ? 'private' : 'publish');
         }
         // Parent ID.
         $variation->set_parent_id($product->get_id());
         // Menu order.
         $variation->set_menu_order($menu_order);
         // Status.
         if (isset($data['visible'])) {
             $variation->set_status(false === $data['visible'] ? 'private' : 'publish');
         }
         // SKU.
         if (isset($data['sku'])) {
             $variation->set_sku(wc_clean($data['sku']));
         }
         // Thumbnail.
         if (isset($data['image']) && is_array($data['image'])) {
             $image = $data['image'];
             $image = current($image);
             if (is_array($image)) {
                 $image['position'] = 0;
             }
             $variation = $this->save_product_images($variation, array($image));
         }
         // Virtual variation.
         if (isset($data['virtual'])) {
             $variation->set_virtual($data['virtual']);
         }
         // Downloadable variation.
         if (isset($data['downloadable'])) {
             $variation->set_downloadable($data['downloadable']);
         }
         // Downloads.
         if ($variation->get_downloadable()) {
             // Downloadable files.
             if (isset($data['downloads']) && is_array($data['downloads'])) {
                 $variation = $this->save_downloadable_files($variation, $data['downloads']);
             }
             // Download limit.
             if (isset($data['download_limit'])) {
                 $variation->set_download_limit($data['download_limit']);
             }
             // Download expiry.
             if (isset($data['download_expiry'])) {
                 $variation->set_download_expiry($data['download_expiry']);
             }
         }
         // Shipping data.
         $variation = $this->save_product_shipping_data($variation, $data);
         // Stock handling.
         if (isset($data['manage_stock'])) {
             $variation->set_manage_stock($data['manage_stock']);
         }
         if (isset($data['in_stock'])) {
             $variation->set_stock_status(true === $data['in_stock'] ? 'instock' : 'outofstock');
         }
         if (isset($data['backorders'])) {
             $variation->set_backorders($data['backorders']);
         }
         if ($variation->get_manage_stock()) {
             if (isset($data['stock_quantity'])) {
                 $variation->set_stock_quantity($data['stock_quantity']);
             } elseif (isset($data['inventory_delta'])) {
                 $stock_quantity = wc_stock_amount($variation->get_stock_amount());
                 $stock_quantity += wc_stock_amount($data['inventory_delta']);
                 $variation->set_stock_quantity($stock_quantity);
             }
         } else {
             $variation->set_backorders('no');
             $variation->set_stock_quantity('');
         }
         // Regular Price.
         if (isset($data['regular_price'])) {
             $variation->set_regular_price($data['regular_price']);
         }
         // Sale Price.
         if (isset($data['sale_price'])) {
             $variation->set_sale_price($data['sale_price']);
         }
         if (isset($data['date_on_sale_from'])) {
             $variation->set_date_on_sale_from($data['date_on_sale_from']);
         }
         if (isset($data['date_on_sale_to'])) {
             $variation->set_date_on_sale_to($data['date_on_sale_to']);
         }
         // Tax class.
         if (isset($data['tax_class'])) {
             $variation->set_tax_class($data['tax_class']);
         }
         // Description.
         if (isset($data['description'])) {
             $variation->set_description(wp_kses_post($data['description']));
         }
         // Update taxonomies.
         if (isset($data['attributes'])) {
             $attributes = array();
             $parent_attributes = $product->get_attributes();
             foreach ($data['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 = sanitize_title($attribute['name']);
                 }
                 if (!$attribute_id && !$attribute_name) {
                     continue;
                 }
                 if (!isset($parent_attributes[$attribute_name]) || !$parent_attributes[$attribute_name]->get_variation()) {
                     continue;
                 }
                 $attribute_key = sanitize_title($parent_attributes[$attribute_name]->get_name());
                 $attribute_value = isset($attribute['option']) ? wc_clean(stripslashes($attribute['option'])) : '';
                 if ($parent_attributes[$attribute_name]->is_taxonomy()) {
                     // If dealing with a taxonomy, we need to get the slug from the name posted to the API.
                     $term = get_term_by('name', $attribute_value, $attribute_name);
                     if ($term && !is_wp_error($term)) {
                         $attribute_value = $term->slug;
                     } else {
                         $attribute_value = sanitize_title($attribute_value);
                     }
                 }
                 $attributes[$attribute_key] = $attribute_value;
             }
             $variation->set_attributes($attributes);
         }
         $variation->save();
         do_action('woocommerce_rest_save_product_variation', $variation->get_id(), $menu_order, $data);
     }
     return true;
 }
Ejemplo n.º 5
0
 /**
  * Add a product line item to the order. This is the only line item type with
  * it's own method because it saves looking up order amounts (costs are added up for you).
  * @param  \WC_Product $product
  * @param  int $qty
  * @param  array $args
  * @return int order item ID
  * @throws WC_Data_Exception
  */
 public function add_product($product, $qty = 1, $args = array())
 {
     if ($product) {
         $default_args = array('name' => $product->get_name(), 'tax_class' => $product->get_tax_class(), 'product_id' => $product->is_type('variation') ? $product->get_parent_id() : $product->get_id(), 'variation_id' => $product->is_type('variation') ? $product->get_id() : 0, 'variation' => $product->is_type('variation') ? $product->get_attributes() : array(), 'subtotal' => wc_get_price_excluding_tax($product, array('qty' => $qty)), 'total' => wc_get_price_excluding_tax($product, array('qty' => $qty)), 'quantity' => $qty);
     } else {
         $default_args = array('quantity' => $qty);
     }
     $args = wp_parse_args($args, $default_args);
     // BW compatibility with old args
     if (isset($args['totals'])) {
         foreach ($args['totals'] as $key => $value) {
             if ('tax' === $key) {
                 $args['total_tax'] = $value;
             } elseif ('tax_data' === $key) {
                 $args['taxes'] = $value;
             } else {
                 $args[$key] = $value;
             }
         }
     }
     $item = new WC_Order_Item_Product();
     $item->set_props($args);
     $item->set_backorder_meta();
     $item->set_order_id($this->get_id());
     $item->save();
     $this->add_item($item);
     wc_do_deprecated_action('woocommerce_order_add_product', array($this->get_id(), $item->get_id(), $product, $qty, $args), '2.7', 'Use woocommerce_new_order_item action instead.');
     return $item->get_id();
 }