Skip to content

Instantly share code, notes, and snippets.

@EricBusch
Last active September 28, 2022 16:32
Show Gist options
  • Save EricBusch/e88fabd7ab524478af426e3c064d01d5 to your computer and use it in GitHub Desktop.
Save EricBusch/e88fabd7ab524478af426e3c064d01d5 to your computer and use it in GitHub Desktop.
Automatically add the product's gender as a gender attribute from multiple fields with defaults and normalize the gender name. [datafeedr][dfrpswc]
<?php
/**
* Add the product's gender as a gender attribute for this product.
*
* The attribute "Gender" with a slug of "gender"
* 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_size 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_gender' ) {
return $value;
}
if ( isset( $product['gender'] ) ) {
return mycode_get_gender( $product['gender'], $product['gender'] );
}
if ( isset( $product['extratexttwo'] ) ) {
return mycode_get_gender( $product['extratexttwo'], $product['extratexttwo'] );
}
if ( isset( $product['name'] ) ) {
return mycode_get_gender( $product['name'] );
}
return $value;
}, 20, 6 );
/**
* A function to normalize gender 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_gender( $field, $default = '' ) {
$map = [];
// ++++++++++ Begin Editing Here ++++++++++
// Use 'Mens' as attribute name if product gender is 'men' or 'mens' or 'male' or 'males' or 'man' or 'mans'.
$map['Mens'] = [ 'men', 'mens', 'male', 'males', 'man', 'mans' ];
// Use 'Womens' as attribute name if product gender is 'women' or 'woman' or 'womens' or 'womans' or 'female' or 'females'.
$map['Womens'] = [ 'women', 'woman', 'womens', 'womans', 'female', 'females' ];
// Use 'Unisex' as attribute name if product gender is 'unisex' or 'uni-sex'.
$map['Unisex'] = [ 'unisex', 'uni-sex' ];
// ++++++++++ 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