Skip to content

Instantly share code, notes, and snippets.

@hereswhatidid
Last active September 8, 2017 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hereswhatidid/6789865a9713cd4dfcf9f7e14fb0e6bb to your computer and use it in GitHub Desktop.
Save hereswhatidid/6789865a9713cd4dfcf9f7e14fb0e6bb to your computer and use it in GitHub Desktop.
Plugin for adding any field as an attribute to the WooCommerce CSV import
<?php
function hwid333_add_custom_attribute_columns( $fields ) {
$fields[] = array(
'slug' => 'shirt-size',
'name' => 'Shirt Size', // This is the name of the column in the CSV
);
$fields[] = array(
'slug' => 'neck-style',
'name' => 'Neck Style', // This is the name of the column in the CSV
);
return $fields;
}
add_filter( 'hwid_wooimport_attribute_fields', 'hwid333_add_custom_attribute_columns' );
<?php
/**
* Plugin Name: Woo CSV Import
* Plugin URI: http://hereswhatidid.com
* Description: Sets up the custom attribute options from a properly formatted CSV
* Author: Gabe Shackle
* Author URI: http://hereswhatidid.com
* Text Domain: woo-csv-import
* Domain Path: /languages
* Version: 0.1.0
*
* @package Woo_CSV_Import
*/
class HWID_Woo_CSV_Import {
static $index = 1;
public $fields = array();
public function initialize() {
add_filter( 'woocommerce_csv_product_import_mapping_options', array( $this, 'add_column_to_importer' ) );
add_filter( 'woocommerce_csv_product_import_mapping_default_columns', array( $this, 'add_column_to_mapping_screen' ) );
// Process the CSV data prior to running it through the Woo importer
add_filter( 'woocommerce_product_importer_pre_expand_data', array( $this, 'set_import_object' ) );
// This is just to allow for duplicate SKUs, for some reason the CSV import always throws a duplicate SKU error
add_filter( 'wc_product_has_unique_sku', '__return_false' );
add_action( 'init', array( $this, 'set_fields' ) );
}
public function set_fields() {
$this->fields = apply_filters( 'hwid_wooimport_attribute_fields', $this->fields );
foreach( $this->fields as &$field ) {
if ( empty( $field['slug'] ) ) {
$field['slug'] = sanitize_title_with_dashes( $field['name'] );
}
}
}
/**
* Register the custom columns in the importer.
*
* @param array $options
* @return array $options
*/
public function add_column_to_importer( $options ) {
// column slug => column name
foreach( $this->fields as $field ) {
$options[$field['slug']] = $field['name'];
}
return $options;
}
/**
* Add automatic mapping support for custom attribute columns.
*
* @param array $columns
* @return array $columns
*/
public function add_column_to_mapping_screen( $columns ) {
foreach( $this->fields as $field ) {
$columns[$field['name']] = $field['slug'];
}
return $columns;
}
/**
* Processes the custom columns for inserting in to the custom attributes
*
* @param array $data Data to import.
*
* @return mixed
*/
public function set_import_object( $data ) {
foreach( $this->fields as $field ) {
if ( ! empty( $data[$field['slug']] ) ) {
self::add_attribute_group( $field['name'], explode( ', ', $data[$field['slug']] ), $data );
}
}
return $data;
}
/**
* Creates the properly formatted array keys for the specific attribute set
*
* @param string $name - Full name of the attribute type
* @param array $value - Array of attribute values
* @param array $data Data to import.
*/
static function add_attribute_group( $name, $value, &$data ) {
$data['attributes:name' . self::$index] = $name;
$data['attributes:value' . self::$index] = $value;
$data['attributes:visible' . self::$index] = true;
$data['attributes:taxonomy' . self::$index] = true;
self::$index++;
}
}
function hwid_woo_csv_import() {
global $hwid_woo_csv_import;
if( !isset($hwid_woo_csv_import) ) {
$hwid_woo_csv_import = new HWID_Woo_CSV_Import();
$hwid_woo_csv_import->initialize();
}
return $hwid_woo_csv_import;
}
// initialize
hwid_woo_csv_import();
@hereswhatidid
Copy link
Author

Insert the code from 'sample-usage.php' in your theme or functionality plugin. The 'woo-csv-import.php' file is a standalone plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment