Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EricBusch/ecc15f780c175a5eb6d6333f6e7ff67a to your computer and use it in GitHub Desktop.
Save EricBusch/ecc15f780c175a5eb6d6333f6e7ff67a to your computer and use it in GitHub Desktop.
Prevent taxonomy-based attributes from being overwritten when a Product Set updates a product already in your store. [datafeedr]
<?php
/**
* Prevent taxonomy-based attributes from being overwritten when a Product Set
* updates a product already in your store.
*
* @see wc_get_product_terms()
* @link http://stackoverflow.com/a/13454788/2489248
*
* @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', 'mycode_prevent_attribute_override_on_update', 100, 6 );
function mycode_prevent_attribute_override_on_update( $value, $attribute, $post, $product, $set, $action ) {
/**
* Add slug value for each attribute that should not be modified during a Product Set update.
* Slugs can be found here: WordPress Admin Area > Products > Attributes
*/
$attrs = array(
'color',
'size',
);
if ( 'insert' == $action ) {
return $value;
}
foreach ( $attrs as $attr ) {
$name = ( substr( $attr, 0, 3 ) === 'pa_' ) ? $attr : 'pa_' . $attr;
if ( $name == $attribute ) {
$value = wc_get_product_terms( $post['ID'], $name, array( 'fields' => 'names' ) );
$value = implode( WC_DELIMITER, $value );
}
}
return $value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment