WC_Product::get_total_sales PHP Method

get_total_sales() public method

Get number total of sales.
Since: 2.7.0
public get_total_sales ( string $context = 'view' ) : integer
$context string
return integer
    public function get_total_sales($context = 'view')
    {
        return $this->get_prop('total_sales', $context);
    }

Usage Example

 /**
  * Get product data.
  *
  * @param WC_Product $product Product instance.
  * @return array
  */
 protected function get_product_data($product)
 {
     $data = array('id' => $product->get_id(), 'name' => $product->get_name(), 'slug' => $product->get_slug(), 'permalink' => $product->get_permalink(), 'date_created' => wc_rest_prepare_date_response($product->get_date_created()), 'date_modified' => wc_rest_prepare_date_response($product->get_date_modified()), 'type' => $product->get_type(), 'status' => $product->get_status(), 'featured' => $product->is_featured(), 'catalog_visibility' => $product->get_catalog_visibility(), 'description' => wpautop(do_shortcode($product->get_description())), 'short_description' => apply_filters('woocommerce_short_description', $product->get_short_description()), 'sku' => $product->get_sku(), 'price' => $product->get_price(), 'regular_price' => $product->get_regular_price(), 'sale_price' => $product->get_sale_price() ? $product->get_sale_price() : '', 'date_on_sale_from' => $product->get_date_on_sale_from() ? date('Y-m-d', $product->get_date_on_sale_from()) : '', 'date_on_sale_to' => $product->get_date_on_sale_to() ? date('Y-m-d', $product->get_date_on_sale_to()) : '', 'price_html' => $product->get_price_html(), 'on_sale' => $product->is_on_sale(), 'purchasable' => $product->is_purchasable(), 'total_sales' => $product->get_total_sales(), 'virtual' => $product->is_virtual(), 'downloadable' => $product->is_downloadable(), 'downloads' => $this->get_downloads($product), 'download_limit' => $product->get_download_limit(), 'download_expiry' => $product->get_download_expiry(), 'download_type' => 'standard', 'external_url' => $product->is_type('external') ? $product->get_product_url() : '', 'button_text' => $product->is_type('external') ? $product->get_button_text() : '', 'tax_status' => $product->get_tax_status(), 'tax_class' => $product->get_tax_class(), 'manage_stock' => $product->managing_stock(), 'stock_quantity' => $product->get_stock_quantity(), 'in_stock' => $product->is_in_stock(), 'backorders' => $product->get_backorders(), 'backorders_allowed' => $product->backorders_allowed(), 'backordered' => $product->is_on_backorder(), 'sold_individually' => $product->is_sold_individually(), 'weight' => $product->get_weight(), 'dimensions' => array('length' => $product->get_length(), 'width' => $product->get_width(), 'height' => $product->get_height()), 'shipping_required' => $product->needs_shipping(), 'shipping_taxable' => $product->is_shipping_taxable(), 'shipping_class' => $product->get_shipping_class(), 'shipping_class_id' => $product->get_shipping_class_id(), '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()), 'parent_id' => $product->get_parent_id(), 'purchase_note' => wpautop(do_shortcode(wp_kses_post($product->get_purchase_note()))), 'categories' => $this->get_taxonomy_terms($product), 'tags' => $this->get_taxonomy_terms($product, 'tag'), 'images' => $this->get_images($product), 'attributes' => $this->get_attributes($product), 'default_attributes' => $this->get_default_attributes($product), 'variations' => array(), 'grouped_products' => array(), 'menu_order' => $product->get_menu_order());
     return $data;
 }
WC_Product