Skip to content

Instantly share code, notes, and snippets.

@EricBusch
Created February 11, 2017 14:49
Show Gist options
  • Save EricBusch/952863730d172847dcd3be15b41f1e63 to your computer and use it in GitHub Desktop.
Save EricBusch/952863730d172847dcd3be15b41f1e63 to your computer and use it in GitHub Desktop.
Automatically add the product's color as a color attribute from multiple fields with defaults and normalize the color name. [datafeedr]
<?php
/**
* Add the product's color as a color attribute for this product.
*
* The attribute "Color" with a slug of "color" must already exist here:
* WordPress Admin Area > Products > Attributes.
*
* @param array|string $value The current value of the $attribute for this $post.
* @param string $attribute The slug of the attribute. Examples: pa_color or pa_shoe-size
* @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|string The updated attribute's value.
*/
add_filter( 'dfrpswc_filter_attribute_value', 'mycode_add_color_attribute', 20, 6 );
function mycode_add_color_attribute( $value, $attribute, $post, $product, $set, $action ) {
if ( $attribute != 'pa_color' ) {
return $value;
}
if ( isset( $product['color'] ) ) {
return mycode_get_color( $product['color'], $product['color'] );
}
if ( isset( $product['extratexttwo'] ) ) {
return mycode_get_color( $product['extratexttwo'], $product['extratexttwo'] );
}
if ( isset( $product['name'] ) ) {
return mycode_get_color( $product['name'] );
}
return $value;
}
/**
* A function to normalize color attribute names.
*
* This returns a normalized value of a term based on a supplied
* array of mappings of a key (desired word) mapped to an array of
* keywords (undesired words).
*
* @param string $field The value to normalize.
* @param string $default Optional. What to return if the $field value doesn't have a "normalized" value. Default: ''
*
* @return string Normalized attribute value.
*/
function mycode_get_color( $field, $default = '' ) {
$map = array();
// ++++++++++ Begin Editing Here ++++++++++
// Use "Red" as attribute name if product color is "cherry" or "rose" or "ruby" or "crimson" or "scarlet".
$map["Red"] = array( "cherry", "rose", "ruby", "crimson", "scarlet" );
// Use "Blue" as attribute name if product color is "navy" or "indigo" or "azure" or "periwinkle" or "lavender".
$map["Blue"] = array( "navy", "indigo", "azure", "periwinkle", "lavender" );
// Use "Green" as attribute name if product color is "lime" or "moss" or "mint" or "pine" or "spring bud".
$map["Green"] = array( "lime", "moss", "mint", "pine", "spring bud" );
// Add "White" as attribute if product color is "pale stone".
$map["White"] = array( "pale stone" );
// Add "Brown" as attribute if product color is "khaki" or "Caramel" or "beige".
$map["Brown"] = array( "khaki", "Caramel", "beige" );
// Add "Grey" as attribute if product color is "dark shadow" or "gray" or "silver" or "silver/grey".
$map["Grey"] = array( "dark shadow", "gray", "silver", "silver/grey" );
// Add "Purple" as attribute if product color is "beetroot" or "burgundy" or "poisonberry" or "mulberry".
$map["Purple"] = array( "beetroot", "burgundy", "poisonberry", "mulberry" );
// ++++++++++ Stop Editing Here ++++++++++
$terms = array();
foreach ( $map as $key => $keywords ) {
if ( preg_match( '/\b' . preg_quote( $key, '/' ) . '\b/i', $field ) ) {
$terms[] = $key;
}
foreach ( $keywords as $keyword ) {
if ( preg_match( '/\b' . preg_quote( $keyword, '/' ) . '\b/i', $field ) ) {
$terms[] = $key;
}
}
}
if ( ! empty( $terms ) ) {
return implode( WC_DELIMITER, array_unique( $terms ) );
}
return $default;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment