Skip to content

Instantly share code, notes, and snippets.

@EricBusch
Forked from EricBusch/add_color_attribute-05.php
Last active September 28, 2022 16:12
Show Gist options
  • Save EricBusch/8b1dda0847449ba3781d79600669d6af to your computer and use it in GitHub Desktop.
Save EricBusch/8b1dda0847449ba3781d79600669d6af 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][dfrpswc]
<?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', function ( $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;
}, 20, 6 );
/**
* 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 = [];
// ++++++++++ Begin Editing Here ++++++++++
// Use 'Red' as attribute name if product color is 'cherry' or 'rose' or 'ruby' or 'crimson' or 'scarlet'.
$map['Red'] = [ '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'] = [ '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'] = [ 'lime', 'moss', 'mint', 'pine', 'spring bud' ];
// Add 'White' as attribute if product color is 'pale stone'.
$map['White'] = [ 'pale stone' ];
// Add 'Brown' as attribute if product color is 'khaki' or 'Caramel' or 'beige'.
$map['Brown'] = [ 'khaki', 'Caramel', 'beige' ];
// Add 'Grey' as attribute if product color is 'dark shadow' or 'gray' or 'silver' or 'silver/grey'.
$map['Grey'] = [ 'dark shadow', 'gray', 'silver', 'silver/grey' ];
// Add 'Purple' as attribute if product color is 'beetroot' or 'burgundy' or 'poisonberry' or 'mulberry'.
$map['Purple'] = [ 'beetroot', 'burgundy', 'poisonberry', 'mulberry' ];
// ++++++++++ Stop Editing Here ++++++++++
$terms = [];
foreach ( $map as $key => $keywords ) {
if ( preg_match( '/\b' . preg_quote( $key, '/' ) . '\b/iu', $field ) ) {
$terms[] = $key;
}
foreach ( $keywords as $keyword ) {
if ( preg_match( '/\b' . preg_quote( $keyword, '/' ) . '\b/iu', $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