Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function glsr_fn_assigned_product_catgeories (array $values) {
- require_once ABSPATH.'wp-admin/includes/taxonomy.php';
- foreach ($values['assigned_posts'] as $postId) {
- // is this an assigned product?
- if ('product' !== get_post_type($postId)) {
- continue;
- }
- // get the product's attached categories
- $productTerms = get_the_terms($postId, 'product_cat');
- foreach ($productTerms as $productTerm) {
- $term = term_exists($productTerm->slug, glsr()->taxonomy);
- // does a Site Reviews category already exist for this product category?
- if (!empty($term['term_taxonomy_id'])) {
- // if it does, add it to the assigned_terms array
- $values['assigned_terms'][] = $term['term_taxonomy_id'];
- continue;
- }
- // if it does not, try to create it
- $term = wp_insert_term($productTerm->name, glsr()->taxonomy, [
- 'description' => 'Woo ttid: '.$productTerm->term_taxonomy_id,
- 'slug' => $productTerm->slug,
- ]);
- if (is_wp_error($term)) {
- // if there was an error then log it to the Site Reviews console
- glsr_log()->error($term->get_error_message());
- continue;
- }
- // if it does, add the newly created category to the assigned_terms array
- $values['assigned_terms'][] = $term['term_taxonomy_id'];
- }
- }
- // make sure the term taxonomy IDs in the array is unique
- $values['assigned_terms'] = array_values(array_unique($values['assigned_terms']));
- return $values;
- }
- add_filter('site-reviews/defaults/create-review', 'glsr_fn_assigned_product_catgeories');
- add_filter('site-reviews/defaults/reviews', 'glsr_fn_assigned_product_catgeories');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement