Skip to content

Instantly share code, notes, and snippets.

@EricBusch
Last active September 19, 2017 19:21
Show Gist options
  • Save EricBusch/43cf7f8afbf46c692fb39c1e940d93ae to your computer and use it in GitHub Desktop.
Save EricBusch/43cf7f8afbf46c692fb39c1e940d93ae to your computer and use it in GitHub Desktop.
Adds specific product fields as product tags. This code will get the values from specific product fields and then set those values as tags for the product. [datafeedr][dfrpswc]
<?php
/**
* Adds specific product fields as product tags.
*
* This code will get the values from specific product fields and then
* set those values as tags for the product.
*
* @param array $taxonomies An array keyed by taxonomy name and having values of taxonomy values.
* @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.
*/
add_filter( 'dfrpswc_filter_taxonomy_array', 'mycode_add_tags', 20, 5 );
function mycode_add_tags( $taxonomies, $post, $product, $set, $action ) {
$fields = array( 'category', 'productgroup' );
$search = array( ";", ">", "," );
$tags = array();
$replace = "||";
foreach ( $fields as $field ) {
if ( ! isset( $product[ $field ] ) ) {
continue;
}
$words = str_replace( $search, $replace, $product[ $field ] );
$words = explode( $replace, $words );
foreach ( $words as $word ) {
$tags[] = trim( $word );
}
}
$taxonomies['product_tag'] = array_unique( $tags );
return $taxonomies;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment