Last active
November 6, 2022 11:06
-
-
Save EricBusch/996a78bcf6a4d23c9da0c2a3b96b41cf 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]
This file contains hidden or 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 | |
/** | |
* 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