Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 );
@mauspartyhouse

This comment has been minimized.

Copy link

@mauspartyhouse mauspartyhouse commented Mar 18, 2020

Thank you for this great work.
Im a happy datafeedr pro user and would like to be able to do the above not as an attribute, but as a tag.

For example 'Add the product's Size as a tag' or 'Add the product's Brand as a tag'.
Would this be possible?
Thank you so much for your help.

@EricBusch

This comment has been minimized.

Copy link
Owner Author

@EricBusch EricBusch commented Mar 18, 2020

Thanks! Yes, you can definitely do that. Here's how https://datafeedrapi.helpscoutdocs.com/article/154-add-product-tags

@jans9208

This comment has been minimized.

Copy link

@jans9208 jans9208 commented Jan 2, 2021

Hello there, i'm using Datafreedr for a few weeks now on my website www.buydrinks.nl. What a great product! I'm importing products with different properties e.g. whisky and wine. Is it possible to import values only for wines and skip whisky and vodka for instance? Pseudocode would be: "If category = wine, then import the given values, else skip import".

This is the code i am using now

`function mycode_import_grape_attribute( $value, $attribute, $post, $product, $set, $action ) {

$ai = new Dfrpswc_Attribute_Importer( 'winegrape', $value, $attribute, $product );

$ai->add_field( [ 'grapes', 'druivenras','winegrape' ], 'winegrape', 'other' );

    $ai->field_delimiter = ',';

return $ai->result();

}`

The product feeds from my affiliate contain the fields: 'grapes', 'druivenras', 'winegrape'. If these field are not present my code attaches the value "other" to all of my products, even they are not wine. The delimiter field is used because the field can contain more grape varieties.

The reason is that properties from the feeds containing wines are added to whisky as well. So my whisky product contain information about winegrapes and vintages i am importing.

Thanks a lot!

@EricBusch

This comment has been minimized.

Copy link
Owner Author

@EricBusch EricBusch commented Jan 2, 2021

Have you tried using Controlled/Preferred vocabulary when importing your attributes?

$ai->add_term( "Wine", [ "wine", "winegrape" ] );

When you use the add_term() method and a product being imported doesn't match the provided terms, then no attribute is imported.

Hope this helps!

@Jottem15

This comment has been minimized.

Copy link

@Jottem15 Jottem15 commented Jan 11, 2021

Hi there, first of all thank you for developing such a great plugin. I'm setting up the plugin for my online store www.bamboebaas.nl and must say i'm a big fan already. Just wanted to thank you guys for delivering a great product with good support in public so other people that maybe doubt to start using it change their minds quickly.

Best regards,
Thomas

@EricBusch

This comment has been minimized.

Copy link
Owner Author

@EricBusch EricBusch commented Jan 12, 2021

Thanks! Glad to help and happy to hear you are enjoying Datafeedr! :)

@jans9208

This comment has been minimized.

Copy link

@jans9208 jans9208 commented Feb 28, 2021

Hello Eric, This worked for me thanks a lot and sorry for the late response. I didn't get notified, or is missed it.

I have one more question about the field delimiter. I am using a product feed where the winegrapes are displayed as "Sauvignon Blanc, Riesling" and "Sauvignon blanc/Riesling". The code i am using right now is:

$ai->field_delimiter = ',';

I want to add an extra condition so that the "/" is also seen as a delimiter in the same import run. I have tried several options:

$ai->field_delimiter = ',','/';

and this

$ai->field_delimiter = ',';
$ai->field_delimiter = ',/;

Can you please give me direction how to validate the delimiter in the import function?

Thanks in advance

Best regards,

Simon Janssen

@EricBusch

This comment has been minimized.

Copy link
Owner Author

@EricBusch EricBusch commented Mar 1, 2021

At this point the code doesn't support multiple delimiters.

@jans9208

This comment has been minimized.

Copy link

@jans9208 jans9208 commented Mar 1, 2021

Hi Eric, thanks for your update and quick response. I will have to wait then ;)

Best regards,

Simon

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