Last active
September 28, 2022 15:59
-
-
Save EricBusch/893a5a86f19008acd0f9ec9636975cd6 to your computer and use it in GitHub Desktop.
Import Attribute From Multiple Fields with Preferred Terms [dfrpswc][datafeedr]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 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. | |
*/ | |
add_filter( 'dfrpswc_filter_attribute_value', function ( $value, $attribute, $post, $product, $set, $action ) { | |
$ai = new Dfrpswc_Attribute_Importer( 'color', $value, $attribute, $product ); | |
$ai->add_field( [ 'description.tags.attribute5' ], 'color' ); | |
$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( '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(); | |
}, 20, 6 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment