Parse shipping class ID and tags IDs

This commit is contained in:
Claudio Sanches 2017-05-22 18:54:30 -03:00
parent f294447543
commit 775a934b74
2 changed files with 44 additions and 34 deletions

View File

@ -463,15 +463,13 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
}
// Product categories.
// @todo
if ( isset( $data['categories'] ) && is_array( $data['categories'] ) ) {
$product = $this->save_taxonomy_terms( $product, $data['categories'] );
if ( isset( $data['category_ids'] ) ) {
$product->set_category_ids( $data['category_ids'] );
}
// Product tags.
// @todo
if ( isset( $data['tags'] ) && is_array( $data['tags'] ) ) {
$product = $this->save_taxonomy_terms( $product, $data['tags'], 'tag' );
if ( isset( $data['tag_ids'] ) ) {
$product->set_tag_ids( $data['tag_ids'] );
}
// Downloadable.
@ -831,11 +829,7 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
// Shipping class.
if ( isset( $data['shipping_class_id'] ) ) {
$shipping_class_term = get_term_by( 'name', wc_clean( $data['shipping_class_id'] ), 'product_shipping_class' );
if ( $shipping_class_term ) {
$product->set_shipping_class_id( $shipping_class_term->term_id );
}
$product->set_shipping_class_id($data['shipping_class_id'] );
}
return $product;
@ -869,28 +863,6 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
return $product;
}
/**
* Save taxonomy terms.
*
* @todo
*
* @param WC_Product $product Product instance.
* @param array $terms Terms data.
* @param string $taxonomy Taxonomy name.
* @return WC_Product
*/
protected function save_taxonomy_terms( $product, $terms, $taxonomy = 'cat' ) {
$term_ids = wp_list_pluck( $terms, 'id' );
if ( 'cat' === $taxonomy ) {
$product->set_category_ids( $term_ids );
} elseif ( 'tag' === $taxonomy ) {
$product->set_tag_ids( $term_ids );
}
return $product;
}
/**
* Save default attributes.
*

View File

@ -208,6 +208,43 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
return $categories;
}
/**
* Parse a tag field from a CSV.
*
* @param string $field Field value.
* @return array
*/
protected function parse_tags( $field ) {
if ( empty( $field ) ) {
return array();
}
$names = array_map( 'wc_clean', array_map( 'trim', explode( ',', $field ) ) );
$tags = array();
foreach ( $names as $name ) {
if ( $term = get_term_by( 'name', $name, 'product_tag' ) ) {
$tags[] = $term->term_id;
}
}
return $tags;
}
/**
* Parse a shipping class field from a CSV.
*
* @param string $field Field value.
* @return int
*/
protected function parse_shipping_class( $field ) {
if ( $term = get_term_by( 'name', $field, 'product_tag' ) ) {
return $term->term_id;
}
return 0;
}
/**
* Get formatting callback.
*
@ -242,7 +279,8 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
'regular_price' => 'wc_format_decimal',
'stock_quantity' => 'absint',
'category_ids' => array( $this, 'parse_categories' ),
'tag_ids' => array( $this, 'parse_comma_field' ),
'tag_ids' => array( $this, 'parse_tags' ),
'shipping_class_id' => array( $this, 'parse_shipping_class' ),
'image_id' => array( $this, 'parse_comma_field' ),
'parent_id' => array( $this, 'parse_relative_field' ),
'upsell_ids' => array( $this, 'parse_relative_comma_field' ),