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

Yes, there is a delay. They should become available in the next day or so. Sorry for the inconvenience!

@jans9208
Copy link

No problem! Thanks for the update!

@jans9208
Copy link

jans9208 commented Oct 19, 2021

Hi Eric,

I hope you are doing well! All goes well for buydrinks.nl. We are growing and we will launch a new winesite very soon. The products that have to be on this website will be sent from a warehouse via drop-shipping. We will also list these same wines via an affiliate network (probably Awin or Daisycon) on our website buydrinks.

I was wondering if it would be possible to import these wines via Datafeedr, and set them automatically as simple product instead of external/affiliate products? In this way we can use Wordpress multisite and maintain a dropshipping website and an affiliate website via Datafeedr via the same installation.

Best regards,

Simon

@EricBusch
Copy link
Author

EricBusch commented Oct 19, 2021

Hi Simon

Glad to hear your site is going well!

Yes, you could use this action to make the changes you need:

do_action( 'dfrpswc_post_save_product', $this );

You will need to have enabled this change before that action will actually work.

The $this argument actually contains all of the objects you will need.

Primarily, it will contain:

$this->wc_product // This is the WC_Product object
$this->post // This is an array of the Post data related to the product imported into WooCommerce
$this->dfr_product // This is an array of the product data from the Datafeedr API

So you could do something like this (this is untested and just a simple example):

add_action( 'dfrpswc_post_save_product', function ( Dfrpswc_Product_Update_Handler $updater ) {
	$updater->wc_product->product_type = 'simple';
	$updater->wc_product->save();
} );

Hope this helps!

Eric

@jans9208
Copy link

jans9208 commented Oct 26, 2021 via email

@EricBusch
Copy link
Author

@jans9208 Yes, that should work. But instead of adding the same str_replace call over and over again, this might simplify your code and make it easier to update your code now and in the future.

add_filter( 'dfrpswc_filter_post_array', function ( $post, $product, $set, $action ) {

	/**
	 * This is for the descriptions.
	 *
	 * The keys are the terms you want to replace and the values are what you want to use instead.
	 */
	$desc_map = [
		'strong' => '',
		'br br'  => '',
		'Br Br'  => '',
	];

	/**
	 * This is for the title.
	 *
	 * The keys are the terms you want to replace and the values are what you want to use instead.
	 */
	$title_map = [
		'bij Jumbo' => '',
	];
	
	$desc  = str_replace( array_keys( $desc_map ), array_values( $desc_map ), $post['post_content'] ?? '' );
	$title = str_replace( array_keys( $title_map ), array_values( $title_map ), $post['post_title'] ?? '' );

	$post['post_content'] = trim( $desc );
	$post['post_title']   = trim( $title );

	return $post;

}, 20, 4 );

@jans9208
Copy link

jans9208 commented Nov 4, 2021

@jans9208 Yes, that should work. But instead of adding the same str_replace call over and over again, this might simplify your code and make it easier to update your code now and in the future.

add_filter( 'dfrpswc_filter_post_array', function ( $post, $product, $set, $action ) {

	/**
	 * This is for the descriptions.
	 *
	 * The keys are the terms you want to replace and the values are what you want to use instead.
	 */
	$desc_map = [
		'strong' => '',
		'br br'  => '',
		'Br Br'  => '',
	];

	/**
	 * This is for the title.
	 *
	 * The keys are the terms you want to replace and the values are what you want to use instead.
	 */
	$title_map = [
		'bij Jumbo' => '',
	];
	
	$desc  = str_replace( array_keys( $desc_map ), array_values( $desc_map ), $post['post_content'] ?? '' );
	$title = str_replace( array_keys( $title_map ), array_values( $title_map ), $post['post_title'] ?? '' );

	$post['post_content'] = trim( $desc );
	$post['post_title']   = trim( $title );

	return $post;

}, 20, 4 );

Thanks Eric, sorry for my noob code. Online marketeer, no developer ;)

@EricBusch
Copy link
Author

No worries. Your code would work fine! My code is the same as yours but maybe just easier to update.

@jans9208
Copy link

jans9208 commented Nov 25, 2021

Hi Eric,

I have a question about new or existing networks/publisher on the Datafeedr ecosystem. I have just received an invitation for "Topdrinks" from Tradetracker. The publisher is active.

Whenever i browse this URL https://www.datafeedr.
com/networks it is not listed, or yet.

Another publisher is "COOP NL - FamilyBlend" they have accepted my request weeks ago but is not listed on the Datafeedr website. Both networks have a productfeed. Are there any restrictions before accepting feeds from your side? Because this would cause delays or publishing problems in the future.

Looking forward to hear from you.

Best regards,

Simon

@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