woocommerce/includes/export/class-wc-product-csv-export...

548 lines
16 KiB
PHP
Raw Normal View History

<?php
/**
* Handles product CSV export.
*
* @author Automattic
* @category Admin
* @package WooCommerce/Export
* @version 3.1.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Include dependencies.
*/
2017-05-15 09:42:10 +00:00
if ( ! class_exists( 'WC_CSV_Batch_Exporter', false ) ) {
include_once( WC_ABSPATH . 'includes/export/abstract-wc-csv-batch-exporter.php' );
}
/**
* WC_Product_CSV_Exporter Class.
*/
class WC_Product_CSV_Exporter extends WC_CSV_Batch_Exporter {
/**
* Type of export used in filter names.
2017-05-12 19:15:08 +00:00
* @var string
*/
protected $export_type = 'product';
2017-05-12 13:48:30 +00:00
/**
* Should meta be exported?
* @var boolean
*/
protected $enable_meta_export = false;
/**
* Which product types are beign exported.
* @var array
*/
protected $product_types_to_export = array();
/**
* Constructor.
*/
public function __construct() {
parent::__construct();
$this->set_product_types_to_export( array_merge( array_keys( wc_get_product_types() ), array( 'variation' ) ) );
}
/**
* Should meta be exported?
2017-05-12 19:15:08 +00:00
*
* @since 3.1.0
2017-05-12 13:48:30 +00:00
* @param bool $enable_meta_export
*/
public function enable_meta_export( $enable_meta_export ) {
$this->enable_meta_export = (bool) $enable_meta_export;
}
/**
* Product types to export.
2017-05-12 19:15:08 +00:00
*
* @since 3.1.0
2017-05-12 13:48:30 +00:00
* @param array $product_types_to_export
*/
public function set_product_types_to_export( $product_types_to_export ) {
$this->product_types_to_export = array_map( 'wc_clean', $product_types_to_export );
}
/**
* Return an array of columns to export.
*
2017-05-12 19:15:08 +00:00
* @since 3.1.0
* @return array
*/
public function get_default_column_names() {
return apply_filters( "woocommerce_product_export_{$this->export_type}_default_columns", array(
'id' => __( 'ID', 'woocommerce' ),
'type' => __( 'Type', 'woocommerce' ),
'sku' => __( 'SKU', 'woocommerce' ),
'name' => __( 'Name', 'woocommerce' ),
'published' => __( 'Published', 'woocommerce' ),
'featured' => __( 'Is featured?', 'woocommerce' ),
'catalog_visibility' => __( 'Visibility in catalog', 'woocommerce' ),
2017-05-26 21:01:15 +00:00
'short_description' => __( 'Short description', 'woocommerce' ),
'description' => __( 'Description', 'woocommerce' ),
'date_on_sale_from' => __( 'Date sale price starts', 'woocommerce' ),
'date_on_sale_to' => __( 'Date sale price ends', 'woocommerce' ),
'tax_status' => __( 'Tax status', 'woocommerce' ),
'tax_class' => __( 'Tax class', 'woocommerce' ),
'stock_status' => __( 'In stock?', 'woocommerce' ),
2017-05-12 15:16:04 +00:00
'stock' => __( 'Stock', 'woocommerce' ),
'backorders' => __( 'Backorders allowed?', 'woocommerce' ),
'sold_individually' => __( 'Sold individually?', 'woocommerce' ),
'weight' => sprintf( __( 'Weight (%s)', 'woocommerce' ), get_option( 'woocommerce_weight_unit' ) ),
'length' => sprintf( __( 'Length (%s)', 'woocommerce' ), get_option( 'woocommerce_dimension_unit' ) ),
'width' => sprintf( __( 'Width (%s)', 'woocommerce' ), get_option( 'woocommerce_dimension_unit' ) ),
'height' => sprintf( __( 'Height (%s)', 'woocommerce' ), get_option( 'woocommerce_dimension_unit' ) ),
'reviews_allowed' => __( 'Allow customer reviews?', 'woocommerce' ),
2017-05-26 21:01:15 +00:00
'purchase_note' => __( 'Purchase note', 'woocommerce' ),
'sale_price' => __( 'Sale price', 'woocommerce' ),
'regular_price' => __( 'Regular price', 'woocommerce' ),
'category_ids' => __( 'Categories', 'woocommerce' ),
'tag_ids' => __( 'Tags', 'woocommerce' ),
2017-05-26 21:01:15 +00:00
'shipping_class_id' => __( 'Shipping class', 'woocommerce' ),
2017-05-26 14:57:29 +00:00
'images' => __( 'Images', 'woocommerce' ),
2017-05-26 21:01:15 +00:00
'download_limit' => __( 'Download limit', 'woocommerce' ),
'download_expiry' => __( 'Download expiry days', 'woocommerce' ),
'parent_id' => __( 'Parent', 'woocommerce' ),
'grouped_products' => __( 'Grouped products', 'woocommerce' ),
'upsell_ids' => __( 'Upsells', 'woocommerce' ),
'cross_sell_ids' => __( 'Cross-sells', 'woocommerce' ),
2017-05-26 18:26:55 +00:00
'product_url' => __( 'External URL', 'woocommerce' ),
'button_text' => __( 'Button text', 'woocommerce' ),
) );
}
/**
* Prepare data for export.
2017-05-12 19:15:08 +00:00
*
* @since 3.1.0
*/
public function prepare_data_to_export() {
$columns = $this->get_column_names();
$products = wc_get_products( array(
'status' => array( 'private', 'publish' ),
2017-05-12 13:48:30 +00:00
'type' => $this->product_types_to_export,
'limit' => $this->get_limit(),
'page' => $this->get_page(),
'orderby' => array(
2017-06-01 18:52:14 +00:00
'ID' => 'ASC',
),
'return' => 'objects',
'paginate' => true,
) );
$this->total_rows = $products->total;
$this->row_data = array();
foreach ( $products->products as $product ) {
$row = array();
foreach ( $columns as $column_id => $column_name ) {
$column_id = strstr( $column_id, ':' ) ? current( explode( ':', $column_id ) ) : $column_id;
2017-05-12 16:00:14 +00:00
$value = '';
2017-05-12 13:48:30 +00:00
// Skip some columns if dynamically handled later or if we're being selective.
if ( in_array( $column_id, array( 'downloads', 'attributes', 'meta' ) ) || ! $this->is_column_exporting( $column_id ) ) {
2017-05-10 19:48:56 +00:00
continue;
}
2017-05-12 16:00:14 +00:00
// Filter for 3rd parties.
if ( has_filter( "woocommerce_product_export_{$this->export_type}_column_{$column_id}" ) ) {
$value = apply_filters( "woocommerce_product_export_{$this->export_type}_column_{$column_id}", '', $product );
// Handle special columns which don't map 1:1 to product data.
2017-05-12 16:00:14 +00:00
} elseif ( is_callable( array( $this, "get_column_value_{$column_id}" ) ) ) {
2017-05-12 13:48:30 +00:00
$value = $this->{"get_column_value_{$column_id}"}( $product );
2017-05-12 13:48:30 +00:00
// Default and custom handling.
} elseif ( is_callable( array( $product, "get_{$column_id}" ) ) ) {
$value = $product->{"get_{$column_id}"}( 'edit' );
}
2017-05-12 13:48:30 +00:00
$row[ $column_id ] = $value;
}
2017-05-12 13:48:30 +00:00
$this->prepare_downloads_for_export( $product, $row );
$this->prepare_attributes_for_export( $product, $row );
$this->prepare_meta_for_export( $product, $row );
2017-05-12 15:39:02 +00:00
$this->row_data[] = apply_filters( 'woocommerce_product_export_row_data', $row, $product );
2017-05-12 13:48:30 +00:00
}
}
2017-05-12 13:48:30 +00:00
/**
* Get published value.
2017-05-12 19:15:08 +00:00
*
* @since 3.1.0
2017-05-12 13:48:30 +00:00
* @param WC_Product $product
2017-05-12 19:15:08 +00:00
* @return int
2017-05-12 13:48:30 +00:00
*/
protected function get_column_value_published( $product ) {
return 'publish' === $product->get_status( 'edit' ) ? 1 : 0;
}
2017-05-12 13:48:30 +00:00
/**
* Get product_cat value.
2017-05-12 19:15:08 +00:00
*
* @since 3.1.0
2017-05-12 13:48:30 +00:00
* @param WC_Product $product
2017-05-12 19:15:08 +00:00
* @return string
2017-05-12 13:48:30 +00:00
*/
protected function get_column_value_category_ids( $product ) {
$term_ids = $product->get_category_ids( 'edit' );
return $this->format_term_ids( $term_ids, 'product_cat' );
}
2017-05-10 22:05:06 +00:00
2017-05-12 13:48:30 +00:00
/**
* Get product_tag value.
2017-05-12 19:15:08 +00:00
*
* @since 3.1.0
2017-05-12 13:48:30 +00:00
* @param WC_Product $product
2017-05-12 19:15:08 +00:00
* @return string
2017-05-12 13:48:30 +00:00
*/
protected function get_column_value_tag_ids( $product ) {
$term_ids = $product->get_tag_ids( 'edit' );
return $this->format_term_ids( $term_ids, 'product_tag' );
}
2017-05-10 22:05:06 +00:00
2017-05-12 13:48:30 +00:00
/**
* Get product_shipping_class value.
2017-05-12 19:15:08 +00:00
*
* @since 3.1.0
2017-05-12 13:48:30 +00:00
* @param WC_Product $product
2017-05-12 19:15:08 +00:00
* @return string
2017-05-12 13:48:30 +00:00
*/
protected function get_column_value_shipping_class_id( $product ) {
$term_ids = $product->get_shipping_class_id( 'edit' );
return $this->format_term_ids( $term_ids, 'product_shipping_class' );
}
2017-05-10 22:05:06 +00:00
2017-05-12 13:48:30 +00:00
/**
2017-05-26 14:57:29 +00:00
* Get images value.
2017-05-12 19:15:08 +00:00
*
* @since 3.1.0
2017-05-12 13:48:30 +00:00
* @param WC_Product $product
2017-05-12 19:15:08 +00:00
* @return string
2017-05-12 13:48:30 +00:00
*/
2017-05-26 14:57:29 +00:00
protected function get_column_value_images( $product ) {
2017-05-12 13:48:30 +00:00
$image_ids = array_merge( array( $product->get_image_id( 'edit' ) ), $product->get_gallery_image_ids( 'edit' ) );
$images = array();
2017-05-12 13:48:30 +00:00
foreach ( $image_ids as $image_id ) {
2017-05-30 17:33:17 +00:00
$image = wp_get_attachment_image_src( $image_id, 'full' );
2017-05-12 13:48:30 +00:00
if ( $image ) {
$images[] = $image[0];
}
2017-05-12 13:48:30 +00:00
}
2017-05-30 17:33:17 +00:00
2017-05-12 13:48:30 +00:00
return implode( ', ', $images );
}
2017-05-12 13:48:30 +00:00
/**
* Prepare linked products for export.
2017-05-12 19:15:08 +00:00
*
* @since 3.1.0
2017-05-12 13:48:30 +00:00
* @param int[] $linked_products
* @return string
*/
protected function prepare_linked_products_for_export( $linked_products ) {
$product_list = array();
foreach ( $linked_products as $linked_product ) {
if ( $linked_product->get_sku() ) {
$product_list[] = $linked_product->get_sku();
} else {
$product_list[] = 'id:' . $linked_product->get_id();
}
2017-05-12 13:48:30 +00:00
}
return implode( ',', $product_list );
}
/**
* Get cross_sell_ids value.
2017-05-12 19:15:08 +00:00
*
* @since 3.1.0
2017-05-12 13:48:30 +00:00
* @param WC_Product $product
* @return string
*/
protected function get_column_value_cross_sell_ids( $product ) {
return $this->prepare_linked_products_for_export( array_filter( array_map( 'wc_get_product', (array) $product->get_cross_sell_ids( 'edit' ) ) ) );
}
/**
* Get upsell_ids value.
2017-05-12 19:15:08 +00:00
*
* @since 3.1.0
2017-05-12 13:48:30 +00:00
* @param WC_Product $product
* @return string
*/
protected function get_column_value_upsell_ids( $product ) {
return $this->prepare_linked_products_for_export( array_filter( array_map( 'wc_get_product', (array) $product->get_upsell_ids( 'edit' ) ) ) );
}
2017-05-12 13:48:30 +00:00
/**
* Get parent_id value.
2017-05-12 19:15:08 +00:00
*
* @since 3.1.0
2017-05-12 13:48:30 +00:00
* @param WC_Product $product
* @return string
*/
protected function get_column_value_parent_id( $product ) {
if ( $product->get_parent_id( 'edit' ) ) {
$parent = wc_get_product( $product->get_parent_id( 'edit' ) );
2017-05-26 20:30:17 +00:00
if ( ! $parent ) {
return '';
2017-05-12 13:48:30 +00:00
}
2017-05-26 20:30:17 +00:00
return $parent->get_sku( 'edit' ) ? $parent->get_sku( 'edit' ) : 'id:' . $parent->get_id();
2017-05-12 13:48:30 +00:00
}
return '';
}
2017-05-10 21:16:53 +00:00
2017-05-26 20:30:17 +00:00
/**
* Get grouped_products value.
2017-05-26 20:30:17 +00:00
*
* @since 3.1.0
* @param WC_Product $product
* @return string
*/
protected function get_column_value_grouped_products( $product ) {
2017-05-26 20:30:17 +00:00
if ( 'grouped' !== $product->get_type() ) {
return '';
}
$grouped_products = array();
2017-05-26 20:30:17 +00:00
$child_ids = $product->get_children( 'edit' );
foreach ( $child_ids as $child_id ) {
$child = wc_get_product( $child_id );
if ( ! $child ) {
continue;
}
$grouped_products[] = $child->get_sku( 'edit' ) ? $child->get_sku( 'edit' ) : 'id:' . $child_id;
2017-05-26 20:30:17 +00:00
}
return implode( ',', $grouped_products );
2017-05-26 20:30:17 +00:00
}
2017-05-12 15:16:04 +00:00
/**
* Get download_limit value.
2017-05-12 19:15:08 +00:00
*
* @since 3.1.0
2017-05-12 15:16:04 +00:00
* @param WC_Product $product
* @return string
*/
protected function get_column_value_download_limit( $product ) {
return $product->is_downloadable() && $product->get_download_limit( 'edit' ) ? $product->get_download_limit( 'edit' ) : '';
}
/**
* Get download_expiry value.
2017-05-12 19:15:08 +00:00
*
* @since 3.1.0
2017-05-12 15:16:04 +00:00
* @param WC_Product $product
* @return string
*/
protected function get_column_value_download_expiry( $product ) {
return $product->is_downloadable() && $product->get_download_expiry( 'edit' ) ? $product->get_download_expiry( 'edit' ) : '';
}
/**
* Get stock value.
2017-05-12 19:15:08 +00:00
*
* @since 3.1.0
2017-05-12 15:16:04 +00:00
* @param WC_Product $product
* @return string
*/
protected function get_column_value_stock( $product ) {
$manage_stock = $product->get_manage_stock( 'edit' );
$stock_quantity = $product->get_stock_quantity( 'edit' );
if ( $product->is_type( 'variation' && 'parent' === $manage_stock ) ) {
return 'parent';
} elseif ( $manage_stock ) {
return $stock_quantity;
} else {
return '';
}
}
2017-05-15 09:42:10 +00:00
/**
* Get stock status value.
*
* @since 3.1.0
* @param WC_Product $product
* @return string
*/
protected function get_column_value_stock_status( $product ) {
$status = $product->get_stock_status( 'edit' );
return 'instock' === $status ? 1 : 0;
}
/**
* Get backorders.
*
* @since 3.1.0
* @param WC_Product $product
* @return string
*/
protected function get_column_value_backorders( $product ) {
$backorders = $product->get_backorders( 'edit' );
switch ( $backorders ) {
case 'notify' :
return 'notify';
default :
return wc_string_to_bool( $backorders ) ? 1 : 0;
}
}
2017-05-12 15:16:04 +00:00
/**
2017-05-25 19:33:14 +00:00
* Get type value.
2017-05-12 19:15:08 +00:00
*
* @since 3.1.0
2017-05-12 15:16:04 +00:00
* @param WC_Product $product
* @return string
*/
protected function get_column_value_type( $product ) {
$types = array();
$types[] = $product->get_type();
if ( $product->is_downloadable() ) {
$types[] = 'downloadable';
}
if ( $product->is_virtual() ) {
$types[] = 'virtual';
}
return implode( ', ', $types );
}
2017-05-12 13:48:30 +00:00
/**
* Export downloads.
2017-05-12 19:15:08 +00:00
*
* @since 3.1.0
* @param WC_Product $product
* @param array $row
2017-05-12 13:48:30 +00:00
*/
protected function prepare_downloads_for_export( $product, &$row ) {
if ( $product->is_downloadable() && $this->is_column_exporting( 'downloads' ) ) {
$downloads = $product->get_downloads( 'edit' );
if ( $downloads ) {
$i = 1;
foreach ( $downloads as $download ) {
2017-05-26 21:01:15 +00:00
$this->column_names[ 'downloads:name' . $i ] = sprintf( __( 'Download %d name', 'woocommerce' ), $i );
2017-05-12 13:48:30 +00:00
$this->column_names[ 'downloads:url' . $i ] = sprintf( __( 'Download %d URL', 'woocommerce' ), $i );
$row[ 'downloads:name' . $i ] = $download->get_name();
$row[ 'downloads:url' . $i ] = $download->get_file();
$i++;
}
}
}
}
2017-05-10 21:16:53 +00:00
2017-05-12 13:48:30 +00:00
/**
* Export attributes data.
2017-05-12 19:15:08 +00:00
*
* @since 3.1.0
* @param WC_Product $product
2017-05-12 13:48:30 +00:00
* @param array $row
*/
protected function prepare_attributes_for_export( $product, &$row ) {
if ( $this->is_column_exporting( 'attributes' ) ) {
2017-05-12 15:16:04 +00:00
$attributes = $product->get_attributes();
$default_attributes = $product->get_default_attributes();
2017-05-10 21:16:53 +00:00
2017-05-12 13:48:30 +00:00
if ( count( $attributes ) ) {
$i = 1;
foreach ( $attributes as $attribute_name => $attribute ) {
$this->column_names[ 'attributes:name' . $i ] = sprintf( __( 'Attribute %d name', 'woocommerce' ), $i );
$this->column_names[ 'attributes:value' . $i ] = sprintf( __( 'Attribute %d value(s)', 'woocommerce' ), $i );
$this->column_names[ 'attributes:visible' . $i ] = sprintf( __( 'Attribute %d visible', 'woocommerce' ), $i );
$this->column_names[ 'attributes:taxonomy' . $i ] = sprintf( __( 'Attribute %d global', 'woocommerce' ), $i );
2017-05-12 13:48:30 +00:00
if ( is_a( $attribute, 'WC_Product_Attribute' ) ) {
$row[ 'attributes:name' . $i ] = wc_attribute_label( $attribute->get_name(), $product );
2017-05-10 21:16:53 +00:00
2017-05-12 13:48:30 +00:00
if ( $attribute->is_taxonomy() ) {
$terms = $attribute->get_terms();
$values = array();
2017-05-10 21:53:17 +00:00
2017-05-12 13:48:30 +00:00
foreach ( $terms as $term ) {
$values[] = $term->name;
2017-05-10 21:53:17 +00:00
}
$row[ 'attributes:value' . $i ] = implode( ', ', $values );
$row[ 'attributes:taxonomy' . $i ] = 1;
2017-05-12 13:48:30 +00:00
} else {
$row[ 'attributes:value' . $i ] = implode( ', ', $attribute->get_options() );
$row[ 'attributes:taxonomy' . $i ] = 0;
2017-05-10 21:16:53 +00:00
}
$row[ 'attributes:visible' . $i ] = $attribute->get_visible();
2017-05-12 13:48:30 +00:00
} else {
$row[ 'attributes:name' . $i ] = wc_attribute_label( $attribute_name, $product );
2017-05-10 21:53:17 +00:00
2017-05-12 13:48:30 +00:00
if ( 0 === strpos( $attribute_name, 'pa_' ) ) {
$option_term = get_term_by( 'slug', $attribute, $attribute_name );
$row[ 'attributes:value' . $i ] = $option_term && ! is_wp_error( $option_term ) ? $option_term->name : $attribute;
$row[ 'attributes:taxonomy' . $i ] = 1;
2017-05-12 13:48:30 +00:00
} else {
$row[ 'attributes:value' . $i ] = $attribute;
$row[ 'attributes:taxonomy' . $i ] = 0;
2017-05-12 13:48:30 +00:00
}
$row[ 'attributes:visible' . $i ] = '';
2017-05-10 21:16:53 +00:00
}
2017-05-12 15:16:04 +00:00
if ( $product->is_type( 'variable' ) && isset( $default_attributes[ sanitize_title( $attribute_name ) ] ) ) {
2017-05-26 21:01:15 +00:00
$this->column_names[ 'attributes:default' . $i ] = sprintf( __( 'Attribute %d default', 'woocommerce' ), $i );
2017-05-12 15:16:04 +00:00
$default_value = $default_attributes[ sanitize_title( $attribute_name ) ];
if ( 0 === strpos( $attribute_name, 'pa_' ) ) {
$option_term = get_term_by( 'slug', $default_value, $attribute_name );
$row[ 'attributes:default' . $i ] = $option_term && ! is_wp_error( $option_term ) ? $option_term->name : $default_value;
} else {
$row[ 'attributes:default' . $i ] = $default_value;
}
}
2017-05-12 13:48:30 +00:00
$i++;
2017-05-10 21:16:53 +00:00
}
}
2017-05-12 13:48:30 +00:00
}
}
2017-05-12 13:48:30 +00:00
/**
* Export meta data.
2017-05-12 19:15:08 +00:00
*
* @since 3.1.0
* @param WC_Product $product
* @param array $row
2017-05-12 13:48:30 +00:00
*/
protected function prepare_meta_for_export( $product, &$row ) {
if ( $this->enable_meta_export ) {
$meta_data = $product->get_meta_data();
if ( count( $meta_data ) ) {
$i = 1;
foreach ( $meta_data as $meta ) {
if ( ! is_scalar( $meta->value ) ) {
continue;
}
$column_key = 'meta:' . esc_attr( $meta->key );
$this->column_names[ $column_key ] = sprintf( __( 'Meta: %s', 'woocommerce' ), $meta->key );
$row[ $column_key ] = $meta->value;
$i++;
}
}
}
}
}