Skip to content

Instantly share code, notes, and snippets.

@EricBusch
Forked from EricBusch/add_color_attribute-02.php
Last active September 28, 2022 16:17
Show Gist options
  • Save EricBusch/5036e38b9b1c1baafa166df5a6d20a3a to your computer and use it in GitHub Desktop.
Save EricBusch/5036e38b9b1c1baafa166df5a6d20a3a to your computer and use it in GitHub Desktop.
Automatically add the product's size as a size attribute from different product fields for this product. [datafeedr][dfrpswc]
<?php
/**
* Add the product's size as a size attribute for this product.
*
* 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_size 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 ) {
if ( $attribute !== 'pa_size' ) {
return $value;
}
if ( isset( $product['size'] ) ) {
return $product['size'];
}
if ( isset( $product['custom1'] ) ) {
return $product['custom1'];
}
return $value;
}, 20, 6 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment