Skip to content

Instantly share code, notes, and snippets.

@EricBusch
Created November 17, 2017 13:55
Show Gist options
  • Save EricBusch/cc14b07c2490635f964b4562bef0b563 to your computer and use it in GitHub Desktop.
Save EricBusch/cc14b07c2490635f964b4562bef0b563 to your computer and use it in GitHub Desktop.
This will import the brands into the "product_brand" taxonomy when using the WooCommerce Brands plugin. [datafeedr][dfrpswc]
<?php
/**
* Adds the brand name to the 'product_brand' taxonomy.
*
* This will import the brands into the 'product_brand' taxonomy when using
* the WooCommerce Brands plugin.
*
* @link http://www.woothemes.com/products/brands/
*
* @param array $taxonomies The currently configured array of taxonomies.
* @param array $post An array of post data including ID, post_title, post_status, etc...
* @param array $product An array of product data returned from the Datafeedr API.
* @param array $set A post array for this Product Set with an array key of postmeta containing all post meta data.
* @param string $action The action the Product Set is performing. Value are either "insert" or "update".
*
* @return array Updated $taxonomies array
*/
function mycode_add_product_brand_taxonomy( $taxonomies, $post, $product, $set, $action ) {
$taxonomy = 'product_brand';
$brand = ( isset( $product['brand'] ) ) ? trim( $product['brand'] ) : false;
if ( ! $brand ) {
return $taxonomies;
}
$term = term_exists( $brand, $taxonomy );
if ( $term !== 0 && $term !== null ) {
$taxonomies[ $taxonomy ] = ( is_array( $term ) ) ? $term['term_id'] : $term;
} else {
$term = wp_insert_term( $brand, $taxonomy );
if ( is_array( $term ) ) {
$taxonomies[ $taxonomy ] = $term['term_id'];
}
}
return $taxonomies;
}
add_filter( 'dfrpswc_filter_taxonomy_array', 'mycode_add_product_brand_taxonomy', 10, 5 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment