WC_Product_CSV_Exporter::prepare_data_to_export
Prepare data for export.
Method of the class: WC_Product_CSV_Exporter{}
Hooks from the method
Returns
null. Nothing (null).
Usage
$WC_Product_CSV_Exporter = new WC_Product_CSV_Exporter(); $WC_Product_CSV_Exporter->prepare_data_to_export();
Changelog
| Since 3.1.0 | Introduced. |
WC_Product_CSV_Exporter::prepare_data_to_export() WC Product CSV Exporter::prepare data to export code WC 10.3.6
public function prepare_data_to_export() {
$args = array(
'status' => array( ProductStatus::PRIVATE, ProductStatus::PUBLISH, ProductStatus::DRAFT, ProductStatus::FUTURE, ProductStatus::PENDING ),
'limit' => $this->get_limit(),
'page' => $this->get_page(),
'orderby' => array(
'ID' => 'ASC',
),
'return' => 'objects',
'paginate' => true,
);
// Set up query args based on whether specific IDs are being exported.
// We ignore type/category initially when specific IDs are provided.
if ( ! empty( $this->product_ids_to_export ) ) {
$args['include'] = $this->product_ids_to_export;
} else {
// Use the type and category filters set on the instance.
$args['type'] = $this->product_types_to_export;
if ( ! empty( $this->product_category_to_export ) ) {
$args['category'] = $this->product_category_to_export;
}
}
/**
* Filter the query args for the product export.
*
* @since 3.5.0
* @param array $args Arguments to pass to wc_get_products().
*/
$args = apply_filters( "woocommerce_product_export_{$this->export_type}_query_args", $args );
if ( ! empty( $args['include'] ) ) {
$args['include'] = array_map( 'absint', (array) $args['include'] );
}
$products = wc_get_products( $args );
$this->total_rows = $products->total;
$this->row_data = array();
$variable_products = array();
foreach ( $products->products as $product ) {
// Check if the product is variable and if either the include or category filter is active.
// This is to ensure that product variations are only included if they are being selectively exported or if they are part of a category.
if ( ( ! empty( $args['include'] ) || ! empty( $args['category'] ) ) &&
$product->is_type( ProductType::VARIABLE ) &&
! in_array( $product->get_id(), $variable_products, true ) ) {
$variable_products[] = $product->get_id();
}
$this->row_data[] = $this->generate_row_data( $product );
}
// If variable products were identified (either through include or category filters), fetch their variations.
if ( ! empty( $variable_products ) ) {
foreach ( $variable_products as $parent_id ) {
$products = wc_get_products(
array(
'parent' => $parent_id,
'type' => array( ProductType::VARIATION ),
'return' => 'objects',
'limit' => -1,
)
);
if ( ! $products ) {
continue;
}
foreach ( $products as $product ) {
$this->row_data[] = $this->generate_row_data( $product );
}
}
}
}