Skip to content

Instantly share code, notes, and snippets.

@felipe-pita
Created July 15, 2020 15:46
Show Gist options
  • Save felipe-pita/c70e404da6380db8e4618661b3424c25 to your computer and use it in GitHub Desktop.
Save felipe-pita/c70e404da6380db8e4618661b3424c25 to your computer and use it in GitHub Desktop.
<?php
// Register Custom Taxonomy
function register_product_origem() {
$labels = array(
'name' => 'Origens',
'singular_name' => 'Origem',
'menu_name' => 'Origem',
);
$rewrite = array(
'slug' => 'origem',
'with_front' => true,
'hierarchical' => false,
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => $rewrite,
);
register_taxonomy( 'product_origem', array( 'product' ), $args );
}
add_action( 'init', 'register_product_origem', 0 );
/**
* Register the 'Custom Column' column in the importer.
*
* @param array $options
* @return array $options
*/
function add_column_to_importer( $options ) {
// column slug => column name
$options['origem'] = 'Origem';
return $options;
}
add_filter( 'woocommerce_csv_product_import_mapping_options', 'add_column_to_importer' );
/**
* Process the data read from the CSV file.
* This just saves the value in meta data, but you can do anything you want here with the data.
*
* @param WC_Product $object - Product being imported or updated.
* @param array $data - CSV data read for the product.
* @return WC_Product $object
*/
function process_import( $object, $data ) {
if (is_a($object, 'WC_Product')) {
if (!empty( $data['origem'] ) ) {
$object->save();
$product_id = $object->get_id();
$tag = array(intval($data['origem']));
wp_set_object_terms($product_id, $tag, 'product_origem');
}
}
return $object;
}
add_filter( 'woocommerce_product_import_pre_insert_product_object', 'process_import', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment