Skip to content

Instantly share code, notes, and snippets.

@EricBusch
Last active June 20, 2022 09:00
Show Gist options
  • Save EricBusch/fbe9f7164ec0d4a85d70d0f430581e9a to your computer and use it in GitHub Desktop.
Save EricBusch/fbe9f7164ec0d4a85d70d0f430581e9a to your computer and use it in GitHub Desktop.
Examples of using the Dfrpswc_Attribute_Importer() class [dfrpswc][datafeedr]
<?php
/**
* Add the product's Size as an attribute.
*
* The attribute "Size" with a slug of "size" 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.
*/
function mycode_import_size_attribute( $value, $attribute, $post, $product, $set, $action ) {
$ai = new Dfrpswc_Attribute_Importer( 'size', $value, $attribute, $product );
$ai->add_field( [ 'size' ] );
// If a field contains a comma "," split the field into multiple values.
$ai->field_delimiter = ',';
$ai->add_term( "Extra Small", [ "xs", "x-small" ] );
$ai->add_term( "Small", [ "s", "sm" ], [ "x-small" ] );
$ai->add_term( "Medium", [ "m", "md", "med" ] );
$ai->add_term( "Large", [ "l", "lg" ], [ "x-large", "xx-large", "xxx-large" ] );
$ai->add_term( "Extra Large", [ "x-large", "xl" ] );
return $ai->result();
}
add_filter( 'dfrpswc_filter_attribute_value', 'mycode_import_size_attribute', 20, 6 );
/**
* Add the product's Color as an attribute.
*
* 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.
*/
function mycode_import_color_attribute( $value, $attribute, $post, $product, $set, $action ) {
$ai = new Dfrpswc_Attribute_Importer( 'color', $value, $attribute, $product );
$ai->add_field( [ 'color' ] );
$ai->add_field( [ 'description.tags.attribute5.name' ], 'color', "Multicolor" );
$ai->field_delimiter = ',';
$ai->add_term( "Black", [ "ebony", "onyx", "obsidian" ] );
$ai->add_term( "Blue", [ "navy", "indigo", "azure", "periwinkle", "lavender", "cobalt" ] );
$ai->add_term( "Brown", [ "caramel" ] );
$ai->add_term( "Green", [ "lime", "moss", "mint", "pine", "chartreuse", "sage", "olive" ] );
$ai->add_term( "Grey", [ "gray", "silver", "charcoal" ] );
$ai->add_term( "Pink", [ "rose", "fuchsia", "rouge", "salmon", "coral", "magenta" ] );
$ai->add_term( "Purple", [ "mauve", "violet", "lavender", "beetroot", "burgundy", "mulberry" ] );
$ai->add_term( "Orange" );
$ai->add_term( "Red", [ "cherry", "rose", "ruby", "crimson", "scarlet", "garnet" ] );
$ai->add_term( "Tan", [ "beige", "camel", "khaki" ] );
$ai->add_term( "White", [ "pearl", "alabaster", "ivory", "cream", "egg shell", "eggshell" ] );
$ai->add_term( "Yellow", [ "lemon" ] );
return $ai->result();
}
add_filter( 'dfrpswc_filter_attribute_value', 'mycode_import_color_attribute', 20, 6 );
/**
* Add the product's Gender as an attribute.
*
* 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_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.
*/
function mycode_import_gender_attribute( $value, $attribute, $post, $product, $set, $action ) {
$ai = new Dfrpswc_Attribute_Importer( 'gender', $value, $attribute, $product );
$ai->add_field( [ 'gender', 'attribute6.name.description' ], 'gender' );
$ai->add_term( "Womens", [ "female", "womens", "woman", "women" ] );
$ai->add_term( "Mens", [ "male", "mens", "man", "men" ] );
return $ai->result();
}
add_filter( 'dfrpswc_filter_attribute_value', 'mycode_import_gender_attribute', 20, 6 );
/**
* Normalize the product's Merchant's name when importing as an attribute.
*
* The attribute "Merchant" with a slug of "merchant" 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.
*/
function mycode_normalize_merchant_name( $value, $attribute, $post, $product, $set, $action ) {
$ai = new Dfrpswc_Attribute_Importer( 'merchant', $value, $attribute, $product );
$ai->add_field( [ "merchant" ], "merchant" );
$ai->add_term( "Adidas" );
$ai->add_term( "AppOutdoors", [ "AppOutdoors.com" ] );
$ai->add_term( "Black Diamond" );
$ai->add_term( "C.A.M.P.", [ "camp" ] );
$ai->add_term( "CampSaver", [ "CampSaver.com" ] );
$ai->add_term( "ChinaVision" );
$ai->add_term( "Mad Rock", [ "madrock" ] );
$ai->add_term( "Marmot" );
$ai->add_term( "Metolius" );
$ai->add_term( "Outdoor Play", [ "outdoorplay" ] );
$ai->add_term( "Petzl" );
$ai->add_term( "REI" );
$ai->add_term( "Summit Sports", [ "Summit Sports Sites", "SummitSports" ] );
$ai->add_term( "The North Face", [ "north face" ] );
$result = $ai->result();
return $result;
}
add_filter( 'dfrpswc_filter_attribute_value', 'mycode_normalize_merchant_name', 20, 6 );
/**
* Add the product's Age Group as an attribute.
*
* The attribute "Age Group" with a slug of "age-group" 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.
*/
function mycode_import_age_group_attribute( $value, $attribute, $post, $product, $set, $action ) {
$ai = new Dfrpswc_Attribute_Importer( 'age-group', $value, $attribute, $product );
$ai->add_field( [ 'gender.name.description.category' ], '', 'All Ages' );
$ai->add_term(
"Girls",
[ "girl" ],
[ "womens", "woman", "women", "adult", "ladies" ]
);
$ai->add_term(
"Boys",
[ "boy" ],
[ "mens", "man", "men", "adult" ]
);
$ai->add_term(
"Womens",
[ "female", "womens", "woman", "women" ],
[ "girl", "girls", "child", "children", "kids", "kid" ]
);
$ai->add_term(
"Mens",
[ "male", "mens", "man", "men" ],
[ "boy", "boys", "child", "children", "kids", "kid" ]
);
$ai->add_term(
"Adult",
[ "male", "mens", "man", "men", "female", "womens", "woman", "women" ],
[ "boy", "boys", "girl", "girls", "child", "children", "kids", "kid" ]
);
$ai->add_term(
"Children",
[ "boy", "boys", "girl", "girls", "child", "children", "kids", "kid" ],
[ "male", "mens", "man", "men", "womens", "woman", "women" ]
);
return $ai->result();
}
add_filter( 'dfrpswc_filter_attribute_value', 'mycode_import_age_group_attribute', 20, 6 );
/**
* Add the product's Country Name as an attribute.
*
* The attribute "Country" with a slug of "country" 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.
*/
function mycode_import_country_attribute( $value, $attribute, $post, $product, $set, $action ) {
$ai = new Dfrpswc_Attribute_Importer( 'country', $value, $attribute, $product );
$ai->add_field( [ "source", "merchant" ], "country", 'United States' );
$ai->add_term( "Australia", [ "AU" ] );
$ai->add_term( "Austria", [ "AT" ] );
$ai->add_term( "Belgium", [ "BE" ] );
$ai->add_term( "Brazil", [ "BR" ] );
$ai->add_term( "Brazil", [ "BR" ] );
$ai->add_term( "Canada", [ "CA" ] );
$ai->add_term( "Denmark", [ "DK" ] );
$ai->add_term( "Finland", [ "FI" ] );
$ai->add_term( "France", [ "FR" ] );
$ai->add_term( "Germany", [ "DE" ] );
$ai->add_term( "Ireland", [ "IE" ] );
$ai->add_term( "Italy", [ "IT" ] );
$ai->add_term( "India", [ "IN" ] );
$ai->add_term( "Netherlands", [ "NL" ] );
$ai->add_term( "New Zealand", [ "NZ" ] );
$ai->add_term( "Norway", [ "NO" ] );
$ai->add_term( "Poland", [ "PL" ] );
$ai->add_term( "Portugal", [ "PT" ] );
$ai->add_term( "Spain", [ "ES" ] );
$ai->add_term( "Sweden", [ "SE" ] );
$ai->add_term( "Switzerland", [ "CH" ] );
$ai->add_term( "United Kingdom", [ "UK", "GB" ] );
$ai->add_term( "United States", [ "US", "USA" ] );
return $ai->result();
}
add_filter( 'dfrpswc_filter_attribute_value', 'mycode_import_country_attribute', 20, 6 );
/**
* Add the product's Country Name as an attribute.
*
* The attribute "Country" with a slug of "country" 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.
*/
function mycode_import_material_attribute( $value, $attribute, $post, $product, $set, $action ) {
$ai = new Dfrpswc_Attribute_Importer( 'material', $value, $attribute, $product );
$ai->add_field( [ "description.name.material" ], "material" );
$ai->add_term( "Bamboo" );
$ai->add_term( "Denim", [ "jean", "jeans" ] );
$ai->add_term( "Flannel" );
$ai->add_term( "Fur" );
$ai->add_term( "Glass" );
$ai->add_term( "Leather", [ "cowhide" ] );
$ai->add_term( "Metal", [ "copper", "steel", "iron", "aluminum" ] );
$ai->add_term( "Nylon" );
$ai->add_term( "Plastic" );
$ai->add_term( "Polyester" );
$ai->add_term( "Rayon" );
$ai->add_term( "Spandex" );
$ai->add_term( "Wood" );
return $ai->result();
}
add_filter( 'dfrpswc_filter_attribute_value', 'mycode_import_material_attribute', 20, 6 );
@EricBusch
Copy link
Author

Hi @jans9208

Here's more information regarding our inclusion of merchants:

https://datafeedrapi.helpscoutdocs.com/article/27-request-a-missing-merchant

If you need to request a missing merchant, just let us know in the Contact Form here:

https://datafeedr.me/contact

Thanks!
Eric

@jans9208
Copy link

jans9208 commented Jun 19, 2022

Hi Eric,

You are all ok? I have to reach out to you again. Since a few week importing product properties doesn't go well. For some properties multiple instances are created. E.g; i have a property rumregion which sets the country of origin for the rum. Here you see the function.

In the attachment you see the result. Do you happen to know what the reason for this is and how i can fix it?

rumregion


  // Change 'color' to the slug of the attribute you want to target.
  $ai = new Dfrpswc_Attribute_Importer( 'rumregion', $value, $attribute, $product );

  $ai->add_field( [ 'description' ], 'rumregion' );

  // Add terms that will be allowed for import
  $ai->add_term( "Trinidad", [ "Trinidad" ] );
  $ai->add_term( "Tobago", [ "Tobago" ] );
  $ai->add_term( "Panama", [ "Tobago" ] );
  $ai->add_term( "Barbados", [ "Barbados" ] );
  $ai->add_term( "Cuba", [ "Cuba" ] );
  $ai->add_term( "Verenigde Staten", [ "Verenigde Staten" ] );
  $ai->add_term( "Dominicaanse Republiek", [ "Dominicaanse Rep" ] );
  $ai->add_term( "Fiji", [ "Fiji" ] );
  $ai->add_term( "Guatemala", [ "Guatemala" ] );
  $ai->add_term( "Belize", [ "Belize" ] );
  $ai->add_term( "Venezuela", [ "Venezuela" ] );
  
  return $ai->result();
}

add_filter( 'dfrpswc_filter_attribute_value', 'mycode_import_rumregion_attribute', 20, 6 );```

@EricBusch
Copy link
Author

@jans9208
Copy link

jans9208 commented Jun 20, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment