Created
May 29, 2018 14:48
-
-
Save EricBusch/e6bd4d53512a959f8348604842fbcf10 to your computer and use it in GitHub Desktop.
Prevent product categories from being overwritten during a Product Set update. [datafeedr][dfrps][dfrpswc]
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 | |
/** | |
* Get all Term IDs this product is associated with and store them as an array | |
* in the postmeta table keyed by "_custom_product_cat_ids". | |
* | |
* @param array $post Array containing WordPress Post information. | |
* @param array $product Array containing Datafeedr Product information. | |
* @param array $set Array containing Product Set information. | |
* @param string $action Either "update" or "insert" depending on what the Product Set is doing. | |
* | |
* @return array Updated $post array. | |
*/ | |
function mycode_save_custom_categories( $post, $product, $set, $action ) { | |
// Skip if we are importing the product for the first time. | |
if ( 'update' != $action ) { | |
return $post; | |
} | |
// Get the Term IDs that this product's Product Set is associated with. | |
$this_product_set_term_ids = ( isset( $set['postmeta']['_dfrps_cpt_terms'][0] ) ) ? unserialize( $set['postmeta']['_dfrps_cpt_terms'][0] ) : []; | |
// Get the Term IDs that this product is associated with in other Product Sets (excluding the current Product Set). | |
$other_product_set_term_ids = dfrpswc_get_all_term_ids_for_product( $post, $set ); | |
// Get the Term IDs that this product is associated with via customization after import. | |
$custom_term_ids = wp_get_post_terms( $post['ID'], DFRPSWC_TAXONOMY, [ 'fields' => 'ids' ] ); | |
// Merge, convert all to integers and uniquify all Term IDs. | |
$term_ids = array_merge( $this_product_set_term_ids, $other_product_set_term_ids, $custom_term_ids ); | |
$term_ids = array_unique( array_map( 'absint', $term_ids ) ); | |
update_post_meta( $post['ID'], '_custom_product_cat_ids', $term_ids ); | |
return $post; | |
} | |
add_filter( 'dfrpswc_filter_post_array', 'mycode_save_custom_categories', 20, 4 ); | |
/** | |
* Retrieves the Term IDs set by mycode_save_custom_categories() and adds them to the | |
* $taxonomies array for 'product_cat'. | |
* | |
* @param array $taxonomies Array keyed by taxonomy name and having values of taxonomy values. | |
* @param array $post Array containing WordPress Post information. | |
* @param array $product Array containing Datafeedr Product information. | |
* @param array $set Array containing Product Set information. | |
* @param string $action Either "update" or "insert" depending on what the Product Set is doing. | |
* | |
* @return array Updated $taxonomies array. | |
*/ | |
function mycode_restore_saved_custom_categories( $taxonomies, $post, $product, $set, $action ) { | |
// Skip if we are importing the product for the first time. | |
if ( 'update' != $action ) { | |
return $taxonomies; | |
} | |
// Get Term IDs saved by mycode_save_custom_categories(). | |
$term_ids = get_post_meta( $post['ID'], '_custom_product_cat_ids', true ); | |
$term_ids = ( is_array( $term_ids ) ) ? $term_ids : []; | |
$taxonomies['product_cat'] = $term_ids; | |
return $taxonomies; | |
} | |
add_filter( 'dfrpswc_filter_taxonomy_array', 'mycode_restore_saved_custom_categories', 20, 5 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment