Skip to content

Instantly share code, notes, and snippets.

@EricBusch
Last active September 28, 2022 15:56
Show Gist options
  • Save EricBusch/7546c47b590f3c3f435390d3fed360c2 to your computer and use it in GitHub Desktop.
Save EricBusch/7546c47b590f3c3f435390d3fed360c2 to your computer and use it in GitHub Desktop.
Import Multiple Attributes from a Single Field [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.
*/
add_filter( 'dfrpswc_filter_attribute_value', function ( $value, $attribute, $post, $product, $set, $action ) {
// Change 'size' to the slug of the attribute you want to target.
$ai = new Dfrpswc_Attribute_Importer( 'size', $value, $attribute, $product );
// If the "size" field exists for the product, import it.
$ai->add_field( [ 'size' ] );
// If a field contains the following character, split the field into multiple values.
$ai->field_delimiter = ',';
return $ai->result();
}, 20, 6 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment